Skip to content

Instantly share code, notes, and snippets.

@razvancrainea
Created September 15, 2015 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save razvancrainea/0a7ed5731ebda4df17f3 to your computer and use it in GitHub Desktop.
Save razvancrainea/0a7ed5731ebda4df17f3 to your computer and use it in GitHub Desktop.
mini opensipsctl C
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#define DEFAULT_COMMAND "ps"
#define OSIPS_FIFO "/tmp/opensips_fifo"
#define REPLY_FIFO_NAME "opensips-reply-fifo"
#define REPLY_FIFO "/tmp/" REPLY_FIFO_NAME
#define BUF_SIZE 1024
int main(int argc, unsigned char **argv)
{
static unsigned char buf[BUF_SIZE];
unsigned char *p, *command;
int osips_fifo, reply_fifo;
int len;
if (argc < 2)
command = DEFAULT_COMMAND;
else
command = argv[1];
/* open reply fifo */
osips_fifo = open(OSIPS_FIFO, O_RDWR);
if (osips_fifo < 0) {
fprintf(stderr, "Cannot open fifo %s\n", OSIPS_FIFO);
exit(-1);
}
unlink(REPLY_FIFO);
/* create the fifo */
reply_fifo = mkfifo(REPLY_FIFO, 0777);
if (reply_fifo < 0) {
fprintf(stderr, "Cannot create reply fifo %s\n", REPLY_FIFO);
exit(-1);
}
reply_fifo = open(REPLY_FIFO, O_RDWR);
if (reply_fifo < 0) {
fprintf(stderr, "Cannot open reply fifo %s\n", REPLY_FIFO);
exit(-1);
}
/* build the command */
p = buf;
*p++ = ':';
memcpy(p, command, strlen(command));
p += strlen(command);
*p++ = ':';
memcpy(p, REPLY_FIFO_NAME, sizeof(REPLY_FIFO_NAME) - 1);
p+= sizeof(REPLY_FIFO_NAME) - 1;
*p++ = '\n';
*p++ = '\n';
/* print the command */
printf("Command is [%.*s]\n", p - buf, buf);
if (write(osips_fifo, buf, p - buf) < 0) {
fprintf(stderr, "Cannot write to fifo\n");
exit(-1);
}
len = read(reply_fifo, buf, BUF_SIZE);
if (len < 0) {
fprintf(stderr, "Cannot read from reply fifo\n");
exit(-1);
}
printf("Response is [%.*s]\n", len, buf);
close(osips_fifo);
close(reply_fifo);
unlink(REPLY_FIFO);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment