Example #1
0
void 
childloop(mqd_t mq)
{
	char		c;
	register char	*cptr = &c;

	for ( ;; ) {
		if (mq_receive(mq, buf, sizeof(buf), NULL) == -1 ||
			mq_send(mq, cptr, sizeof(c), 0) == -1) {
			    perror("(w) receive/send on mq");
                mq_close(mq);
                exit(1);
		}
	}
}
Example #2
0
// handlers
void client_handler(union sigval sv){
  struct mq_attr attributes;
  char *buf;
  mqd_t mqdes = *((mqd_t *)sv.sival_ptr);
  if(mq_getattr(mqdes, &attributes)==-1){
    handle_error("mq_getattr");
  }

  while(1){
    buf = malloc(attributes.mq_msgsize+1);
    mq_receive(mqdes,buf,attributes.mq_msgsize,NULL);
    printf("\n%s\n",buf);
    free(buf);
  }
}
Example #3
0
int nilfs_cleaner_reload(struct nilfs_cleaner *cleaner, const char *conffile)
{
	struct nilfs_cleaner_request_with_path req;
	struct nilfs_cleaner_response res;
	size_t pathlen, reqsz;
	int bytes, ret = -1;

	if (cleaner->sendq < 0 || cleaner->recvq < 0) {
		errno = EBADF;
		goto out;
	}
	if (nilfs_cleaner_clear_queueu(cleaner) < 0)
		goto out;

	if (conffile) {
		if (myrealpath(conffile, req.pathname,
			       NILFS_CLEANER_MSG_MAX_PATH) == NULL)
			goto out;

		pathlen = strlen(req.pathname);
		req.hdr.argsize = pathlen + 1;
		reqsz = sizeof(req.hdr) + pathlen + 1;
	} else {
		req.hdr.argsize = 0;
		reqsz = sizeof(req.hdr);
	}
	req.hdr.cmd = NILFS_CLEANER_CMD_RELOAD;
	uuid_copy(req.hdr.client_uuid, cleaner->client_uuid);

	ret = mq_send(cleaner->sendq, (char *)&req, reqsz,
		      NILFS_CLEANER_PRIO_NORMAL);
	if (ret < 0)
		goto out;

	bytes = mq_receive(cleaner->recvq, (char *)&res, sizeof(res), NULL);
	if (bytes < sizeof(res)) {
		if (bytes >= 0)
			errno = EIO;
		ret = -1;
		goto out;
	}
	if (res.result == NILFS_CLEANER_RSP_NACK) {
		ret = -1;
		errno = res.err;
	}
out:
	return ret;
}
Example #4
0
void server_handler(union sigval sv){

  printf("server handler started\n");
  #define MAX_CLIENTS 10
  struct mq_attr attributes;
  char *buf, *message;
  char keyValue, testValue='/';
  char testString[45] = "yolo hello ./knkp . not be included";
  int current_client = 0;
  int clients = 0;
  mqd_t client_list[MAX_CLIENTS];
  mqd_t mqdes = *((mqd_t *)sv.sival_ptr);

  if(mq_getattr(mqdes, &attributes)==-1){
    handle_error("mq_getattr");
  }

  while(1){
    buf = malloc(attributes.mq_msgsize+1);
    mq_receive(mqdes,buf,attributes.mq_msgsize,NULL);
    printf("new message\n");
    parse_message2(buf, &message);
    printf("first value of message is: %c\n",  &message[0]);
    printf("message is: %s\n",&message);
    keyValue = &message[0];

    if(keyValue == testValue){
      
      printf("got new client\n");
   
      if(clients == MAX_CLIENTS){
	printf("reached maximum clients\n");
      }
      else {
	client_list[clients] = mq_open(&message, O_RDWR);
        clients++;
      }
    }

    else {
      printf("sending messages\n");
      for(current_client = 0; current_client < clients; current_client++){
	send_message(client_list[current_client], &message);
      }
    }
      free(buf);
  }
}
Example #5
0
static int
(mqrecv) (mqd_t q, int line)
{
  char c;
  ssize_t rets = TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
  if (rets != 1)
    {
      if (rets == -1)
	printf ("mq_receive on line %d failed with: %m\n", line);
      else
	printf ("mq_receive on line %d returned %zd != 1\n",
		line, rets);
      return 1;
    }
  return 0;
}
Example #6
0
void *sort8k_entry_posix( void *data ) {
    void *ptr;
    unsigned int dummy = 23;
    timing_t t_start = 0, t_stop = 0;
    
    while ( 1 ) {
        mq_receive(mb_start, (void*)&ptr, sizeof(ptr), 0);
//        fprintf(stderr, "*");

        t_start = gettime();
        bubblesort( (unsigned int*) ptr, N);
        t_stop = gettime();
        fprintf(stderr, "bubble: %d ms\n", calc_timediff_ms( t_start, t_stop ));
        mq_send(mb_done, (void*)&dummy, sizeof(dummy), 0);
    }
}
int *receive(void * ID)
{
	int i;
	int ThreadID = *(int *)ID;

	printf("Enter into receive[%d] \n", ThreadID);
	for (i = 0; i< MAX_MSG; i++) {
		if (-1 == mq_receive(mq, r_msg_ptr[ThreadID][i], MSG_SIZE, NULL)) {
			perror("mq_receive doesn't return success \n");
			pthread_exit((void *)1);
		}
		printf("[%d] receive '%s' in thread receive[%d]. \n", i+1, r_msg_ptr[ThreadID][i], ThreadID);
	}
	printf("receive[%d] quit ...\n", ThreadID);
	pthread_exit((void *)0);
}
Example #8
0
int main()
{

	char mqname[NAMESIZE];
	mqd_t mqdes;
        char msgrv[BUFFER];
	struct mq_attr attr;
	int unresolved = 0, failure = 0;

	sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid());

	attr.mq_msgsize = BUFFER;
	attr.mq_maxmsg = BUFFER;
	mqdes = mq_open(mqname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR, &attr);
	if (mqdes == (mqd_t)-1) {
		perror(ERROR_PREFIX "mq_open()");
		unresolved = 1;
	}
       	if (mq_receive(mqdes, msgrv, BUFFER, NULL) == -1) {
		if (EBADF != errno) {
			printf("errno != EBADF \n");
			failure = 1;
		}
	}
	else {
		printf("mq_receive() succeed unexpectly \n");
		failure = 1;
	}
	if (mq_close(mqdes) != 0) {
                perror(ERROR_PREFIX "mq_close");
                unresolved=1;
        }
        if (mq_unlink(mqname) != 0) {
                perror(ERROR_PREFIX "mq_unlink()");
                unresolved=1;
        }
        if (failure==1) {
                printf("Test FAILED\n");
                return PTS_FAIL;
        }
        if (unresolved==1) {
                printf("Test UNRESOLVED\n");
                return PTS_UNRESOLVED;
	}
	printf("Test PASSED\n");
        return PTS_PASS;
}
Example #9
0
int main(int argc, char *argv[]) {
	mqd_t msgq_id;
	char* msgcontent = (char*)malloc(MAX_MSG_LEN);
	int msgsz;
	unsigned int sender;
	struct mq_attr msgq_attr;
	
	assert(argc == 2);
	if (argv[1][0] != '/') {
		fprintf(stderr, "Queue name must start with '/'\n");
		exit(EXIT_FAILURE);
	}
	
	/* opening the queue		--  mq_open() */
	msgq_id = mq_open(argv[1], O_RDWR);
	if (msgq_id == (mqd_t)-1) {
		perror("In mq_open()");
		exit(1);
	}

	/* getting the attributes from the queue		--  mq_getattr() */
	mq_getattr(msgq_id, &msgq_attr);
	printf("Queue \"%s\":\n\t- stores at most %ld messages\n\t- large at most %ld bytes each\n\t- currently holds %ld messages\n", argv[1], msgq_attr.mq_maxmsg, msgq_attr.mq_msgsize, msgq_attr.mq_curmsgs);

	/* getting a message */
	while(1){
		msgsz = mq_receive(msgq_id, msgcontent, MAX_MSG_LEN, &sender);
		if (msgsz == -1) {
			perror("In mq_receive()");
			exit(1);
		}

		if(msgsz != 0){
			msgcontent[msgsz] = '\0';
			fprintf(stdout,"%d %s\n", sender, msgcontent);
			
		}
		if(sender == 666){
			break;
		}
	}
	/* closing the queue	--  mq_close() */
	mq_close(msgq_id);
	
	
	return 0;
}
Example #10
0
static void *
log_thread (void *arg)
{
  int len;
  errh_sMsg buf;

  while (1) {
    len = mq_receive(mqid, (char *)&buf, LOG_MAX_MSG_SIZE, NULL);
    if (len == -1) {
      if (errno != EINTR)
        perror("rt_logmod.c: mq_receive ");
    } else {
      switch ( buf.message_type) {
      case errh_eMsgType_Log:
	len -= (sizeof(buf) - sizeof(buf.str) - sizeof(buf.message_type) + 1);
	buf.str[len] = 0;
	pthread_mutex_lock(&fileMutex);
	if (logFile != -1) {			    			       
	  /* Set up a timer if you want better performance, ML */
	  CheckTimeStamp(newLogFile); 
	  newLogFile = 0; 	   
	  write(logFile, buf.str, len);
	  write(logFile, "\n", 1);
	}
	pthread_mutex_unlock(&fileMutex);
	
	pthread_mutex_lock(&termMutex);
	if (term != -1) {			    	
	  write(term, buf.str, len);
	  write(term, "\n", 1);
	}
	pthread_mutex_unlock(&termMutex);

	if (logToStdout)
	  printf("%.*s\n", len, buf.str);

	if ( errl_log_cb)
	  (errl_log_cb)( errl_log_userdata, buf.str, buf.severity, buf.sts, buf.anix, buf.message_type);
	break;
      case errh_eMsgType_Status:
	if ( errl_log_cb)
	  (errl_log_cb)( errl_log_userdata, 0, 0, buf.sts, buf.anix, buf.message_type);
      }
    }
  }
}
Example #11
0
void 
doit(register iter_t iterations, void *cookie)
{
	state_t *state = (state_t *) cookie;
	char		c;
	register char	*cptr = &c;
    register mqd_t mq = state->mq;

	while (iterations-- > 0) {
		if (mq_send(mq, cptr, sizeof(c), 0) == -1 ||
		    mq_receive(mq, buf, sizeof(buf), NULL) == -1) {
			perror("(r) receive/send on mq");
            mq_close(mq);
			exit(1);
		}
	}
}
Example #12
0
int main(int argc, char **argv)
{
	int c, flags;
	mqd_t mqd;
	ssize_t n;
	unsigned int prio;
	void *buff;
	struct mq_attr attr;

	flags = O_RDONLY;
	while ((c = getopt(argc, argv, "n")) != -1) {
		switch (c) {
		case 'n':
			flags |= O_NONBLOCK;
			break;
		case '?':
			exit(1);
		}
	}
	if (optind != argc - 1) {
		fprintf(stderr, "usage: mqreceive [ -n ] <name>\n");
		exit(1);
	}

	if ((mqd = mq_open(argv[optind], flags)) == -1) {
		fprintf(stderr, "mq_open error for %s: %s\n", argv[optind], strerror(errno));
		exit(1);
	}
	if (mq_getattr(mqd, &attr) == -1) {
		perror("mq_getattr error");
		exit(1);
	}

	if ((buff = malloc(attr.mq_msgsize)) == NULL) {
		perror("malloc error");
		exit(1);
	}

	if ((n = mq_receive(mqd, buff, attr.mq_msgsize, &prio)) == -1) {
		perror("mq_receive error");
		exit(1);
	}
	printf("read %ld bytes, priority = %u\n", (long) n, prio);

	exit(0);
}
Example #13
0
void notify_thread(union sigval arg)
{
  int n, prio;

  if (mq_notify(mqd, &sigev) == -1)
  {
    perror("mq notify error");
    exit(1);
  }
  if ((n = mq_receive(mqd, buffer, len, &prio)) == -1)
  {
    perror("receive mq error");
    exit(1);
  }

  printf("SIGUSR1 received, read %d bytes, buffer: %s\n", n, buffer);
}
Example #14
0
static void notify_thread(union sigval arg)
{
	ssize_t n;
	void *buff;

	printf("notify_thread started\n");
	buff = (void *)Malloc(attr.mq_msgsize);
	Mq_notify(mqd, &sigev);

	while ((n = mq_receive(mqd, buff, attr.mq_msgsize, NULL)) >= 0)
		printf("read %ld bytes\n", (long)n);
	if (errno != EAGAIN)
		err_sys("mq_receive error");

	free(buff);
	pthread_exit(NULL);
}
Example #15
0
void msg_rcv(mqd_t mqdes, int i)
{
    char buf[10000];
    errno = 0;
    if (mq_receive(mqdes, buf, sizeof(buf), NULL) == -1) {
        perror("mq_receive failed");
        fflush(stdout);
        sleep(1);
        exit(1);
    }
    if (i != atoi(buf)) {
        printf("Msg mismatch: expected: %d, got: %s\n", i, buf);
        fflush(stdout);
        sleep(1);
        exit(1);
    }
}
Example #16
0
int main(int argc, char *argv[])
{
	mqd_t mqd = mq_open(MQ_NAME, O_RDWR | O_CREAT, 0666, NULL);
	
	if (mqd == (mqd_t)-1) {
		errExit("mq_open");
	}

#if 0
	char s[][8] =  {
			"1hello",
			"2world",
			"3linux",
			"4test",
			"5uplook",
		};
	
	int i;
	for (i = 0; i < sizeof s/ sizeof(s[0]) - 1; ++i) {
		mq_send(mqd, s[i], sizeof(s[i]), 4);
	}
	mq_send(mqd, s[i], sizeof(s[i]), 1);
#endif
	char rbuf[BUFF_SIZE] = {0};
	unsigned prio = 0;
	int n = 0;
	while (1) {
		if  ((n = mq_receive(mqd, rbuf, BUFF_SIZE, &prio)) > 0) {
			printf("reveive_num = %d\n", n);
			printf("buf:%s, proj:%u\n", rbuf, prio);
		} else if (n == 0) {
			printf("receive end\n");
			break;
		} else {
			errExit("mq_receive");
		}
	}


	printf("mq_recv over\n");

	mq_close(mqd);
	mq_unlink(MQ_NAME);	

	return 0;
}
Example #17
0
Communication_Thread::Communication_Thread()
{
	mq_attr att;
	att.mq_maxmsg = 10;
	att.mq_msgsize = sizeof(Msg_Ordre_Com);

	bal_ordre_com = mq_open(BAL_ORDRE_COM, O_RDONLY | O_NONBLOCK | O_CREAT, S_IRWXU, &att);

	Msg_Ordre_Com msg_ordre_com;
	while (mq_receive(bal_ordre_com, (char*)&msg_ordre_com, sizeof(Msg_Ordre_Com), NULL)!= -1) {}


	att.mq_msgsize = sizeof(Msg_Com_Robot);
	bal_com_robot = mq_open(BAL_COM_ROBOT, O_WRONLY | O_CREAT | O_NONBLOCK, S_IRWXU, &att);




	robot1 = new Communication();
	printf("Communication Thread : opening First Khepera  : %i\n", robot1->Open(FICHIER_ROBOT_1));

	robot2 = new Communication();
	printf("Communication Thread : opening Second Khepera : %i\n", robot2->Open(FICHIER_ROBOT_2));


	for (int i = 0; i<4; i++)
	{
		liste.ordres[i].robot1.pas_gauche = 0;
		liste.ordres[i].robot1.pas_droite = 0;
		liste.ordres[i].robot2.pas_gauche = 0;
		liste.ordres[i].robot2.pas_droite = 0;

	}
	ordreCourantRobot1 = 4;
	ordreCourantRobot2 = 4;

	pasPreviousTopRobot1.pas_droite = 0;
	pasPreviousTopRobot1.pas_gauche = 0;

	pasPreviousTopRobot2.pas_droite = 0;
	pasPreviousTopRobot2.pas_gauche = 0;



}
Example #18
0
int receiveMessage(const MessageQueue* messageQueue,
                   char* msg,
                   size_t msg_max_len,
                   unsigned int* msg_priority)
{
    ssize_t msg_len;

    msg_len = mq_receive(messageQueue->mq_des, msg, msg_max_len, msg_priority);

//    if (msg_len < 0)
//    {
//        perror("\n\rmq_receive failed !!!\n");

//        switch(errno)
//        {
//            case EAGAIN:
//                perror("The queue was empty, and the O_NONBLOCK flag was set for the message queue description referred to by mqdes.\n");
//                break;
//            case EBADF:
//                perror("The descriptor specified in mqdes was invalid.\n");
//                break;
//            case EINTR:
//                perror("The call was interrupted by a signal handler.\n");
//                break;
//            case EINVAL:
//                perror("The call would have blocked, and abs_timeout was invalid, either because tv_sec was less than zero, or because tv_nsec was less than zero or greater than 1000 million.\n");
//                break;
//            case EMSGSIZE:
//                perror("msg_len was less than the mq_msgsize attribute of the message queue.\n");
//                break;
//            case ETIMEDOUT:
//                perror("The call timed out before a message could be transferred.\n");
//                break;
//            default :
//                perror("Unknown error\n");
//                break;
//        }

//        return -1;
//    }

    msg[msg_max_len -1] = '\0';

    return (int)msg_len;
}
Example #19
0
/* $$.bp$$ */
int
main(int argc, char **argv)
{
	int		nfds;
	char	c;
	fd_set	rset;
	mqd_t	mqd;
	void	*buff;
	ssize_t	n;
	struct mq_attr	attr;
	struct sigevent	sigev;

	if (argc != 2)
		err_quit("usage: mqnotifysig5 <name>");

		/* 4open queue, get attributes, allocate read buffer */
	mqd = Mq_open(argv[1], O_RDONLY | O_NONBLOCK);
	Mq_getattr(mqd, &attr);
	buff = Malloc(attr.mq_msgsize);

	Pipe(pipefd);

		/* 4establish signal handler, enable notification */
	Signal(SIGUSR1, sig_usr1);
	sigev.sigev_notify = SIGEV_SIGNAL;
	sigev.sigev_signo = SIGUSR1;
	Mq_notify(mqd, &sigev);

	FD_ZERO(&rset);
	for ( ; ; ) {
		FD_SET(pipefd[0], &rset);
		nfds = Select(pipefd[0] + 1, &rset, NULL, NULL, NULL);

		if (FD_ISSET(pipefd[0], &rset)) {
			Read(pipefd[0], &c, 1);
			Mq_notify(mqd, &sigev);			/* reregister first */
			while ( (n = mq_receive(mqd, buff, attr.mq_msgsize, NULL)) >= 0) {
				printf("read %ld bytes\n", (long) n);
			}
			if (errno != EAGAIN)
				err_sys("mq_receive error");
		}
	}
	exit(0);
}
Example #20
0
void gSystem_loop(void)
{
	char recvd_data[GAME_MQUEUE_RECV_BUF_SIZE];
	ssize_t bytes_read;
	st_game_msg *game_msg = NULL;
	en_game_return_code ret;
	en_mixed_return_code mret;
	mqd_t gsystem_mqueue;

	/* Setup mqueue. */
	debug(Gsystem_label, "Setup mqueue for receiving requests.");
	mret = mixed_mqueue_create(&gsystem_mqueue,
							GSYSTEM_MQUEUE_NAME,
							GAME_MQUEUE_SIZE,
							GAME_MQUEUE_CRRECV_MODE);
	if (mret != MIXED_RET_SUCCESS) {
		critical("Failed to setup mqueue.");
		exit(-1);
	}

	while(true) {

		memset(&recvd_data, 0, sizeof(recvd_data));
		bytes_read = mq_receive(gsystem_mqueue, recvd_data, GAME_MQUEUE_RECV_BUF_SIZE, NULL);

		if (bytes_read == -1) {
			error("Failed to receive message. errno: %d; msg: %s", errno, strerror(errno));
			continue;
		}
		game_msg = (st_game_msg *)recvd_data;

		//printf("bytes: %d - msg type: %d - msg id: %d\n", bytes_read, game_msg->type, game_msg->id);

		ret = gSystem_process_message(game_msg);
		if (ret == GAME_RET_HALT) {
			debug(Gsystem_label, "Received halt solicitation. Will exit...");
			/* Exiting message received. */
			break;
		}
	}

	/* Free the resources. */
	mixed_mqueue_close(&gsystem_mqueue, GSYSTEM_MQUEUE_NAME);
	debug(Gsystem_label, "System module exited the loop.");
}
Example #21
0
static ssize_t
qb_ipc_pmq_recv(struct qb_ipc_one_way *one_way,
		void *msg_ptr, size_t msg_len, int32_t ms_timeout)
{
	uint32_t msg_prio;
	struct timespec ts_timeout;
	ssize_t res;

	if (ms_timeout >= 0) {
		qb_util_timespec_from_epoch_get(&ts_timeout);
		qb_timespec_add_ms(&ts_timeout, ms_timeout);
	}

mq_receive_again:
	if (ms_timeout >= 0) {
		res = mq_timedreceive(one_way->u.pmq.q,
				      (char *)msg_ptr,
				      one_way->max_msg_size,
				      &msg_prio, &ts_timeout);
	} else {
		res = mq_receive(one_way->u.pmq.q,
				 (char *)msg_ptr,
				 one_way->max_msg_size, &msg_prio);
	}
	if (res == -1) {
		switch (errno) {
		case EINTR:
			goto mq_receive_again;
			break;
		case EAGAIN:
			res = -ETIMEDOUT;
			break;
		case ETIMEDOUT:
			res = -errno;
			break;
		default:
			res = -errno;
			qb_util_perror(LOG_ERR,
				       "error waiting for mq_timedreceive");
			break;
		}
	}

	return res;
}
Example #22
0
//-----------------------------------------------------------------------------
void child_func(int arg) {

int i;
size_t n = 0;
char inBuf[50];
uint priority = 0;
int nMsgs = 0;
struct mq_attr my_attrs ={0,0,0,0};
mode_t my_mode = 0;
int my_oflags = (O_RDONLY | O_NONBLOCK);
mqd_t rx_q = INVALID_PQUEUE;


  rt_printk("Starting child task %d\n", arg);
  for(i = 0; i < 3; i++) {

    rt_printk("child task %d, loop count %d\n", arg, i);

  }
  //Open the queue for reading
  rx_q = mq_open("my_queue", my_oflags, my_mode, &my_attrs);
  if (rx_q <= 0) {
    rt_printk("ERROR: child cannot open my_queue\n");
  } else {

    //Get the message(s) off the pQueue
    n = mq_getattr(rx_q, &my_attrs);
    nMsgs = my_attrs.mq_curmsgs;
    rt_printk("There are %d messages on the queue\n", nMsgs);

    while(nMsgs-- > 0) {
      n = mq_receive(rx_q, inBuf, sizeof(inBuf), &priority);
      inBuf[n] = '\0';

      //Report what happened
      rt_printk("Child got >%s<, %d bytes at priority %d\n", inBuf,n, priority);
    }
  }

  mq_close(rx_q);
  mq_unlink("my_queue");
  free_z_apps(rt_whoami());
  rt_task_delete(rt_whoami());

}
Example #23
0
void log_windows()
{
	/*Recuperation de la boite aux lettres*/
	int bal_log_windows = mq_open( BALWIN, O_RDONLY );
	log_t message;	
	
	do/*Envoi de message tant qu'on ne reçoit pas de trame de fin*/
	{
		/*Reception du message du superviseur*/
		mq_receive(bal_log_windows, message, sizeof(log_t), NULL);
		if( strcmp(message, TRAME_FIN) ) /*Si ce n'est pas un message de fin...*/
			envoyer(message);
	}
	while( strcmp(message, TRAME_FIN) );
	printf("Fin log win\n");
	
	pthread_exit(0);
}
Example #24
0
CAMLprim value
caml_backpack_mq_receive(value val_mq, value val_buff, value val_ofs, value val_len)
{
	CAMLparam4(val_mq, val_buff, val_ofs, val_len);
	CAMLlocal1(val_res);
	unsigned int prio;
	ssize_t size;

	if ((size = mq_receive(Int_val(val_mq), &Byte(val_buff, Long_val(val_ofs)),
			       Long_val(val_len), &prio)) == -1)
		uerror("mq_receive", Nothing);

	val_res = caml_alloc_tuple(2);
	Store_field(val_res, 0, Val_long(size));
	Store_field(val_res, 1, Val_int(prio));

	CAMLreturn(val_res);
}
Example #25
0
int main(int argc, char **argv)
{
  /* open mqueue for udev.rule program */
  struct mq_attr attr;
  attr.mq_flags = 0;
  attr.mq_maxmsg = 10;
  attr.mq_msgsize = MAX_MSG_SIZE;
  attr.mq_curmsgs = 0;
  mqd_t udev_mq = mq_open("/thotcon0x5", O_CREAT | O_RDONLY, 0666, &attr);

  while(1) {
    // read 1 message from queue
    bytes_read = mq_receive (mq, buffer, MAX_MSG_SIZE, NULL);

    // print the messsage
    printf("Msg: %s", buffer);
  }
}
Example #26
0
/**
  * read - the function will read bytes from the file
  *                    
  * @param fd    the file handle
  * @param buf   the recieving buffer point
  * @param count the maximum bytes to be read
  *
  * @return the result
  */
ssize_t _read(int fd, void *buf, size_t count)
{
    struct socket *socket = (struct socket *)fd;
    ssize_t size = -1;

    os_u32 data;
   
    if (mq_receive(socket->rx_mqd, (char *)&data, sizeof(os_u32), 0))
    {
        struct pbuf *pbuf = (struct pbuf *)data;
                
        size = ippkg_unpack(pbuf, (os_u8 *)buf, count);
                
        pbuf_free(pbuf);
    }
  
    return size;
}
Example #27
0
int main(int argc, char const *argv[])
{
    int seqnum;
    mqd_t mqd, client_mqd;
    struct mq_attr attr;

    openlog("mq_seqnum_client", LOG_PID, LOG_USER);

    srand(time(NULL));
    client_id = rand();
    snprintf(client_mq, 21, CLIENT_MQ, client_id);

    client_mqd = mq_open(client_mq, O_CREAT | O_RDONLY, S_IWUSR | S_IRUSR, NULL);
    if (client_mqd == (mqd_t) -1)
        log_err_exit("mq_open server error");
    if (atexit(clear) != 0)
        log_err_exit("atexit error");
    syslog(LOG_INFO, "open mq %s to receive", client_mq);

    if (mq_getattr(client_mqd, &attr) == -1)
        errExit("mq_getattr");

    mqd = mq_open(SERVER_MQ, O_WRONLY);
    if (mqd == (mqd_t) -1)
        log_err_exit("mq_open client error");
    syslog(LOG_INFO, "open mq %s to send", SERVER_MQ);

    if (mq_send(mqd, (char *) &client_id, sizeof(client_id), 0) == -1)
        log_err_exit("mq_send error");
    syslog(LOG_INFO, "client id sent: %d", client_id);

    if (mq_close(mqd) == -1)
        log_err_exit("mq_close server error");

    if (mq_receive(client_mqd, (char *) &seqnum, attr.mq_msgsize, NULL) == -1)
        log_err_exit("mq_receive error");

    if (mq_close(client_mqd) == -1)
        log_err_exit("mq_close client error");

    printf("%d\n", seqnum);

    exit(EXIT_SUCCESS);
}
Example #28
0
int main(int argc, char *argv[])
{
	int	c, flags;
	mqd_t	mqd;
	ssize_t	n;
	uint32_t prio;
	void	*buf;
	struct mq_attr attr;

	flags = O_RDONLY;
	while ((c = getopt(argc, argv, "n")) != -1) {
		switch (c) {
		case 'n':
			flags |= O_NONBLOCK;
			break;
		}
	}
	if (optind != argc -1)
		err_quit("usage: mqreceive [ -n ] <name>\n");

	mqd = mq_open(argv[optind],flags);
	if (mqd < 0) {
		err_sys("mq_open error:");
	}
	if (mq_getattr(mqd, &attr) < 0) {
		err_sys("mq_getattr error:");
	}

	buf = malloc(attr.mq_msgsize);
	if (buf == NULL) {
		err_sys("malloc error:");
	}

	n = mq_receive(mqd,buf, attr.mq_msgsize, &prio);
	if (n < 0) {
		err_sys("mq_receive error:");
	}

	printf("read %ld bytes, priority = %u\n",
	       (long)n, prio);
	
	
	return 0;
}
Example #29
0
int main(int argc, char **argv)
{
	mqd_t mqd;
	void *buff;
	ssize_t n;
	sigset_t zeromask, newmask, oldmask;
	struct mq_attr attr;
	struct sigevent sigev;

	if (argc != 2)
		err_quit("Usage: nfs3 <name>");

	mqd = Mq_open(argv[1], O_RDONLY | O_NONBLOCK);
	Mq_getattr(mqd, &attr);
	buff = (void *)Malloc(attr.mq_msgsize);

	Sigemptyset(&zeromask);
	Sigemptyset(&newmask);
	Sigemptyset(&oldmask);
	Sigaddset(&newmask, SIGUSR1);

	Signal(SIGUSR1, sig_usr1);
	sigev.sigev_notify = SIGEV_SIGNAL;
	sigev.sigev_signo = SIGUSR1;
	Mq_notify(mqd, &sigev);

	for ( ; ; )
	{
		Sigprocmask(SIG_BLOCK, &newmask, &oldmask);
		while (mqflag == 0)
			sigsuspend(&zeromask);
		mqflag = 0;

		Mq_notify(mqd, &sigev);
		while ((n = mq_receive(mqd, buff, attr.mq_msgsize, NULL)) >= 0)
		{
			printf("read %ld bytes\n", (long)n);
		}
		if (errno != EAGAIN)
			err_sys("mq_receive error");
		Sigprocmask(SIG_UNBLOCK, &newmask, NULL);
	}
	exit(0);
}
portLONG lPosixIPCReceiveMessage( mqd_t hPipeHandle, xMessageObject *pxMessage )
{
static char pcBuffer[ MAX_MESSAGE_SIZE ];
unsigned int uiPriority;
portLONG lBytesReceived = 0;
portLONG lBytesToProcess;
portLONG lReturn = pdFALSE;

	lBytesReceived = mq_receive( hPipeHandle, pcBuffer, MAX_MESSAGE_SIZE, &uiPriority );
	for ( lBytesToProcess = 0; lBytesToProcess < lBytesReceived; lBytesToProcess += sizeof( xMessageObject ) )
	{
		memcpy( pxMessage, pcBuffer, sizeof( xMessageObject ) );
		lReturn = pdTRUE;

		/* Only receiving one message at the moment. */
		break;
	}
	return lReturn;
}