Created
September 21, 2012 15:39
-
-
Save dsuch/3762240 to your computer and use it in GitHub Desktop.
MQCMD_SET_AUTH_REC in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
#include <cmqc.h> /* MQI */ | |
#include <cmqcfc.h> /* PCF */ | |
#include <cmqbc.h> /* MQAI */ | |
void check_call_result(MQCHAR *, MQLONG , MQLONG ); | |
void execute_command(MQHCONN); | |
int main(int argc, char *argv[]) | |
{ | |
MQHCONN hConn; /* handle to MQ connection */ | |
MQCHAR QMName[MQ_Q_MGR_NAME_LENGTH+1]=""; /* default QMgr name */ | |
MQLONG connReason; /* MQCONN reason code */ | |
MQLONG compCode; /* completion code */ | |
MQLONG reason; /* reason code */ | |
if (argc > 1) | |
strncpy(QMName, argv[1], (size_t)MQ_Q_MGR_NAME_LENGTH); | |
MQCONN(QMName, &hConn, &compCode, &connReason); | |
if (compCode == MQCC_FAILED) | |
{ | |
check_call_result("Queue Manager connection", compCode, connReason); | |
exit( (int)connReason); | |
} | |
execute_command(hConn); | |
if (connReason != MQRC_ALREADY_CONNECTED) | |
{ | |
MQDISC(&hConn, &compCode, &reason); | |
check_call_result("Disconnect from Queue Manager", compCode, reason); | |
} | |
return 0; | |
} | |
void execute_command(MQHCONN hConn) | |
{ | |
MQLONG reason; /* reason code */ | |
MQLONG compCode; /* completion code */ | |
MQHBAG adminBag = MQHB_UNUSABLE_HBAG; /* admin bag for mqExecute */ | |
MQHBAG responseBag = MQHB_UNUSABLE_HBAG;/* response bag for mqExecute */ | |
MQHBAG resultBag; /* result bag from mqExecute */ | |
MQLONG mqExecuteCC; /* mqExecute completion code */ | |
MQLONG mqExecuteRC; /* mqExecute reason code */ | |
mqCreateBag(MQCBO_ADMIN_BAG, &adminBag, &compCode, &reason); | |
check_call_result("Create the admin bag", compCode, reason); | |
if (compCode !=MQCC_OK) | |
return; | |
mqCreateBag(MQCBO_ADMIN_BAG, &responseBag, &compCode, &reason); | |
check_call_result("Create the response bag", compCode, reason); | |
if (compCode !=MQCC_OK) | |
return; | |
mqAddString(adminBag, MQCACF_AUTH_PROFILE_NAME, MQBL_NULL_TERMINATED, "*", &compCode, &reason); | |
mqAddInteger(adminBag, MQIACF_OBJECT_TYPE, MQOT_Q, &compCode, &reason); | |
mqAddInteger(adminBag, MQIACF_AUTH_ADD_AUTHS, MQAUTH_BROWSE, &compCode, &reason); | |
mqAddString(adminBag, MQCACF_GROUP_ENTITY_NAMES, MQBL_NULL_TERMINATED, "mqm", &compCode, &reason); | |
mqExecute(hConn, /* MQ connection handle */ | |
MQCMD_SET_AUTH_REC, /* Command to be executed */ | |
MQHB_NONE, /* No options bag */ | |
adminBag, /* Handle to bag containing commands */ | |
responseBag, /* Handle to bag to receive the response*/ | |
MQHO_NONE, /* Put msg on SYSTEM.ADMIN.COMMAND.QUEUE*/ | |
MQHO_NONE, /* Create a dynamic q for the response */ | |
&compCode, /* Completion code from the mqexecute */ | |
&reason); /* Reason code from mqexecute call */ | |
if (reason == MQRC_CMD_SERVER_NOT_AVAILABLE) | |
{ | |
printf("Please start the command server: <strmqcsv QMgrName>\n"); | |
MQDISC(&hConn, &compCode, &reason); | |
check_call_result("Disconnect from Queue Manager", compCode, reason); | |
exit(98); | |
} | |
if ( compCode == MQCC_OK ) | |
printf("All good\n"); | |
else | |
{ | |
printf("Command failed: Completion Code = %d : Reason = %d\n", compCode, reason); | |
if (reason == MQRCCF_COMMAND_FAILED) | |
{ | |
mqInquireBag(responseBag, MQHA_BAG_HANDLE, 0, &resultBag, &compCode, &reason); | |
check_call_result("Get the result bag handle", compCode, reason); | |
mqInquireInteger(resultBag, MQIASY_COMP_CODE, MQIND_NONE, &mqExecuteCC, | |
&compCode, &reason); | |
check_call_result("Get the completion code from the result bag", compCode, reason); | |
mqInquireInteger(resultBag, MQIASY_REASON, MQIND_NONE, &mqExecuteRC, | |
&compCode, &reason); | |
check_call_result("Get the reason code from the result bag", compCode, reason); | |
printf("Error returned by the command server: Completion Code = %d : Reason = %d\n", mqExecuteCC, mqExecuteRC); | |
} | |
} | |
if (adminBag != MQHB_UNUSABLE_HBAG) | |
{ | |
mqDeleteBag(&adminBag, &compCode, &reason); | |
check_call_result("Delete the admin bag", compCode, reason); | |
} | |
if (responseBag != MQHB_UNUSABLE_HBAG) | |
{ | |
mqDeleteBag(&responseBag, &compCode, &reason); | |
check_call_result("Delete the response bag", compCode, reason); | |
} | |
} | |
void check_call_result(char *callText, MQLONG cc, MQLONG rc) | |
{ | |
if (cc != MQCC_OK) | |
printf("%s failed: Completion Code = %d : Reason = %d\n", callText, cc, rc); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment