Segmentation fault error in C program using MQPUT - c

I have a program below
#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;
MQLONG C_options;
MQGMO gmo = {MQGMO_DEFAULT};
/* MQCONNX options */
MQCNO Connect_options = {MQCNO_DEFAULT};
/* Client connection channel */
MQCD ClientConn = {MQCD_CLIENT_CONN_DEFAULT};
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");
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;
}
I put print statement and found out that after printf("oleg4\n";) I get error
Segmentation fault
Could you please help me top debug the program? Do I have to use malloc for the structure? It is written in C and using MQPUT
Below is a program, where above function is called
#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.";
int msgsToGet;
int msgsGot;
int dteretinit;
int dteretdeinit;
int dteretopen;
int dteretclose;
int qd;
int dteretput;
if (argc != 6)
{
printf("Usage: NewMQTest QMgrName ChlName hostname port QName\n");
exit(-1);
}
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(port,argv[4],4);
strncpy(QName, argv[5], MQ_Q_NAME_LENGTH);
QName[MQ_Q_NAME_LENGTH] = '\0';
dteretinit = dteMqInit(QMgrName,hostname,channelName);
printf("Return code from dteMqInit = %d\n",dteretinit);
qd = -1;
dteretopen = dteMqOpen(QName, qd);
printf ("Return code from dteMqOpen = %d\n",dteretopen);
if (dteretopen == 0 )
{
buflen = strlen(TmpBuf);
TmpBuf[buflen + 1] = '\0';
dteretput = dteMqSend(qd,*TmpBuf,buflen);
printf("return mqput %d\n",dteretput);
}
dteretclose = dteMqClose(qd);
printf("Return code from dteMqClose = %d\n",dteretclose);
dteretdeinit = dteMqDeinit();
printf("Return code from dteMqDeinit = %d\n",dteretdeinit);
}

I analyzed my program and check MQ. SSL didn't let me connect to MQ, that is why I couldn't change qd. The program is very old, and when failed, return value was set up as 0, 1 is success. I never did it like that. 0 is always success.
Thanks

This is some bad code.
typedef struct tagDTE_QUEUE_DESCRIPTOR
{
MQHOBJ handle;
int IsSyncpointControled;
} DTE_QUEUE_DESCRIPTOR, *PDTE_QUEUE_DESCRIPTOR;
static PDTE_QUEUE_DESCRIPTOR sQueues = NULL;
if(sQueues[qd].IsSyncpointControled)
In the code that you have posted, 'sQueues' is never set, so that is why it is throwing segmentation fault on the 'if' statement. And if 'qd' is really '-1' as per your comment, then I have to wonder what in the world you are doing.
dteretinit = dteMqInit(QMgrName,hostname,channelName);
How come you don't pass the port number into the dteMqInit subroutine?

Related

MQOPEN gives error 2085, which I didn't have before

I have everything was working OK, until today I on MQOPEN got error
2085 MQRC_UNKNOWN_OBJECT_NAME
#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;/* MQCONNX options */
MQCNO Connect_options = {MQCNO_DEFAULT};
/* Client connection channel */
MQCD ClientConn = {MQCD_CLIENT_CONN_DEFAULT};
#define MAX_NUM_OPEN_QUEUES 10
int dteMqOpen(const char *name, int *qd)
{
MQLONG options;
MQHOBJ hObj;
int i;
printf("SAM\n");
strncpy(od.ObjectName, name, MQ_Q_NAME_LENGTH);
printf("SAM2\n");
O_options = MQOO_INPUT_AS_Q_DEF + MQOO_FAIL_IF_QUIESCING;
printf("SAM3\n");
MQOPEN(sHConn, &od, O_options, &hObj, &sCompCode, &sReason);
printf("MQopen = %d and %d\n",sCompCode,sReason);
if (sCompCode != MQCC_OK)
{
printf("RETURN %d\n",DTE_MQR_FAILED);
return DTE_MQR_FAILED;
}
++sNumOpenQueues;
*qd = 1;
for(i = 0; i < MAX_NUM_OPEN_QUEUES; i++)
{
printf("In the loop1\n");
if(sQueues[i].handle == -1)
{
*qd = i;
printf("QDESC1 = %d\n",qd);
sQueues[i].handle = hObj;
sQueues[i].IsSyncpointControled = 0;
break;
}
printf("In the loop\n");
}
printf("QDESC = %d\n",qd);
return DTE_MQR_OK;
}
Function call is:
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);
}
Error 2085. But several days before there was no such error
Connection is OK, but MQOPEN failed
dteretopen = dteMqOpen(QName, &qd);
Clearly 'QName' has an invalid value.
You have been posting question after question about the same program. Obviously, you have zero training in MQ programming. You need to get some MQ training ASAP. There is lots and lots of information on the web and videos too.
Why aren't you doing some basic debugging and outputting 'QName?
You need to take some initiative in debugging your program before posting questions here. We are not here to do your work.

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?

Char array corrupted after passing through ONC RPC

when calling a ONC-RPC-function that returns a char array I get (apparently) uninitialized memory on the first call. Subsequent calls give me the correct result.
This example showcases the issue:
ftp.x:
const BLOCKSIZE = 1024;
struct block {
char arr<BLOCKSIZE>;
};
program FILE_GET_BLOCK_PROG {
version FILE_GET_BLOCK_VERS {
block FILE_GET_BLOCK() = 1;
} = 1;
} = 0x42424243;
ftp_client.c:
#include "ftp.h"
#include <errno.h>
#include <string.h>
size_t
file_get_block_prog_1(char *host, char *buf)
{
CLIENT *clnt;
block *result_1;
#ifndef DEBUG
clnt = clnt_create (host, FILE_GET_BLOCK_PROG, FILE_GET_BLOCK_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
result_1 = file_get_block_1(NULL, clnt);
if (result_1 == (block *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
clnt_destroy (clnt);
#endif /* DEBUG */
memcpy(buf, result_1->arr.arr_val, result_1->arr.arr_len);
return result_1->arr.arr_len;
}
int
main (int argc, char *argv[])
{
char *host, *source_filename;
char buf[BLOCKSIZE+1];
int block_count;
size_t bytes_read;
if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
bytes_read = file_get_block_prog_1 (host, buf);
buf[bytes_read] = '\0';
printf("%d bytes:\n%s\n", bytes_read, buf);
exit (0);
}
ftp_server.c:
#include "ftp.h"
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
block *
file_get_block_1_svc(void *argp, struct svc_req *rqstp)
{
static block result;
static int request = 1;
char buf[BLOCKSIZE+1];
size_t bytes_read;
strcpy(buf, "This is just a simple test block. There is nothing relevant in here.");
result.arr.arr_len = strlen(buf);
result.arr.arr_val = buf;
printf("Request #%d:\n%s\n", request++, buf);
return &result;
}
When running the server and calling the client twice, this is my output:
client:
$ ./ftp_client localhost
68 bytes:
$ ./ftp_client localhost
68 bytes:
This is just a simple test bock. There is nothing relevant in here.
server:
Request #1:
This is just a simple test bock. There is nothing relevant in here.
Request #2:
This is just a simple test bock. There is nothing relevant in here.
Is there any initialization I missed that I need to do before the first request? What else would cause this behavior?

dbus c program - send(with reply) and receive using method_call

I'm new to D-Bus. I want a c program to send and receive data using the dbus_message_new_method_call function. I have tried the following programs from the link How to reply a D-Bus message but I'm getting error in the server.c side like "the name client.signal.Object was not provided by any .service files"
"server.c"
/* server.c */
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static DBusHandlerResult
filter_func(DBusConnection *connection, DBusMessage *message, void *usr_data)
{
DBusMessage *reply;
dbus_bool_t handled = false;
char *word = NULL;
DBusError dberr;
dbus_error_init(&dberr);
dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID);
printf("receive message: %s\n", word);
handled = true;
reply = dbus_message_new_method_return(message);
char * reply_content;
printf("\nEnter your Reply Msg : ");
scanf("%s",reply_content);
dbus_message_append_args(reply, DBUS_TYPE_STRING, &reply_content, DBUS_TYPE_INVALID);
dbus_connection_send(connection, reply, NULL);
dbus_connection_flush(connection);
dbus_message_unref(reply);
return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}
int main(int argc, char *argv[])
{
DBusError dberr;
DBusConnection *dbconn;
dbus_error_init(&dberr);
dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL))
{
return -1;
}
dbus_bus_add_match(dbconn, "type='method_call',interface='client.signal.Type'", &dberr);
while(dbus_connection_read_write_dispatch(dbconn, -1))
{
/* loop */
}
return 0;
}
here client.c
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static DBusHandlerResult
filter_func(DBusConnection *connection, DBusMessage *message, void *usr_data)
{
dbus_bool_t handled = false;
char *word = NULL;
DBusError dberr;
dbus_error_init(&dberr);
dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID);
printf("receive message %s\n", word);
handled = true;
return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}
int db_send(DBusConnection *dbconn)
{
DBusMessage *dbmsg;
char *word = (char *)malloc(sizeof(char));
int i;
dbmsg = dbus_message_new_method_call("client.signal.Object","/client/signal/Object", "client.signal.Type", "Test");
scanf("%s", word);
if (!dbus_message_append_args(dbmsg, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID))
{
return -1;
}
if (!dbus_connection_send(dbconn, dbmsg, NULL))
{
return -1;
}
dbus_connection_flush(dbconn);
printf("send message %s\n", word);
dbus_message_unref(dbmsg);
return 0;
}
int main(int argc, char *argv[])
{
DBusError dberr;
DBusConnection *dbconn;
dbus_error_init(&dberr);
dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL))
{
return -1;
}
db_send(dbconn);
while(dbus_connection_read_write_dispatch(dbconn, -1))
{
db_send(dbconn);
}
dbus_connection_unref(dbconn);
return 0;
}
Please help me to fix.
Have a look here: http://www.freedesktop.org/wiki/IntroductionToDBus/
From what it looks like you need to write a file "*.service" that describes the service.
(Quoted from the website)
# (Lines starting with hash marks are comments)
# Fixed section header (do not change):
[D-BUS Service]
Names=com.bigmoneybank.Deposits;com.bigmoneybank.Withdrawals
Exec=/usr/local/bin/bankcounter
This is what I found within 5 minutes of Googleing.

How to List Active Ports and Processes using them in Linux, C Code

I am trying to write a C Code to do the same Job as:
netstat -vatp
List all Remote/Local Addresses and Processes using them. But I dunno which files should I be reading?
I tried looking into /proc/net/tcp and /proc/net/udp, but they don't have the process name or process identifier like netstat displays it!
Thanks.
You could check the source code http://freecode.com/projects/net-tools. Just download, unpack the bz2 file and you'll find the netstat.c source code
Quick analyse:
/proc/net/tcp for example has an inode tab, in /proc there is a subfolder for each of these inodes, which contains the information you need.
Some more analysing:
I think it's even worse. netstat just loops through the /proc directory and checks the contents of the numeric sub-directories to find the actual process matching the inode. Not sure as I'm just analysing
http://linux.die.net/man/5/proc is very nice reading :)
For your answer, see How can i match each /proc/net/tcp entry to each opened socket?
You could call the netstat application from within your code. Have a look at execve to capture stdout and stderr.
EDIT:
Since code says more than words:
IEFTask.h
#ifndef IEFTASK_H
#define IEFTASK_H
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <signal.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* MARK: Structure */
struct IEFTask {
const char **arguments; /* last argument should be NULL */
int standardInput;
void *callbackArgument;
void (*callback)(int term, char *out, size_t outLen,
char *err, size_t errLen, void *arg);
};
typedef struct IEFTask IEFTask;
/* MARK: Running */
int
IEFTaskRun(IEFTask *theTask);
#endif /* IEFTASK_H */
IEFTask.c
#include "IEFTask.h"
/* MARK: DECLARATION: Data Conversion */
char *
IEFTaskCreateBufferFromPipe(int fd, size_t *bufLen);
/* MARK: Running */
int
IEFTaskRun(IEFTask *myTask) {
pid_t pid;
int exitStatus, status;
int outPipe[2], errPipe[2];
assert(myTask != NULL);
/* Create stdout and stderr pipes */
{
status = pipe(outPipe);
if(status != 0) {
return -1;
}
status = pipe(errPipe);
if(status != 0) {
close(errPipe[0]);
close(errPipe[1]);
return -1;
}
}
/* Fork the process and wait pid */
{
pid = fork();
if(pid < 0) { /* error */
return -1;
} else if(pid > 0) { /* parent */
waitpid(pid, &exitStatus, 0);
exitStatus = WEXITSTATUS(exitStatus);
} else { /* child */
/* close unneeded pipes */
close(outPipe[0]);
close(errPipe[0]);
/* redirect stdout, stdin, stderr */
if(myTask->standardInput >= 0) {
close(STDIN_FILENO);
dup2(myTask->standardInput, STDIN_FILENO);
close(myTask->standardInput);
}
close(STDOUT_FILENO);
dup2(outPipe[1], STDOUT_FILENO);
close(outPipe[1]);
close(STDERR_FILENO);
dup2(errPipe[1], STDERR_FILENO);
close(errPipe[1]);
execve(myTask->arguments[0],
(char *const *)myTask->arguments, NULL);
exit(127);
}
}
/* Parent continues */
{
char *output, *error;
size_t outLen, errLen;
/* 127 = execve failed */
if(exitStatus == 127) {
close(errPipe[0]);
close(errPipe[1]);
close(outPipe[0]);
close(outPipe[1]);
return -1;
}
/* Read in data */
close(errPipe[1]);
close(outPipe[1]);
output = IEFTaskCreateBufferFromPipe(outPipe[0], &outLen);
error = IEFTaskCreateBufferFromPipe(errPipe[0], &errLen);
close(errPipe[0]);
close(outPipe[0]);
/* Call callback */
(*myTask->callback)(exitStatus,
output, outLen,
error, errLen, myTask->callbackArgument);
if(output) free(output);
if(error) free(error);
}
return 0;
}
/* MARK: Data Conversion */
#define READ_BUF_SIZE (128)
char *
IEFTaskCreateBufferFromPipe(int fd, size_t *bufLen) {
ssize_t totalRead = 0, nowRead;
char readBuffer[READ_BUF_SIZE], *myBuffer = NULL;
char *ptr;
while(1) {
nowRead = read(fd, readBuffer, READ_BUF_SIZE);
if(nowRead == -1) {
free(myBuffer);
return NULL;
} else if(nowRead == 0) {
break;
} else {
ptr = realloc(myBuffer, totalRead + nowRead);
if(ptr == NULL) {
free(myBuffer);
return NULL;
}
myBuffer = ptr;
memcpy(&(myBuffer[totalRead]), readBuffer, nowRead);
totalRead += nowRead;
}
}
if(bufLen) *bufLen = (size_t)totalRead;
return myBuffer;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "IEFTask.h"
void taskCallback(int term,
char *out, size_t outlen,
char *err, size_t errlen)
{
char *ptr;
printf("Task terminated: %d\n", term);
ptr = malloc(outlen + 1);
memcpy(ptr, out, outlen);
ptr[outlen] = '\0';
printf("***STDOUT:\n%s\n***END\n", ptr);
free(ptr);
ptr = malloc(errlen + 1);
memcpy(ptr, err, errlen);
ptr[errlen] = '\0';
printf("***STDERR:\n%s\n***END\n", ptr);
free(ptr);
}
int main() {
const char *arguments[] = {
"/bin/echo",
"Hello",
"World",
NULL
};
IEFTask myTask;
myTask.arguments = arguments;
myTask.standardInput = -1;
myTask.callback = &taskCallback;
int status;
status = IEFTaskRun(&myTask);
if(status != 0) {
printf("Failed: %s\n", strerror(errno));
}
return 0;
}

Resources