My C with MQ receive a message return code 2037 - c

I run the C program, which connect to MQ and try to get a message from it. I always get a message:
MQGET ended with reason code 2037
which means that MQ is not opened, but MQOPEN CC=0 RC=0
MQ error log is empty.
this is the program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cmqc.h> /* includes for MQI*/
#include <cmqxc.h>
int main(int argc, char **argv)
{
MQCNO Connect_options = {MQCNO_DEFAULT};/MQNONNX opt*/
MQCD ClientConn = {MQCD_CLIENT_CONN_DEFAULT};/*client channel*/
MQHCONN Hcon; /* connection handle */
MQHOBJ Hobj; /* object handle */
MQLONG CompCode; /* completion code */
MQLONG OpenCode; /* MQOPEN completion code*/
MQLONG Reason; /* reason code */
MQOD od = {MQOD_DEFAULT}; /* Object Descriptor */
MQMD md = {MQMD_DEFAULT}; /* Message Descriptor */
MQPMO pmo = {MQPMO_DEFAULT}; /* put message options*/
MQLONG O_options; /* MQOPEN options */
MQLONG C_options; /* MQCLOSE options */
MQGMO gmo = {MQGMO_DEFAULT}; /* get message options */
char QMgrName[MQ_Q_MGR_NAME_LENGTH+1];
char QName[MQ_Q_NAME_LENGTH+1];
char channelName[MQ_CHANNEL_NAME_LENGTH+1];
char hostname[1024];
char port[4];
MQLONG buflen; /* buffer length*/
char TempBuf[65536];
int msgsToGet;
int msgsGot;
if (argc != 6)
{
printf("Usage: MQTest11 QMgrName ChlName hostname port QName\n");
return(1);
}
**/* copy MQ manager name */**
strncpy(QMgrName, argv[1], MQ_Q_MGR_NAME_LENGTH);
QMgrName[MQ_Q_MGR_NAME_LENGTH] = '\0';
**/* copy channel name */**
strncpy(channelName, argv[2], MQ_CHANNEL_NAME_LENGTH);
channelName[MQ_CHANNEL_NAME_LENGTH] = '\0';
**/* copy hostname */**
strncpy(hostname, argv[3], 1023);
hostname[1023] = '\0';
**/* copy port number */**
strncpy(port,argv[4],4);
strncpy(QName, argv[5], MQ_Q_NAME_LENGTH);
QName[MQ_Q_NAME_LENGTH] = '\0';
**/* copy hostname for connection */**
strncpy(ClientConn.ConnectionName,hostname, MQ_CONN_NAME_LENGTH);
**/* copy channel name */**
strncpy(ClientConn.ChannelName,channelName,MQ_CHANNEL_NAME_LENGTH);
**/* Point the MQCNO to the client connection definition */**
Connect_options.ClientConnPtr = &ClientConn;
Connect_options.Version = MQCNO_VERSION_2;
**/* use MQCONNX */**
if (CompCode == MQCC_FAILED)
{
/* exit with print the reason */
}
else
{
strncpy(od.ObjectName, QName, (size_t)MQ_Q_NAME_LENGTH);
O_options = MQOO_OUTPUT + MQOO_FAIL_IF_QUIESCING;
MQOPEN(Hcon, /* connection handle */
&od, /* object descriptor for queue */
O_options, /* open options */
&Hobj, /* object handle */
&OpenCode, /* MQOPEN completion code */
&Reason); /* reason code */
printf("MQTest11 MQOPEN CC=%ld RC=%ld\n", CompCode, Reason);
if (OpenCode == MQCC_OK) /* if MQOPEN , then continue in the while loop */
{
gmo.Options = MQGMO_WAIT + MQGMO_CONVERT;
gmo.WaitInterval = 15000;
msgsGot = 0;
msgsToGet = 0;
CompCode = OpenCode;
}
while (CompCode != MQCC_FAILED && ((msgsToGet == 0) || (msgsGot < msgsToGet)))
{
/* define length of the buffer -1 */
buflen = strlen(TempBuf) - 1; */ buffer length */
memcpy(md.MsgId, MQMI_NONE, sizeof(md.MsgId)); /*copy msg ID*/
memcpy(md.CorrelId, MQCI_NONE, sizeof(md.CorrelId));/*copy corrlID*/
md.Encoding = MQENC_NATIVE; /*encode*/
md.CodedCharSetId = MQCCSI_Q_MGR;
/* function to get message from MQ*/
MQGET(Hcon, /* get message from MQ */
Hobj, /* object handle*/
&md, /* message descriptor*/
&gmo, /*get message options*/
buflen, /*buffer length*/
TempBuf, /* buffer */
&messlen, /* message length*/
&CompCode, /* completion code*/
&Reason); /* reason code*/
**/* I put some statements to check if transaction failed or not*/**
if (Reason != MQRC_NONE)
{
if (Reason == MQRC_NO_MSG_AVAILABLE)
{
/* print statement no more messages */
else
{
printf("MQGET ended with reason code %d comcode %d\n",Reason,CompCode);
if (Reason == MQRC_TRUNCATED_MSG_FAILED)
{
/print statement that it is failed*/
}
}
}
**This is almost done, only statement if Compcode not failed, then print buffer**
I have changed char TempBuf declaration to MQBYTE and it is not help

MQRC 2037 is MQRC_NOT_OPEN_FOR_INPUT, you can find this information by running the mqrc command provided with the IBM MQ Client or server install, below is a sample output on a Linux server:
$ mqrc 2037
2037 0x000007f5 MQRC_NOT_OPEN_FOR_INPUT
You do not show the MQOPEN call but if it is using the O_options, it would be explained by this, you currently have the following:
O_options = MQOO_OUTPUT + MQOO_FAIL_IF_QUIESCING;
This should someing like the following:
O_options = MQOO_INPUT_AS_Q_DEF + MQOO_FAIL_IF_QUIESCING;
I would suggested that you review the sample applications provided with the IBM MQ install. On Linux these would be located in /opt/mqm/samp. The sample amqsget0.c would be similar to your program except it is using a MQCONN not MQCONNX.

Related

How do I perform port forwarding in C using libssh on Windows

Below is a section of code I wrote with the help of this post here: C: can I forward port for external application with libssh?.
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; /* initialize the mutex */
struct arg_struct
{
int sockfd; /* client socket */
ssh_session session; /* ssh session */
char *rhost; /* remote where we perform forwarding */
int rport; /* ssh port */
int lport; /* local port to listen on */
};
/* forward_handler: handle the port forwarding for every thread */
void *forward_handler(void *arg)
{
char data[4060];
int rc, recvl, nwritten, nread;
ssh_channel forwarding_channel;
struct arg_struct *args = (struct arg_struct*)arg;
pthread_mutex_lock(&lock); /* lock mutex until task complete */
/* requests on the fd can return immediately with a failure status if no input, vs blocking */
if ((fcntl(args->sockfd, F_SETFL, O_NONBLOCK)) != 0)
goto exit;
/* get pointer to a newly allocated channel, NULL on error */
if ((forwarding_channel = ssh_channel_new(args->session)) == NULL) {
perror("Error");
goto exit;
}
/* open a TCP/IP forwarding channel, SSH_OK if successfull */
rc = ssh_channel_open_forward(forwarding_channel,
args->rhost, args->rport,
"127.0.0.1", args->lport);
if (rc != SSH_OK) {
perror("Error");
goto exit;
}
printf("Connected! Tunnel open (127.0.0.1:%d) -> (ssh://%s) -> (\"%s:%d\")\n",
args->lport, args->rhost, args->rhost, args->rport
);
/* while we have an open channel and channel has NOT sent an EOF response */
while(ssh_channel_is_open(forwarding_channel) && !ssh_channel_is_eof(forwarding_channel))
{
if ((recvl = recv(args->sockfd, data, sizeof(data), MSG_DONTWAIT)) < 0) /* recieve data & enable nonblocking */
if ((nread = ssh_channel_read_nonblocking(forwarding_channel, data, sizeof(data), 0)) > 0) /* do a nonblocking read on the channel */
if ((write(args->sockfd, data, nread)) < 0) /* write to socket */
{
perror("Error");
goto exit;
}
else if (!recvl) {
printf("Local client disconnected, exiting\n");
goto exit;
}
nwritten = ssh_channel_write(forwarding_channel, data, recvl); /* send to the remote host */
if (recvl != nwritten) {
perror("Error");
goto exit;
}
}
exit:
ssh_channel_free(forwarding_channel); /* free the forwarding channel */
pthread_mutex_unlock(&lock); /* unlock mutex */
close(args->sockfd); /* close socket file descriptor */
pthread_exit(NULL); /* exit thread */
}
This code has not been optimized and heavily tested and tbh wrote it fairly quickly and need to review it, but it works on Linux as my POC for now. When I try to do the same on Windows I mainly run into an issue on the following lines:
if ((fcntl(args->sockfd, F_SETFL, O_NONBLOCK)) != 0)
goto exit;
---snip---
if ((recvl = recv(args->sockfd, data, sizeof(data), MSG_DONTWAIT)) < 0) /* recieve data & enable nonblocking */
if ((nread = ssh_channel_read_nonblocking(forwarding_channel, data, sizeof(data), 0)) > 0) /* do a nonblocking read on the channel */
if ((write(args->sockfd, data, nread)) < 0)
Below are my attempts to fix this:
/* forward_handler: handle the port forwarding for every thread */
void *forward_handler(void *arg)
{
char data[4096];
unsigned long int iMode = 1;
int rc, recvl, nwritten, nread;
ssh_channel forwarding_channel;
struct arg_struct *args = (struct arg_struct*)arg;
pthread_mutex_lock(&lock); /* lock mutex until task complete */
/* requests on the fd can return immediately with a failure status if no input, vs blocking */
if ((ioctlsocket(args->sockfd, FIONBIO, &iMode)) != NO_ERROR)
goto exit;
/* get pointer to a newly allocated channel, NULL on error */
if ((forwarding_channel = ssh_channel_new(args->session)) == NULL) {
fprintf(stderr, "forward_handler: Failed to create new channel with error: %d\n", forwarding_channel);
exit(0);
}
/* open a TCP/IP forwarding channel, SSH_OK if successfull */
rc = ssh_channel_open_forward(forwarding_channel,
args->rhost, args->rport,
"127.0.0.1", args->lport);
if (rc != SSH_OK) {
fprintf(stderr, "forward_handler: Failed to open forwarding channel with error: %d\n", rc);
goto exit_and_free;
}
printf("Connected! Tunnel open (127.0.0.1:%d) -> (ssh://%s) -> (\"%s:%d\")\n",
args->lport, args->rhost, args->rhost, args->rport
);
/* while we have an open channel and channel has NOT sent an EOF response */
while(ssh_channel_is_open(forwarding_channel) && !ssh_channel_is_eof(forwarding_channel))
{
if ((recvl = recv(args->sockfd, data, sizeof(data), 0)) < 0) {
if ((nread = ssh_channel_read_nonblocking(forwarding_channel, data, sizeof(data), 0)) > 0) /* do a nonblocking read on the channel */
if ((WriteFile((HANDLE)args->sockfd, data, nread, NULL, NULL)) < 0) /* write to socket */
{
fprintf(stderr, "forward_handler: Error writing to file descriptor");
goto exit_and_free;
}
}
else if (!recvl) {
printf("Local client disconnected, exiting\n");
goto exit_and_free;
}
nwritten = ssh_channel_write(forwarding_channel, data, recvl); /* send to the remote host */
if (recvl != nwritten) {
fprintf(stderr, "forward_handler: Error writing to SSH channel\n");
goto exit_and_free;
}
}
exit_and_free:
ssh_channel_free(forwarding_channel); /* free the forwarding channel */
exit:
pthread_mutex_unlock(&lock); /* unlock mutex */
close(args->sockfd); /* close socket file descriptor */
pthread_exit(NULL); /* exit thread */
}
Aight hit me with the roasts on bad code :) because I'm stuck.

MQPUT error aborted

Again a problem with MQPUT
Below is my function
#include <stdio.h>
#include <cmqc.h>
#include <cmqxc.h>
#include "dte_mq.h"
#include <string.h>
#include <stdlib.h>
typedef struct tagDTE_QUEUE_DESCRIPTOR
{
MQHOBJ handle;
int IsSyncpointControled;
} DTE_QUEUE_DESCRIPTOR, *PDTE_QUEUE_DESCRIPTOR;
static MQHCONN sHConn = 0;
static MQLONG sCompCode = MQCC_OK;
static MQLONG sReason = MQRC_NONE;
static int sNumOpenQueues = 0;
static PDTE_QUEUE_DESCRIPTOR sQueues = NULL;
MQLONG OpenCode;
MQOD od = {MQOD_DEFAULT}; /* Object Descriptor */
MQMD md = {MQMD_DEFAULT};
MQPMO pmo = {MQPMO_DEFAULT};
MQLONG O_options;
MQGMO gmo = {MQGMO_DEFAULT};
/* MQCONNX options */
MQCNO Connect_options = {MQCNO_DEFAULT};
/* Client connection channel */
MQCD ClientConn = {MQCD_CLIENT_CONN_DEFAULT};
#define MAX_NUM_OPEN_QUEUES 10
DTE_MQ_RESULT dteMqSend(int qd, void *buf, int len)
{
printf("oleg\n");
/* memcpy(md.Format, MQFMT_STRING, MQ_FORMAT_LENGTH); */
md.MsgType = MQMT_DATAGRAM;
printf("oleg1\n");
memcpy(md.MsgId, MQMI_NONE, sizeof(md.MsgId));
printf("oleg2\n");
memcpy(md.CorrelId, MQCI_NONE, sizeof(md.CorrelId));
printf("oleg3\n");
memcpy(md.Format, MQFMT_STRING, (size_t)MQ_FORMAT_LENGTH);
printf("oleg4\n");
printf("QD is = %d\n",qd);
if(sQueues[qd].IsSyncpointControled)
pmo.Options |= MQPMO_SYNCPOINT;
printf("oleg5\n");
MQPUT(sHConn, sQueues[qd].handle, &md, &pmo, len, buf, &sCompCode, &sReason);
printf("MQput CC=%ld RC=%ld\n", sCompCode, sReason);
if (sCompCode != MQCC_OK) return DTE_MQR_FAILED;
return DTE_MQR_OK;
}
Below is a program with function call:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cmqc.h> /* includes for MQI */
#include <cmqxc.h>
int main(int argc, char **argv)
{
MQLONG messlen; /* message length received */
char QMgrName[MQ_Q_MGR_NAME_LENGTH+1];
char QName[MQ_Q_NAME_LENGTH+1];
char channelName[MQ_CHANNEL_NAME_LENGTH+1];
char hostname[1024];
char port[4];
MQLONG buflen;
/* MQBYTE TmpBuf[65536] = "This is a simple test message."; */
MQBYTE TmpBuf[1024] = "This is a simple test message.";
int msgsToGet;
int msgsGot;
int dteretinit;
int dteretdeinit;
int dteretopen;
int dteretclose;
int qd;
int dteretput;
dteretinit = dteMqInit(QMgrName,hostname,channelName);
printf("Return code from dteMqInit = %d\n",dteretinit);
if (dteretinit == 0)
{
printf("Connection ro MQ failed, check the error log\n");
exit(99);
}
qd = -1;
dteretopen = dteMqOpen(QName, &qd);
printf ("Return code from dteMqOpen = %d\n",dteretopen);
if (dteretopen ==0)
{
printf("MQOPEN could not open MQ, check errpr log\n");
exit(99);
}
buflen = strlen(TmpBuf);
TmpBuf[buflen + 1] = '\0';
dteretput = dteMqSend(qd,*TmpBuf,buflen);
printf("return mqput %d\n",dteretput);
if (dteretput == 0)
{
printf("Could not put the message to MQ, check error log\n");
exit(99);
}
dteretclose = dteMqClose(qd);
printf("Return code from dteMqClose = %d\n",dteretclose);
if (dteretclose == 0)
{
printf("Could not close MQ, check error log\n");
exit(99);
}
dteretdeinit = dteMqDeinit();
printf("Return code from dteMqDeinit = %d\n",dteretdeinit);
if (dteretdeinit == 0)
{
printf("Could not disconnect from MQ, check error log\n");
exit(99);
}
}
The program started running, connected to MQ, open MQ, then go to send message, print all including "oleg5", and aborted
This is output:
Using values:
QMgrName : QM.SU00005
QName : AP.TR.FROM.ADS
ChannelName: AVNCHCTM.CLIENT
hostname : enbmqu02.uk.db.com
port : 1415
CC = 0, RS = 0
Return code from dteMqInit = 1
SAM
SAM2
SAM3
MQopen = 0 and 0
In the loop1
QDESC1 = -990964260
QDESC = -990964260
Return code from dteMqOpen = 1
oleg
oleg1
oleg2
oleg3
oleg4
QD is = 0
oleg5
Aborted
as you can see qd=0 and in this place before MQPUT in if statement it didn't come. May be I have to make else statement and assign Options?
if(sQueues[qd].IsSyncpointControled)
{
pmo.Options |= MQPMO_SYNCPOINT;
printf("Synchronized\n");
}
This happened exactly on MQPUT call
Could you please help me?

C program with MQ get a message MQGET ended with reason code 2005

I made and run the C program, which connect to MQ and try to get a message. I always get a message:
MQGET ended with reason code 2005
this means:
MQRC_BUFFER_LENGTH_ERROR (2005, X'7D5') Buffer length parameter not valid
I declared the buffer as
char TempBuf[65536];
The message in MQ is
"This is a test message"
in MQ error log nothing is written.
Below is the program:
**/* header files */**
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cmqc.h> /* includes for MQI*/
#include <cmqxc.h>
int main(int argc, char **argv)
{
MQCNO Connect_options = {MQCNO_DEFAULT};/MQNONNX opt*/
MQCD ClientConn = {MQCD_CLIENT_CONN_DEFAULT};/*client channel*/
MQHCONN Hcon; /* connection handle */
MQHOBJ Hobj; /* object handle */
MQLONG CompCode; /* completion code */
MQLONG OpenCode; /* MQOPEN completion code*/
MQLONG Reason; /* reason code */
MQOD od = {MQOD_DEFAULT}; /* Object Descriptor */
MQMD md = {MQMD_DEFAULT}; /* Message Descriptor */
MQPMO pmo = {MQPMO_DEFAULT}; /* put message options*/
MQLONG O_options; /* MQOPEN options */
MQLONG C_options; /* MQCLOSE options */
MQGMO gmo = {MQGMO_DEFAULT}; /* get message options */
char QMgrName[MQ_Q_MGR_NAME_LENGTH+1];
char QName[MQ_Q_NAME_LENGTH+1];
char channelName[MQ_CHANNEL_NAME_LENGTH+1];
char hostname[1024];
char port[4];
MQLONG buflen; /* buffer length*/
char TempBuf[65536];
int msgsToGet;
int msgsGot;
if (argc != 6)
{
printf("Usage: MQTest11 QMgrName ChlName hostname port QName\n");
return(1);
}
**/* copy MQ manager name */**
strncpy(QMgrName, argv[1], MQ_Q_MGR_NAME_LENGTH);
QMgrName[MQ_Q_MGR_NAME_LENGTH] = '\0';
**/* copy channel name */**
strncpy(channelName, argv[2], MQ_CHANNEL_NAME_LENGTH);
channelName[MQ_CHANNEL_NAME_LENGTH] = '\0';
/* copy hostname */
strncpy(hostname, argv[3], 1023);
hostname[1023] = '\0';
/* copy port number */
strncpy(port,argv[4],4);
strncpy(QName, argv[5], MQ_Q_NAME_LENGTH);
QName[MQ_Q_NAME_LENGTH] = '\0';
/* copy hostname for connection */
strncpy(ClientConn.ConnectionName,hostname, MQ_CONN_NAME_LENGTH);
/* copy channel name */
strncpy(ClientConn.ChannelName,channelName,MQ_CHANNEL_NAME_LENGTH);
/* Point the MQCNO to the client connection definition */
Connect_options.ClientConnPtr = &ClientConn;
Connect_options.Version = MQCNO_VERSION_2;
/* use MQCONNX */
if (CompCode == MQCC_FAILED)
{
/* exit with print the reason */
}
else
{
strncpy(od.ObjectName, QName, (size_t)MQ_Q_NAME_LENGTH);
O_options = MQOO_OUTPUT + MQOO_FAIL_IF_QUIESCING;
/* use MQOPEN */
if (OpenCode == MQCC_OK) /* if MQOPEN , then continue in the while loop */
{
gmo.Options = MQGMO_WAIT + MQGMO_CONVERT;
gmo.WaitInterval = 15000;
msgsGot = 0;
msgsToGet = 0;
}
while (CompCode != MQCC_FAILED && ((msgsToGet == 0) || (msgsGot < msgsToGet)))
{
/* define length of the buffer -1 */
buflen = strlen(TempBuf) - 1; */ buffer length */
memcpy(md.MsgId, MQMI_NONE, sizeof(md.MsgId)); /*copy msg ID*/
memcpy(md.CorrelId, MQCI_NONE, sizeof(md.CorrelId));/*copy corrlID*/
md.Encoding = MQENC_NATIVE; /*encode*/
md.CodedCharSetId = MQCCSI_Q_MGR;
/* function to get message from MQ*/
MQGET(Hcon, /* get message from MQ */
Hobj, /* object handle*/
&md, /* message descriptor*/
&gmo, /*get message options*/
buflen, /*buffer length*/
TempBuf, /* buffer */
&messlen, /* message length*/
&CompCode, /* completion code*/
&Reason); /* reason code*/
**/* I put some statements to check if transaction failed or not*/**
if (Reason != MQRC_NONE)
{
if (Reason == MQRC_NO_MSG_AVAILABLE)
{
/* print statement no more messages */
else
{
printf("MQGET ended with reason code %d comcode %d\n",Reason,CompCode);
if (Reason == MQRC_TRUNCATED_MSG_FAILED)
{
/print statement that it is failed*/
}
}
}
**/* This is almost done, only statement if Compcode not failed, then print buffer */**
The buffer is not printed and error message you can see above.
I appreciate any help.
Thanks
The buffer should be of type MQBYTE not char:
Change: char TempBuf[65536];
To: MQBYTE TempBuf[65536];
To get the buffer length you should use sizeof not strlen.
Change: buflen = strlen(TempBuf) - 1;
To: buflen = sizeof(TempBuf)-1;

Adding Argument to a char device

I am writing a character device that will be given a processes pid and return the parent process id, start time, and number of siblings. I am using this site: http://tldp.org/LDP/lkmpg/2.6/html/x892.html#AEN972 as a guide.
I would like to add an argument into the read call of the character driver but it is not letting me do that. My code looks like this:
/*
* chardev.c - Create an input/output character device
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#include <linux/fs.h>
#include <asm/uaccess.h> /* for get_user and put_user */
#include "chardev.h"
#define SUCCESS 0
#define DEVICE_NAME "char_dev"
#define BUF_LEN 80
struct procinfo {
pid_t pid;
pid_t ppid;
struct timespec start_time;
int num_sib;
};
/*
* Is the device open right now? Used to prevent
* concurent access into the same device
*/
static int Device_Open = 0;
/*
* The message the device will give when asked
*/
static char Message[BUF_LEN];
/*
* How far did the process reading the message get?
* Useful if the message is larger than the size of the
* buffer we get to fill in device_read.
*/
static char *Message_Ptr;
/*
* This is called whenever a process attempts to open the device file
*/
static int device_open(struct inode *inode, struct file *file)
{
#ifdef DEBUG
printk(KERN_INFO "device_open(%p)\n", file);
#endif
/*
* We don't want to talk to two processes at the same time
*/
if (Device_Open)
return -EBUSY;
Device_Open++;
/*
* Initialize the message
*/
Message_Ptr = Message;
try_module_get(THIS_MODULE);
return SUCCESS;
}
static int device_release(struct inode *inode, struct file *file)
{
#ifdef DEBUG
printk(KERN_INFO "device_release(%p,%p)\n", inode, file);
#endif
/*
* We're now ready for our next caller
*/
Device_Open--;
module_put(THIS_MODULE);
return SUCCESS;
}
/*
* This function is called whenever a process which has already opened the
* device file attempts to read from it.
*/
static ssize_t device_read(struct file *file, /* see include/linux/fs.h */
char __user * buffer, /* buffer to be
* filled with data */
pid_t pid,
size_t length, /* length of the buffer */
loff_t * offset)
{
/*
* Number of bytes actually written to the buffer
*/
int bytes_read = 0;
#ifdef DEBUG
printk(KERN_INFO "device_read(%p,%p,%d)\n", file, buffer, length);
#endif
/*
* If we're at the end of the message, return 0
* (which signifies end of file)
*/
if (*Message_Ptr == 0)
return 0;
/*
* Actually put the data into the buffer
*/
while (length && *Message_Ptr) {
/*
* Because the buffer is in the user data segment,
* not the kernel data segment, assignment wouldn't
* work. Instead, we have to use put_user which
* copies data from the kernel data segment to the
* user data segment.
*/
put_user(*(Message_Ptr++), buffer++);
length--;
bytes_read++;
}
#ifdef DEBUG
printk(KERN_INFO "Read %d bytes, %d left\n", bytes_read, length);
#endif
/*
* Read functions are supposed to return the number
* of bytes actually inserted into the buffer
*/
return bytes_read;
}
/*
* This function is called when somebody tries to
* write into our device file.
*/
static ssize_t
device_write(struct file *file,
const char __user * buffer, size_t length, loff_t * offset)
{
int i;
#ifdef DEBUG
printk(KERN_INFO "device_write(%p,%s,%d)", file, buffer, length);
#endif
for (i = 0; i < length && i < BUF_LEN; i++)
get_user(Message[i], buffer + i);
Message_Ptr = Message;
/*
* Again, return the number of input characters used
*/
return i;
}
/*
* This function is called whenever a process tries to do an ioctl on our
* device file. We get two extra parameters (additional to the inode and file
* structures, which all device functions get): the number of the ioctl called
* and the parameter given to the ioctl function.
*
* If the ioctl is write or read/write (meaning output is returned to the
* calling process), the ioctl call returns the output of this function.
*
*/
long device_ioctl(struct file *file, /* see include/linux/fs.h */
unsigned int ioctl_num, /* number and param for ioctl */
unsigned long ioctl_param)
{
int i;
char *temp;
char ch;
/*
* Switch according to the ioctl called
*/
switch (ioctl_num) {
case IOCTL_SET_MSG:
/*
* Receive a pointer to a message (in user space) and set that
* to be the device's message. Get the parameter given to
* ioctl by the process.
*/
temp = (char *)ioctl_param;
/*
* Find the length of the message
*/
get_user(ch, temp);
for (i = 0; ch && i < BUF_LEN; i++, temp++)
get_user(ch, temp);
device_write(file, (char *)ioctl_param, i, 0);
break;
case IOCTL_GET_MSG:
/*
* Give the current message to the calling process -
* the parameter we got is a pointer, fill it.
*/
i = device_read(file, (char *)ioctl_param, 99, 0);
/*
* Put a zero at the end of the buffer, so it will be
* properly terminated
*/
put_user('\0', (char *)ioctl_param + i);
break;
case IOCTL_GET_NTH_BYTE:
/*
* This ioctl is both input (ioctl_param) and
* output (the return value of this function)
*/
return Message[ioctl_param];
break;
}
return SUCCESS;
}
/* Module Declarations */
/*
* This structure will hold the functions to be called
* when a process does something to the device we
* created. Since a pointer to this structure is kept in
* the devices table, it can't be local to
* init_module. NULL is for unimplemented functions.
*/
struct file_operations Fops = {
.read = device_read,
.write = device_write,
.unlocked_ioctl = device_ioctl,
.open = device_open,
.release = device_release, /* a.k.a. close */
};
/*
* Initialize the module - Register the character device
*/
int init_module()
{
int ret_val;
/*
* Register the character device (atleast try)
*/
ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &Fops);
/*
* Negative values signify an error
*/
if (ret_val < 0) {
printk(KERN_ALERT "%s failed with %d\n",
"Sorry, registering the character device ", ret_val);
return ret_val;
}
printk(KERN_INFO "%s The major device number is %d.\n",
"Registeration is a success", MAJOR_NUM);
printk(KERN_INFO "If you want to talk to the device driver,\n");
printk(KERN_INFO "you'll have to create a device file. \n");
printk(KERN_INFO "We suggest you use:\n");
printk(KERN_INFO "mknod %s c %d 0\n", DEVICE_FILE_NAME, MAJOR_NUM);
printk(KERN_INFO "The device file name is important, because\n");
printk(KERN_INFO "the ioctl program assumes that's the\n");
printk(KERN_INFO "file you'll use.\n");
return 0;
}
/*
* Cleanup - unregister the appropriate file from /proc
*/
void cleanup_module()
{
unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
#if 0
int ret;
/*
* Unregister the device
*/
ret = unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
/*
* If there's an error, report it
*/
if (ret < 0)
printk(KERN_ALERT "Error: unregister_chrdev: %d\n", ret);
#endif
}

Not able to connect to MQ Server from MQ Client

In c, I tried to connect to the MQ server from the same Unix server where MQ server is installed and it is getting connected and putting message into the queue but the same thing if I am trying from MQ Client (from another development server) it is throwing error message:
MQCONN ended with reason code 2195.
Following is sample:
// Libraries included
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cmqc.h>
#include <cmqxc.h>
Following are the variables which i declared and defined to connecing to MQ Server:
// Variable declarations.
MQCNO Connect_options = {MQCNO_DEFAULT}; /* MQCONNX options */
MQCD ClientConn = {MQCD_CLIENT_CONN_DEFAULT}; /* Client connection channel */
MQHCONN Hcon; /* connection handle */
MQHOBJ Hobj; /* object handle */
MQLONG CompCode; /* completion code */
MQLONG Reason; /* reason code */
MQOD od = {MQOD_DEFAULT}; /* Object Descriptor */
MQMD md = {MQMD_DEFAULT}; /* Message Descriptor */
MQPMO pmo = {MQPMO_DEFAULT}; /* put message options */
MQLONG O_options; /* MQOPEN options */
MQLONG C_options; /* MQCLOSE options */
char QMgrName[MQ_Q_MGR_NAME_LENGTH+1];
char QName[MQ_Q_NAME_LENGTH+1];
char channelName[MQ_CHANNEL_NAME_LENGTH+1];
char hostname[1024];
MQLONG buflen; /* buffer length */
char TempBuf[1024] = "MQPutdata:This is a simple test message.";
Following are the other variables defined to call MQCONNX function:
printf("MQPutdata started\n");
strncpy(QMgrName, argv[1], MQ_Q_MGR_NAME_LENGTH);
QMgrName[MQ_Q_MGR_NAME_LENGTH] = '\0';
strncpy(channelName, argv[2], MQ_CHANNEL_NAME_LENGTH);
channelName[MQ_CHANNEL_NAME_LENGTH] = '\0';
strncpy(hostname, argv[3], 1023);
hostname[1023] = '\0';
strncpy(QName, argv[4], MQ_Q_NAME_LENGTH);
QMgrName[MQ_Q_NAME_LENGTH] = '\0';
Here I am setting required values for MQCONNX function
strncpy(ClientConn.ConnectionName,
hostname,
MQ_CONN_NAME_LENGTH);
strncpy(ClientConn.ChannelName,
channelName,
MQ_CHANNEL_NAME_LENGTH);
Now I am calling the MQCONNX function in this way:
Connect_options.ClientConnPtr = &ClientConn;
Connect_options.Version = MQCNO_VERSION_2;
MQCONNX(QMgrName,
&Connect_options,
&Hcon,
&CompCode,
&Reason);
I compiled with the following optpions but nothing is working:
1. cc -H -I/export/home/aupadhya/mqm/inc -L/opt/mqm/lib64 -o MQPutdata.exe MQPutdata.c -lmqic
2. cc -H -I/export/home/aupadhya/mqm/inc -L/opt/mqm/lib64 -o MQPutdata.exe MQPutdata.c -lmqic -lmqmcs -lsocket
3. cc -H -I/export/home/aupadhya/mqm/inc -L/opt/mqm/lib64 -o MQPutdata.exe MQPutdata.c -lmqic -lmqmcs -lmqmzse -lsocket

Resources