예제 #1
0
파일: 18-2.c 프로젝트: 1587/ltp
int main(void)
{
	char qname[NAMESIZE];
	const char *msgptr = MSGSTR;
	struct timespec ts;
	mqd_t queue;

	sprintf(qname, "/msgqueue_%d", getpid());

	queue = mq_open(qname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL);
	if (queue == (mqd_t) - 1) {
		perror("mq_open() did not return success");
		printf("Test UNRESOLVED\n");
		return PTS_UNRESOLVED;
	}

	ts.tv_sec = time(NULL) - 1;
	ts.tv_nsec = -1;
	if (mq_timedsend(queue, msgptr, strlen(msgptr), 1, &ts) == -1) {
		printf("mq_timedsend() did fail on invalid abs_time\n");
		mq_close(queue);
		mq_unlink(qname);
		return PTS_PASS;
	}

	mq_close(queue);
	mq_unlink(qname);

	printf("mq_timedsend() did not fail on invalid abs_time\n");
	return PTS_PASS;
}
예제 #2
0
파일: 1-1.c 프로젝트: Mellanox/arc_ltp
int main()
{
        char qname[NAMESIZE], msgrcd[BUFFER];
        const char *msgptr = MSGSTR;
	struct timespec ts;
        mqd_t queue;
	struct mq_attr attr;
	int unresolved=0, failure=0;
	unsigned pri;

        sprintf(qname, "/mq_timedsend_1-1_%d", getpid());

	attr.mq_msgsize = BUFFER;
	attr.mq_maxmsg = MAXMSG;
        queue = mq_open(qname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, &attr);
        if (queue == (mqd_t)-1) {
                perror("mq_open() did not return success");
                return PTS_UNRESOLVED;
        }

	ts.tv_sec=time(NULL)+1;
	ts.tv_nsec=0;
        if (mq_timedsend(queue, msgptr, strlen(msgptr), 1, &ts) != 0) {
                perror("mq_timedsend() did not return success");
		failure=1;
        }

        if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
		perror("mq_receive() returned failure");
		failure=1;
	}

	if (strncmp(msgptr, msgrcd, strlen(msgptr)) != 0) {
		printf("FAIL:  sent %s received %s\n", msgptr, msgrcd);
		failure = 1;
	}

        if (mq_close(queue) != 0) {
		perror("mq_close() did not return success");
		unresolved=1;
        }

        if (mq_unlink(qname) != 0) {
		perror("mq_unlink() did not return success");
		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;
}
예제 #3
0
파일: 14-1.c 프로젝트: Nan619/ltp-ddt
int main()
{
	char qname[NAMESIZE];
	const char *msgptr = MSGSTR;
	struct timespec ts;
	mqd_t queue;
	int unresolved = 0, failure = 0, i;
	struct mq_attr attr;

	sprintf(qname, "/mq_timedsend_14-1_%d", getpid());

	ts.tv_sec = time(NULL);
	ts.tv_nsec = 0;
	for (i = 0; i < NUMINVALID; i++) {
		attr.mq_msgsize = messagesize[i];
		attr.mq_maxmsg = messagesize[i];

		queue =
		    mq_open(qname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
		if (queue == (mqd_t) - 1) {
			perror("mq_open() did not return success");
			return PTS_UNRESOLVED;
		}

		ts.tv_sec++;
		if (mq_timedsend(queue, msgptr, strlen(msgptr), 1, &ts) != -1) {
			printf("mq_timedsend() didn't ret -1 for EMSGSIZE\n");
			failure = 1;
		}

		if (errno != EMSGSIZE) {
			printf("errno != EMSGSIZE\n");
			failure = 1;
		}

		if (mq_close(queue) != 0) {
			perror("mq_close() did not return success");
			unresolved = 1;
		}

		if (mq_unlink(qname) != 0) {
			perror("mq_unlink() did not return success");
			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;
}
예제 #4
0
/* Add a message to queue */
int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
	    unsigned int msg_prio)
{
#ifdef __UCLIBC_HAS_THREADS_NATIVE__
	return mq_timedsend(mqdes, msg_ptr, msg_len, msg_prio, NULL);
#else
	return __syscall_mq_timedsend(mqdes, msg_ptr, msg_len, msg_prio, NULL);
#endif
}
예제 #5
0
int main()
{
        char qname[NAMESIZE];
        const char *msgptr = MSGSTR;
	struct timespec ts;
        mqd_t queue;
	int unresolved=0, failure=0, i;

        sprintf(qname, "/mq_timedsend_13-1_%d", getpid());

        queue = mq_open(qname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, NULL);
        if (queue == (mqd_t)-1) {
                perror("mq_open() did not return success");
                return PTS_UNRESOLVED;
        }

	ts.tv_sec=time(NULL)+1;
	ts.tv_nsec=0;
	for (i=0; i<NUMINVALID; i++) {
        	if (mq_timedsend(queue, msgptr, 
				strlen(msgptr), invalidpri[i], &ts)== 0) {
                	printf("mq_timedsend() ret success on invalid %d\n",
				invalidpri[i]);
			failure = 1;
        	}
		if (errno != EINVAL) {
			printf("errno not == EINVAL for invalid %d\n",
				invalidpri[i]);
			failure = 1;
		}
	}

        if (mq_close(queue) != 0) {
		perror("mq_close() did not return success");
		unresolved=1;
        }

        if (mq_unlink(qname) != 0) {
		perror("mq_unlink() did not return success");
		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;
}
예제 #6
0
/* runs without GVL */
static void *xsend(void *ptr)
{
	struct rw_args *x = ptr;

	x->retval = x->timeout ?
		mq_timedsend(x->des, x->msg_ptr, x->msg_len,
		             x->msg_prio, x->timeout) :
		mq_send(x->des, x->msg_ptr, x->msg_len, x->msg_prio);

	return NULL;
}
예제 #7
0
void mqueue_check_functions(mqd_t m)
{
    (void)mq_close(m);
    (void)mq_getattr(m, (struct mq_attr *)0);
    (void)mq_notify(m, (const struct sigevent *)0);
    (void)mq_open((const char *)0, 0, 0);
    (void)mq_receive(m, (char*)0, (size_t)0, (unsigned*)0);
    (void)mq_send(m, (const char *)0, (size_t)0, (unsigned)0);
    (void)mq_setattr(m, (const struct mq_attr *)0, (struct mq_attr *)0);
    (void)mq_timedreceive(m, (char*)0, (size_t)0, (unsigned*)0, (const struct timespec*)0);
    (void)mq_timedsend(m, (const char *)0, (size_t)0, (unsigned)0, (const struct timespec*)0);
    (void)mq_unlink((const char *)0);
}
예제 #8
0
int main()
{
        char qname[NAMESIZE];
        const char *msgptr = MSGSTR;
	struct timespec ts;
        mqd_t queue;
	int unresolved=0, failure=0;

        sprintf(qname, "/mq_timedsend_11-1_%d", getpid());

        queue = mq_open(qname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, NULL);
        if (queue == (mqd_t)-1) {
                perror("mq_open() did not return success");
                return PTS_UNRESOLVED;
        }

	ts.tv_sec=time(NULL)+1;
	ts.tv_nsec=0;
        if (mq_timedsend(queue+1, msgptr, strlen(msgptr), 1, &ts) != -1) {
                printf("mq_timedsend() did not return -1 on invalid queue\n");
		failure=1;
        }

	if (errno != EBADF) {
		printf("errno != EBADF\n");
		failure = 1;
	}

        if (mq_close(queue) != 0) {
		perror("mq_close() did not return success");
		unresolved=1;
        }

        if (mq_unlink(qname) != 0) {
		perror("mq_unlink() did not return success");
		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;
}
예제 #9
0
파일: 2-1.c 프로젝트: shubmit/shub-ltp
int main()
{
        char qname[NAMESIZE];
        const char *msgptr = MSGSTR;
	struct timespec ts;
        mqd_t queue;
	int unresolved=0, failure=0;
	struct mq_attr attr;

        sprintf(qname, "/mq_timedsend_2-1_%d", getpid());

	attr.mq_msgsize = MSGSIZE;
	attr.mq_maxmsg  = MSGSIZE;

        queue = mq_open(qname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, &attr);
        if (queue == (mqd_t)-1) {
                perror("mq_open() did not return success");
                return PTS_UNRESOLVED;
        }

	ts.tv_sec=time(NULL)+1;
	ts.tv_nsec=0;
        if (mq_timedsend(queue, msgptr, strlen(msgptr), 1, &ts) == 0) {
                printf("mq_timedsend() ret success w/msg_len>=mq_msgsize\n");
		failure=1;
        }

        if (mq_close(queue) != 0) {
		perror("mq_close() did not return success");
		unresolved=1;
        }

        if (mq_unlink(qname) != 0) {
		perror("mq_unlink() did not return success");
		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;
}
예제 #10
0
파일: comm.c 프로젝트: FondFish/OSS
SWORD32 VOS_TimedMsgQSend(WORD32 dwOSMsgQID, BYTE *pMsg, WORD32 dwMsgLen, WORD32 dwMsgPrio, WORD32 dwTimeOut)
{
    struct timespec  sTimeSpec;
    
    if (NO_WAIT == dwTimeOut)
    {
        sTimeSpec.tv_sec = -1;
        sTimeSpec.tv_nsec = -1;
    }
    else
    {
        /* 获取绝对时间 */
        clock_gettime(0, &sTimeSpec);
        sTimeSpec.tv_sec  += dwTimeOut/1000;
        sTimeSpec.tv_sec += ((dwTimeOut%1000)*1000*1000 + sTimeSpec.tv_nsec)/(1000*1000*1000);
        sTimeSpec.tv_nsec = ((dwTimeOut%1000)*1000*1000 + sTimeSpec.tv_nsec)%(1000*1000*1000);
    }
    return mq_timedsend(dwOSMsgQID, (CHAR*)pMsg, dwMsgLen, dwMsgPrio, &sTimeSpec);
}
예제 #11
0
파일: init.c 프로젝트: cloud-hot/rtems
static void benchmark_mq_timedsend(void)
{
  benchmark_timer_t end_time;
  int              status;
  struct timespec  timeout;

  status = 5;
  timeout.tv_sec  = 0;
  timeout.tv_nsec = 1;
  benchmark_timer_initialize();
    status = mq_timedsend(
	     queue, (const char *)&status, MQ_MSGSIZE, 1, &timeout);
  end_time = benchmark_timer_read();
  rtems_test_assert( status != (-1) );

  put_time(
    "mq_timedsend: no threads waiting",
    end_time,
    1,        /* Only executed once */
    0,
    0
  );
}
예제 #12
0
static void *
tf (void *arg)
{
  int r = pthread_barrier_wait (&b);
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("tf: barrier_wait failed");
      exit (1);
    }

  pthread_cleanup_push (cl, NULL);

  char c = ' ';

  switch ((long) arg)
    {
    case TF_MQ_SEND:
      TEMP_FAILURE_RETRY (mq_send (q, &c, 1, 1));
      break;
    case TF_MQ_TIMEDSEND:
      TEMP_FAILURE_RETRY (mq_timedsend (q, &c, 1, 1, &never));
      break;
    case TF_MQ_RECEIVE:
      TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
      break;
    case TF_MQ_TIMEDRECEIVE:
      TEMP_FAILURE_RETRY (mq_timedreceive (q, &c, 1, NULL, &never));
      break;
    }

  pthread_cleanup_pop (0);

  printf ("tf: %s returned\n", names[(long) arg]);

  exit (1);
}
예제 #13
0
파일: 3-2.c 프로젝트: 1587/ltp
int main(void)
{
	char qname[NAMESIZE], msgrcd[BUFFER];
	const char *msgptr1 = MSG1;
	const char *msgptr2 = MSG2;
	const char *msgptr3 = MSG3;
	const char *msgptr4 = MSG4;
	const char *msgptr5 = MSG5;
	struct timespec ts;
	mqd_t queue;
	struct mq_attr attr;
	int unresolved = 0, failure = 0;
	unsigned pri;

	sprintf(qname, "/mq_timedsend_3-2_%d", getpid());

	attr.mq_msgsize = BUFFER;
	attr.mq_maxmsg = BUFFER;
	queue = mq_open(qname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
	if (queue == (mqd_t) - 1) {
		perror("mq_open() did not return success");
		return PTS_UNRESOLVED;
	}

	ts.tv_sec = time(NULL) + 1;
	ts.tv_nsec = 0;
	if (mq_timedsend(queue, msgptr3, strlen(msgptr3), PRI3, &ts) != 0) {
		perror("mq_timedsend() did not return success");
		printf("error sending %s\n", msgptr3);
		failure = 1;
	}

	ts.tv_sec++;
	if (mq_timedsend(queue, msgptr1, strlen(msgptr1), PRI1, &ts) != 0) {
		perror("mq_timedsend() did not return success");
		printf("error sending %s\n", msgptr1);
		failure = 1;
	}

	ts.tv_sec++;
	if (mq_timedsend(queue, msgptr4, strlen(msgptr4), PRI4, &ts) != 0) {
		perror("mq_timedsend() did not return success");
		printf("error sending %s\n", msgptr4);
		failure = 1;
	}

	ts.tv_sec++;
	if (mq_timedsend(queue, msgptr2, strlen(msgptr2), PRI2, &ts) != 0) {
		perror("mq_timedsend() did not return success");
		printf("error sending %s\n", msgptr2);
		failure = 1;
	}

	ts.tv_sec++;
	if (mq_timedsend(queue, msgptr5, strlen(msgptr5), PRI5, &ts) != 0) {
		perror("mq_timedsend() did not return success");
		printf("error sending %s\n", msgptr5);
		failure = 1;
	}

	if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
		perror("mq_receive() returned failure");
		unresolved = 1;
	}

	if (strncmp(msgptr1, msgrcd, strlen(msgptr1)) != 0) {
		printf("FAIL:  sent %s received %s\n", msgptr1, msgrcd);
		failure = 1;
	}

	if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
		perror("mq_receive() returned failure");
		unresolved = 1;
	}

	if (strncmp(msgptr2, msgrcd, strlen(msgptr2)) != 0) {
		printf("FAIL:  sent %s received %s\n", msgptr2, msgrcd);
		failure = 1;
	}

	if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
		perror("mq_receive() returned failure");
		unresolved = 1;
	}

	if (strncmp(msgptr3, msgrcd, strlen(msgptr3)) != 0) {
		printf("FAIL:  sent %s received %s\n", msgptr3, msgrcd);
		failure = 1;
	}

	if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
		perror("mq_receive() returned failure");
		unresolved = 1;
	}

	if (strncmp(msgptr4, msgrcd, strlen(msgptr4)) != 0) {
		printf("FAIL:  sent %s received %s\n", msgptr4, msgrcd);
		failure = 1;
	}

	if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
		perror("mq_receive() returned failure");
		unresolved = 1;
	}

	if (strncmp(msgptr5, msgrcd, strlen(msgptr5)) != 0) {
		printf("FAIL:  sent %s received %s\n", msgptr5, msgrcd);
		failure = 1;
	}

	if (mq_close(queue) != 0) {
		perror("mq_close() did not return success");
		unresolved = 1;
	}

	if (mq_unlink(qname) != 0) {
		perror("mq_unlink() did not return success");
		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;
}
예제 #14
0
void *a_thread_func()
{

    int i;
    struct sigaction act;
    char gqname[NAMESIZE];
    mqd_t gqueue;
    const char *msgptr = MSGSTR;
    struct mq_attr attr;
    struct timespec ts;

    /* Set up handler for SIGUSR1 */
    act.sa_handler=justreturn_handler;
    act.sa_flags=0;
    sigemptyset(&act.sa_mask);
    sigaction(SIGUSR1, &act, 0);

    /* Set up mq */
    sprintf(gqname, "/mq_timedsend_12-1_%d", getpid());

    attr.mq_maxmsg = MAXMSG;
    attr.mq_msgsize = BUFFER;
    gqueue = mq_open(gqname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, &attr);
    if (gqueue == (mqd_t)-1) {
        perror("mq_open() did not return success");
        pthread_exit((void*)PTS_UNRESOLVED);
        return NULL;
    }

    /* mq_timedsend will block for 10 seconds when it waits */
    ts.tv_sec=time(NULL)+10;
    ts.tv_nsec=0;

    /* Tell main it can go ahead and start sending SIGUSR1 signal */
    sem = INMAIN;

    for (i=0; i<MAXMSG+1; i++) {
        if (mq_timedsend(gqueue, msgptr,
                         strlen(msgptr), 1, &ts) == -1) {
            if (errno == EINTR) {
                if (mq_unlink(gqname) != 0) {
                    perror("mq_unlink() did not return success");
                    pthread_exit((void*)PTS_UNRESOLVED);
                    return NULL;
                }
                printf("thread: mq_timedsend interrupted by signal and correctly set errno to EINTR\n");
                errno_eintr=1;
                pthread_exit((void*)PTS_PASS);
                return NULL;
            } else {
                printf("mq_timedsend not interrupted by signal or set errno to incorrect code: %d\n", errno);
                pthread_exit((void*)PTS_FAIL);
                return NULL;
            }
        }
    }

    /* Tell main that it the thread did not block like it should have */
    sem = INTHREAD;

    perror("Error: thread never blocked\n");
    pthread_exit((void*)PTS_FAIL);
    return NULL;
}
예제 #15
0
파일: 5-2.c 프로젝트: ystk/debian-ltp
int main()
{
    int pid;
    const char *msgptr = MSGSTR;
    struct mq_attr attr;
    struct sigaction act;

    sprintf(gqname, "/mq_timedsend_5-2_%d", getpid());

    attr.mq_msgsize = BUFFER;
    attr.mq_maxmsg = MAXMSG;
    gqueue = mq_open(gqname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, &attr);
    if (gqueue == (mqd_t)-1) {
        perror("mq_open() did not return success");
        return PTS_UNRESOLVED;
    }

    /* parent and child use justreturn_handler to just return out of
     * situations -- parent uses to stop it's sleep and wait again for
     * the child; child uses to stop its mq_timedsend
     */
    act.sa_handler=justreturn_handler;
    act.sa_flags=0;
    sigemptyset(&act.sa_mask);
    sigaction(SIGABRT, &act, 0);

    if ((pid = fork()) == 0) {
        /* child here */
        int i;
        struct timespec ts;
        /* set up timeout to be as long as possible */
        ts.tv_sec=INT32_MAX;
        ts.tv_nsec=0;

        sleep(1);  // give parent time to set up handler
        for (i=0; i<MAXMSG+1; i++) {
            if (mq_timedsend(gqueue, msgptr,
                             strlen(msgptr), 1, &ts) == -1) {
                if (errno == EINTR) {
                    printf("mq_timedsend interrupted by signal\n");
                    return CHILDPASS;
                } else {
                    printf("mq_timedsend not interrupted by signal\n");
                    return CHILDFAIL;
                }
            }
            /* send signal to parent each time message is sent */
            kill(getppid(), SIGABRT);
        }

        printf("Child never blocked\n");
        return CHILDFAIL;
    } else {
        /* parent here */
        int j,k, blocking=0;

        for (j=0; j<MAXMSG+1; j++) {
            if (sleep(3) == 0) {
                /* If sleep finished, child is probably blocking */
                blocking=1; //set blocking flag
                kill(pid, SIGABRT); //signal child
                break;
            }
        }

        if (blocking!=1) {
            printf("Signal never blocked\n");
            kill(pid, SIGKILL); //kill child if not gone
            mq_close(gqueue);
            mq_unlink(gqname);
            return PTS_UNRESOLVED;
        }
        mq_close(gqueue);
        if (mq_unlink(gqname) != 0) {
            perror("mq_unlink()");
            kill(pid, SIGKILL); //kill child if not gone
            return PTS_UNRESOLVED;
        }

        if (wait(&k) == -1) {
            perror("Error waiting for child to exit\n");
            kill(pid, SIGKILL); //kill child if not gone
            return PTS_UNRESOLVED;
        }

        if (!WIFEXITED(k) || !WEXITSTATUS(k)) {
            printf("Test FAILED\n");
            return PTS_FAIL;
        }

        printf("Test PASSED\n");
        return PTS_PASS;
    }

    return PTS_UNRESOLVED;
}
static int
do_test (void)
{
  int result = 0;

  char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3];
  snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ());

  struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
  mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);

  if (q == (mqd_t) -1)
    {
      printf ("mq_open failed with: %m\n");
      return result;
    }
  else
    add_temp_mq (name);

  mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  if (q2 != (mqd_t) -1)
    {
      puts ("mq_open with O_EXCL unexpectedly succeeded");
      result = 1;
    }
  else if (errno != EEXIST)
    {
      printf ("mq_open did not fail with EEXIST: %m\n");
      result = 1;
    }

  char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3];
  snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ());

  attr.mq_maxmsg = -2;
  q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  if (q2 != (mqd_t) -1)
    {
      puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded");
      add_temp_mq (name2);
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_open with invalid mq_maxmsg did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  attr.mq_maxmsg = 2;
  attr.mq_msgsize = -56;
  q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  if (q2 != (mqd_t) -1)
    {
      puts ("mq_open with invalid mq_msgsize unexpectedly succeeded");
      add_temp_mq (name2);
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_open with invalid mq_msgsize did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  char buf[3];
  struct timespec ts;
  if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
    ts.tv_sec += 10;
  else
    {
      ts.tv_sec = time (NULL) + 10;
      ts.tv_nsec = 0;
    }

  if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0)
    {
      puts ("mq_timedreceive with too small msg_len did not fail");
      result = 1;
    }
  else if (errno != EMSGSIZE)
    {
      printf ("mq_timedreceive with too small msg_len did not fail with "
	      "EMSGSIZE: %m\n");
      result = 1;
    }

  ts.tv_nsec = -1;
  if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
    {
      puts ("mq_timedreceive with negative tv_nsec did not fail");
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_timedreceive with negative tv_nsec did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  ts.tv_nsec = 1000000000;
  if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
    {
      puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail");
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 };
  sigemptyset (&sa.sa_mask);
  sigaction (SIGALRM, &sa, NULL);

  struct itimerval it = { .it_value = { .tv_sec = 1 } };
  setitimer (ITIMER_REAL, &it, NULL);

  if (mq_receive (q, buf, 2, NULL) == 0)
    {
      puts ("mq_receive on empty queue did not block");
      result = 1;
    }
  else if (errno != EINTR)
    {
      printf ("mq_receive on empty queue did not fail with EINTR: %m\n");
      result = 1;
    }

  setitimer (ITIMER_REAL, &it, NULL);

  ts.tv_nsec = 0;
  if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
    {
      puts ("mq_timedreceive on empty queue did not block");
      result = 1;
    }
  else if (errno != EINTR)
    {
      printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n");
      result = 1;
    }

  buf[0] = '6';
  buf[1] = '7';
  if (mq_send (q, buf, 2, 3) != 0
      || (buf[0] = '8', mq_send (q, buf, 1, 4) != 0))
    {
      printf ("mq_send failed: %m\n");
      result = 1;
    }

  memset (buf, ' ', sizeof (buf));

  unsigned int prio;
  ssize_t rets = mq_receive (q, buf, 3, &prio);
  if (rets != 1)
    {
      if (rets == -1)
	printf ("mq_receive failed: %m\n");
      else
	printf ("mq_receive returned %zd != 1\n", rets);
      result = 1;
    }
  else if (prio != 4 || memcmp (buf, "8  ", 3) != 0)
    {
      printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8  \")\n",
	      prio, buf[0], buf[1], buf[2]);
      result = 1;
    }

  rets = mq_receive (q, buf, 2, NULL);
  if (rets != 2)
    {
      if (rets == -1)
	printf ("mq_receive failed: %m\n");
      else
	printf ("mq_receive returned %zd != 2\n", rets);
      result = 1;
    }
  else if (memcmp (buf, "67 ", 3) != 0)
    {
      printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n",
	      buf[0], buf[1], buf[2]);
      result = 1;
    }

  buf[0] = '2';
  buf[1] = '1';
  if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
    ts.tv_sec = time (NULL);
  ts.tv_nsec = -1000000001;
  if ((mq_timedsend (q, buf, 2, 5, &ts) != 0
       && (errno != EINVAL || mq_send (q, buf, 2, 5) != 0))
      || (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec,
	  (mq_timedsend (q, buf, 1, 4, &ts) != 0
	   && (errno != EINVAL || mq_send (q, buf, 1, 4) != 0))))
    {
      printf ("mq_timedsend failed: %m\n");
      result = 1;
    }

  buf[0] = '-';
  ts.tv_nsec = 1000000001;
  if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
    {
      puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail");
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  ts.tv_nsec = -2;
  if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
    {
      puts ("mq_timedsend with negative tv_nsec did not fail");
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_timedsend with megatove tv_nsec did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  setitimer (ITIMER_REAL, &it, NULL);

  if (mq_send (q, buf, 2, 8) == 0)
    {
      puts ("mq_send on full queue did not block");
      result = 1;
    }
  else if (errno != EINTR)
    {
      printf ("mq_send on full queue did not fail with EINTR: %m\n");
      result = 1;
    }

  setitimer (ITIMER_REAL, &it, NULL);

  ts.tv_sec += 10;
  ts.tv_nsec = 0;
  if (mq_timedsend (q, buf, 2, 7, &ts) == 0)
    {
      puts ("mq_timedsend on full queue did not block");
      result = 1;
    }
  else if (errno != EINTR)
    {
      printf ("mq_timedsend on full queue did not fail with EINTR: %m\n");
      result = 1;
    }

  memset (buf, ' ', sizeof (buf));

  if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
    ts.tv_sec = time (NULL);
  ts.tv_nsec = -1000000001;
  rets = mq_timedreceive (q, buf, 2, &prio, &ts);
  if (rets == -1 && errno == EINVAL)
    rets = mq_receive (q, buf, 2, &prio);
  if (rets != 2)
    {
      if (rets == -1)
	printf ("mq_timedreceive failed: %m\n");
      else
	printf ("mq_timedreceive returned %zd != 2\n", rets);
      result = 1;
    }
  else if (prio != 5 || memcmp (buf, "21 ", 3) != 0)
    {
      printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n",
	      prio, buf[0], buf[1], buf[2]);
      result = 1;
    }

  if (mq_receive (q, buf, 1, NULL) == 0)
    {
      puts ("mq_receive with too small msg_len did not fail");
      result = 1;
    }
  else if (errno != EMSGSIZE)
    {
      printf ("mq_receive with too small msg_len did not fail with "
	      "EMSGSIZE: %m\n");
      result = 1;
    }

  ts.tv_nsec = -ts.tv_nsec;
  rets = mq_timedreceive (q, buf, 2, NULL, &ts);
  if (rets == -1 && errno == EINVAL)
    rets = mq_receive (q, buf, 2, NULL);
  if (rets != 1)
    {
      if (rets == -1)
	printf ("mq_timedreceive failed: %m\n");
      else
	printf ("mq_timedreceive returned %zd != 1\n", rets);
      result = 1;
    }
  else if (memcmp (buf, "31 ", 3) != 0)
    {
      printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n",
	      buf[0], buf[1], buf[2]);
      result = 1;
    }

  if (mq_send (q, "", 0, 2) != 0)
    {
      printf ("mq_send with msg_len 0 failed: %m\n");
      result = 1;
    }

  rets = mq_receive (q, buf, 2, &prio);
  if (rets)
    {
      if (rets == -1)
	printf ("mq_receive failed: %m\n");
      else
	printf ("mq_receive returned %zd != 0\n", rets);
      result = 1;
    }

  long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX);
  if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max)
    {
      if (mq_send (q, buf, 1, mq_prio_max) == 0)
	{
	  puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded");
	  result = 1;
	}
      else if (errno != EINVAL)
	{
	  printf ("mq_send with MQ_PRIO_MAX priority did not fail with "
		  "EINVAL: %m\n");
	  result = 1;
	}

      if (mq_send (q, buf, 1, mq_prio_max - 1) != 0)
	{
	  printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n");
	  result = 1;
	}
    }

  if (mq_unlink (name) != 0)
    {
      printf ("mq_unlink failed: %m\n");
      result = 1;
    }

  q2 = mq_open (name, O_RDWR);
  if (q2 != (mqd_t) -1)
    {
      printf ("mq_open of unlinked %s without O_CREAT unexpectedly"
	      "succeeded\n", name);
      result = 1;
    }
  else if (errno != ENOENT)
    {
      printf ("mq_open of unlinked %s without O_CREAT did not fail with "
	      "ENOENT: %m\n", name);
      result = 1;
    }

  if (mq_close (q) != 0)
    {
      printf ("mq_close in parent failed: %m\n");
      result = 1;
    }

  if (mq_receive (q, buf, 2, NULL) == 0)
    {
      puts ("mq_receive on invalid mqd_t did not fail");
      result = 1;
    }
  else if (errno != EBADF)
    {
      printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n");
      result = 1;
    }

  if (mq_send (q, buf, 1, 2) == 0)
    {
      puts ("mq_send on invalid mqd_t did not fail");
      result = 1;
    }
  else if (errno != EBADF)
    {
      printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n");
      result = 1;
    }

  if (mq_getattr (q, &attr) == 0)
    {
      puts ("mq_getattr on invalid mqd_t did not fail");
      result = 1;
    }
  else if (errno != EBADF)
    {
      printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n");
      result = 1;
    }

  memset (&attr, 0, sizeof (attr));
  if (mq_setattr (q, &attr, NULL) == 0)
    {
      puts ("mq_setattr on invalid mqd_t did not fail");
      result = 1;
    }
  else if (errno != EBADF)
    {
      printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n");
      result = 1;
    }

  if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1)
    {
      puts ("mq_unlink of non-existant message queue unexpectedly succeeded");
      result = 1;
    }
  else if (errno != ENOENT)
    {
      printf ("mq_unlink of non-existant message queue did not fail with "
	      "ENOENT: %m\n");
      result = 1;
    }
  return result;
}
예제 #17
0
파일: 16-1.c 프로젝트: Mellanox/arc_ltp
int main()
{
	int pid;
	struct mq_attr attr;
        const char *msgptr = MSGSTR;

        sprintf(gqname, "/mq_timedsend_16-1_%d", getpid());

	attr.mq_maxmsg = MAXMSG;
	attr.mq_msgsize = BUFFER;
        gqueue = mq_open(gqname, O_CREAT |O_RDWR, S_IRUSR | S_IWUSR, &attr);
	mq_unlink(gqname);
        if (gqueue == (mqd_t)-1) {
                perror("mq_open() did not return success");
                return PTS_UNRESOLVED;
        }

	if ((pid = fork()) == 0) {
		/* child here */
		int i, sig;
		struct timespec ts;
		sigset_t mask;

		/* wait for parent to set up handler */
		sigemptyset(&mask);
		sigaddset(&mask, SIGUSR1);
		sigprocmask(SIG_BLOCK,&mask,NULL);
		sigwait(&mask, &sig);

		/* child should block in < TIMEOUT seconds */
#ifdef _POSIX_TIMERS
		printf("Using CLOCK_REALTIME\n");
		clock_gettime(CLOCK_REALTIME, &ts);
		ts.tv_sec += TIMEOUT;
#else
		ts.tv_sec=time(NULL)+TIMEOUT;
#endif
		ts.tv_nsec=0;

		for (i=0; i<MAXMSG+1; i++) {
        		if (mq_timedsend(gqueue, msgptr,
						strlen(msgptr), 1, &ts) != 0) {
				/* send will fail after timeout occurs*/
				kill(getppid(), SIGABRT);
				return CHILDPASS;
        		}
			/* send signal to parent each time message is sent */
			kill(getppid(), SIGABRT);
		}
		printf("Child never interrupted\n");
		return CHILDFAIL;
	} else {
		/* parent here */
		struct sigaction act;
		int j;

		/* parent runs stopsleep_handler when sleep is interrupted
                   by child */
		act.sa_handler=stopsleep_handler;
		act.sa_flags=0;
		sigemptyset(&act.sa_mask);
		sigaction(SIGABRT, &act, 0);

		/* wait 1 second and tell child handler is set up */
		struct timespec ts;
		ts.tv_sec = 1;
		ts.tv_nsec = 0;
		nanosleep(&ts, NULL);
		kill(pid, SIGUSR1);

		/* wait for heartbeats from child */
		for (j=0; j<MAXMSG+1; j++) {
			ts.tv_sec = 3;
			ts.tv_nsec = 0;
			if (nanosleep(&ts, NULL)
				== 0)
			{
			/* If sleep finished, child is probably blocking */
				break;
			}
		}

		if (j == MAXMSG+1) {
			printf("Child never blocked\n");
			printf("Test FAILED\n");
			kill(pid, SIGKILL); //kill child
			mq_close(gqueue);
			mq_unlink(gqname);
			return PTS_FAIL;
		}

		/*
		 * Wait for timeout to complete.
		 */
		ts.tv_sec = TIMEOUT;
		ts.tv_nsec = 0;
		if (nanosleep(&ts, NULL) == 0) {
			/*
		 	* If sleep lasted the full time, child never timed out
		 	*/
			printf("Child never timed out\n");
			kill(pid, SIGKILL); //kill child
			mq_close(gqueue);
			mq_unlink(gqname);
			printf("Test FAILED\n");
			return PTS_FAIL;
		}

		mq_close(gqueue);
		mq_unlink(gqname);
		printf("Test PASSED\n");
		return PTS_PASS;
	}

	return PTS_UNRESOLVED;
}
예제 #18
0
int main()
{
        char *msgptr=MSGSTR;
	struct timespec ts;
	struct sigaction act;
	struct mq_attr attr;
	time_t currsec;
	int maxreached=0, i;

        sprintf(gqname, "/mq_timedsend_15-1_%d", getpid());

	attr.mq_msgsize = BUFFER;
	attr.mq_maxmsg = MAXMSG;
        gqueue = mq_open(gqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
        if (gqueue == (mqd_t)-1) {
                perror("mq_open() did not return success");
		printf("Test UNRESOLVED\n");
                return PTS_UNRESOLVED;
        }

	currsec = time(NULL);

	ts.tv_sec=currsec-5; //time in past
	ts.tv_nsec=0;

	/*
	 * If timeout never happens, set up an alarm that will go off
	 * after TIMEOUT seconds and call a handler to end the test
	 */
	act.sa_handler=testfailed_handler;
	act.sa_flags=0;
	sigemptyset(&act.sa_mask);
	sigaction(SIGALRM, &act, 0);
	alarm(TIMEOUT);

	for (i=0; i<MAXMSG+1; i++) {
        	if (mq_timedsend(gqueue, msgptr, strlen(msgptr), 1, &ts) 
								== -1) {
			maxreached=1;
			if (errno != ETIMEDOUT) {
				printf("errno != ETIMEDOUT\n");
				printf("Test FAILED\n");
				mq_close(gqueue);
				mq_unlink(gqname);
				return PTS_FAIL;
			}
			break;
        	}
	}

	mq_close(gqueue);
	mq_unlink(gqname);

	if (maxreached==0) {
		printf("Test UNRESOLVED:  Couldn't fill message queue\n");
		return PTS_UNRESOLVED;
	}

	if (time(NULL) > currsec+DELTA) {
		printf("Timeout lasted too long\n");
		printf("Test FAILED\n");
		return PTS_FAIL;
	}

        printf("Test PASSED\n");
        return PTS_PASS;
}
예제 #19
0
파일: 19-1.c 프로젝트: 1587/ltp
int main(void)
{
	char qname[NAMESIZE];
	char *msgptr = MSGSTR;
	struct timespec ts;
	mqd_t queue;
	struct mq_attr attr;
	int failure = 0, i, maxreached = 0;

	sprintf(qname, "/mq_timedsend_19-1_%d", getpid());

	attr.mq_maxmsg = MAXMSG;
	attr.mq_msgsize = BUFFER;
	queue = mq_open(qname, O_CREAT | O_RDWR | O_NONBLOCK,
			S_IRUSR | S_IWUSR, &attr);
	if (queue == (mqd_t) - 1) {
		perror("mq_open() did not return success");
		printf("Test UNRESOLVED\n");
		return PTS_UNRESOLVED;
	}

	ts.tv_sec = time(NULL);
	ts.tv_nsec = 0;
	for (i = 0; i < MAXMSG + 1; i++) {
		ts.tv_sec++;
		if (mq_timedsend(queue, msgptr, strlen(msgptr), 1, &ts)
		    == -1) {
			maxreached = 1;
			break;
		}
	}

	if (maxreached == 0) {
		printf("Test UNRESOLVED:  Couldn't fill message queue\n");
		mq_close(queue);
		mq_unlink(qname);
		return PTS_UNRESOLVED;
	}

	/*
	 * Message queue is full -- call mq_timedsend() with invalid
	 * timespec values
	 * First, open message queue as blocking
	 */
	mq_close(queue);
	queue = mq_open(qname, O_RDWR, S_IRUSR | S_IWUSR, NULL);
	if (queue == (mqd_t) - 1) {
		perror("mq_open() did not return success");
		printf("Test UNRESOLVED\n");
		return PTS_UNRESOLVED;
	}

	for (i = 0; i < NUMTESTS; i++) {
		ts.tv_nsec = invalid_tests[i];
		if (mq_timedsend(queue, msgptr, strlen(msgptr), 1, &ts) != -1) {
			printf("mq_timedsend() didn't fail w/invalid ts\n");
			printf("ts.tv_nsec = %ld\n", ts.tv_nsec);
			failure = 1;
		} else {
			if (errno != EINVAL) {
				printf("errno != EINVAL\n");
				printf("ts.tv_nsec = %ld\n", ts.tv_nsec);
				failure = 1;
			}
		}
	}

	mq_close(queue);
	mq_unlink(qname);

	if (failure == 1) {
		printf("Test FAILED\n");
		return PTS_FAIL;
	}

	printf("Test PASSED\n");
	return PTS_PASS;
}
예제 #20
0
파일: 9-1.c 프로젝트: shubmit/shub-ltp
int main()
{
        char qname[NAMESIZE], msgrcd[BUFFER];
        const char *msgptr = MSGSTR;
	struct timespec ts;
        mqd_t queue;
	struct mq_attr attr;
	int unresolved=0, failure=0;
	unsigned pri;

        sprintf(qname, "/mq_timedsend_9-1_%d", getpid());

	attr.mq_msgsize = BUFFER;
	attr.mq_maxmsg = BUFFER;
        queue = mq_open(qname, O_CREAT |O_RDWR | O_NONBLOCK,
			S_IRUSR | S_IWUSR, &attr);
        if (queue == (mqd_t)-1) {
                perror("mq_open() did not return success");
                return PTS_UNRESOLVED;
        }

	// Verify mq_timedsend() returns -1
	ts.tv_sec=time(NULL)+1;
	ts.tv_nsec=0;
        if (mq_timedsend(queue+1, msgptr, strlen(msgptr), 1, &ts) != -1) {
                printf("mq_timedsend() did not return -1 on invalid queue\n");
		failure=1;
        }

	// Verify errno is set
	if (errno != EBADF) {
		printf("errno was not set on invalid queue\n");
		failure = 1;
	}

	// Verify message was not queued (cannot be received)
        if (mq_receive(queue, msgrcd, BUFFER, &pri) != -1) {
        	if (strcmp(msgptr, msgrcd) == 0) {
			printf("Message ended up being sent\n");
			failure = 1;
		} else {
			printf("Error with mq_receive()\n");
			unresolved = 1;
		}
        }

        if (mq_close(queue) != 0) {
		perror("mq_close() did not return success");
		unresolved=1;
        }

        if (mq_unlink(qname) != 0) {
		perror("mq_unlink() did not return success");
		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;
}
예제 #21
0
static void *sender_thread(void *arg)
{
  mqd_t mqfd;
  char msg_buffer[TEST_MSGLEN];
  struct mq_attr attr;
  int status = 0;
  int nerrors = 0;
  int i;

  printf("sender_thread: Starting\n");

  /* Fill in attributes for message queue */

  attr.mq_maxmsg  = TEST_SEND_NMSGS-1;
  attr.mq_msgsize = TEST_MSGLEN;
  attr.mq_flags   = 0;

  /* Set the flags for the open of the queue.
   * Make it a blocking open on the queue, meaning it will block if
   * this process tries to send to the queue and the queue is full.
   *
   *   O_CREAT - the queue will get created if it does not already exist.
   *   O_WRONLY - we are only planning to write to the queue.
   *
   * Open the queue, and create it if the receiving process hasn't
   * already created it.
   */

  mqfd = mq_open("testmq", O_WRONLY|O_CREAT, 0666, &attr);
  if (mqfd < 0)
    {
        printf("sender_thread: ERROR mq_open failed\n");
        pthread_exit((pthread_addr_t)1);
    }

  /* Fill in a test message buffer to send */

  memcpy(msg_buffer, TEST_MESSAGE, TEST_MSGLEN);

  /* Perform the send TEST_SEND_NMSGS times */

  for (i = 0; i < TEST_SEND_NMSGS; i++)
    {
       struct timespec ts;
       status = clock_gettime(CLOCK_REALTIME, &ts);
       if (status != 0)
         {
           printf("sender_thread: ERROR clock_gettime failed\n");
         }
       ts.tv_sec += 5;

      /* The first TEST_SEND_NMSGS-1 send should succeed.  The last
       * one should fail with errno == ETIMEDOUT
       */

      status = mq_timedsend(mqfd, msg_buffer, TEST_MSGLEN, 42, &ts);
      if (status < 0)
        {
          if (i == TEST_SEND_NMSGS-1 && errno == ETIMEDOUT)
            {
              printf("sender_thread: mq_timedsend %d timed out as expected\n", i);
            }
          else
            {
              printf("sender_thread: ERROR mq_timedsend failure=%d on msg %d\n", errno, i);
              nerrors++;
            }
        }
      else
        {
          if (i == TEST_SEND_NMSGS-1)
            {
              printf("sender_thread: ERROR mq_timedsend of msg %d succeeded\n", i);
              nerrors++;
            }
          else
            {
              printf("sender_thread: mq_timedsend succeeded on msg %d\n", i);
            }
        }
    }

  /* Close the queue and return success */

  if (mq_close(mqfd) < 0)
    {
      printf("sender_thread: ERROR mq_close failed\n");
    }

  printf("sender_thread: returning nerrors=%d\n", nerrors);
  FFLUSH();
  return (pthread_addr_t)nerrors;
}
예제 #22
0
파일: mq_send.c 프로젝트: AubrCool/glibc
/* Add message pointed by MSG_PTR to message queue MQDES.  */
int
mq_send (mqd_t mqdes, const char *msg_ptr, size_t msg_len,
	 unsigned int msg_prio)
{
  return mq_timedsend (mqdes, msg_ptr, msg_len, msg_prio, NULL);
}
예제 #23
0
파일: mq_send.c 프로젝트: 4ian/emscripten
int mq_send(mqd_t mqd, const char *msg, size_t len, unsigned prio)
{
	return mq_timedsend(mqd, msg, len, prio, 0);
}
예제 #24
0
파일: 10-1.c 프로젝트: 1587/ltp
int main(void)
{
	char qname[NAMESIZE];
	char msgptr[MESSAGESIZE];
	struct timespec ts;
	struct mq_attr attr;
	mqd_t queue;
	int unresolved = 0, failure = 0, i, maxreached = 0;

	sprintf(qname, "/mq_timedsend_10-1_%d", getpid());

	attr.mq_msgsize = BUFFER;
	attr.mq_maxmsg = MAXMSG;
	queue = mq_open(qname, O_CREAT | O_RDWR | O_NONBLOCK,
			S_IRUSR | S_IWUSR, &attr);
	if (queue == (mqd_t) - 1) {
		perror("mq_open() did not return success");
		return PTS_UNRESOLVED;
	}

	ts.tv_sec = time(NULL) + 1;
	ts.tv_nsec = 0;
	for (i = 0; i < MAXMSG + 1; i++) {
		sprintf(msgptr, "message %d", i);
		if (mq_timedsend(queue, msgptr, strlen(msgptr), 1, &ts) == -1) {
			maxreached = 1;
			if (errno != EAGAIN) {
				printf("mq_timedsend() did not w/EAGAIN\n");
				failure = 1;
			}
			break;
		}
	}

	if (mq_close(queue) != 0) {
		perror("mq_close() did not return success");
		unresolved = 1;
	}

	if (mq_unlink(qname) != 0) {
		perror("mq_unlink() did not return success");
		unresolved = 1;
	}

	if (maxreached == 0) {
		printf("Test inconclusive:  Couldn't fill message queue\n");
		return PTS_UNRESOLVED;
	}
	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;
}
예제 #25
0
static int
do_test (void)
{
  int result = 0;

  char name[sizeof "/tst-mqueue4-" + sizeof (pid_t) * 3 + NAME_MAX];
  char *p;
  p = name + snprintf (name, sizeof (name), "/tst-mqueue4-%u", getpid ());
  struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
  mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);

  if (q == (mqd_t) -1)
    {
      printf ("mq_open failed with: %m\n");
      return result;
    }
  else
    add_temp_mq (name);

  *p = '.';
  memset (p + 1, 'x', NAME_MAX + 1 - (p - name));
  name[NAME_MAX + 1] = '\0';

  mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  if (q2 == (mqd_t) -1)
    {
      printf ("mq_open with NAME_MAX long name compoment failed with: %m\n");
      result = 1;
    }

  if (mq_unlink (name) != 0)
    {
      printf ("mq_unlink failed: %m\n");
      result = 1;
    }

  if (mq_close (q2) != 0)
    {
      printf ("mq_close failed: %m\n");
      result = 1;
    }

  name[NAME_MAX + 1] = 'x';
  name[NAME_MAX + 2] = '\0';
  q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  if (q2 != (mqd_t) -1)
    {
      puts ("mq_open with too long name component unexpectedly succeeded");
      mq_unlink (name);
      mq_close (q2);
      result = 1;
    }
  else if (errno != ENAMETOOLONG)
    {
      printf ("mq_open with too long name component did not fail with "
	      "ENAMETOOLONG: %m\n");
      result = 1;
    }

  if (mq_unlink (name) == 0)
    {
      puts ("mq_unlink with too long name component unexpectedly succeeded");
      result = 1;
    }
  else if (errno != ENAMETOOLONG)
    {
      printf ("mq_unlink with too long name component did not fail with "
	      "ENAMETOOLONG: %m\n");
      result = 1;
    }

  *p = '\0';
  attr.mq_maxmsg = 1;
  attr.mq_msgsize = 3;
  q2 = mq_open (name, O_CREAT | O_RDWR, 0600, &attr);
  if (q2 == (mqd_t) -1)
    {
      printf ("mq_open without O_EXCL failed with %m\n");
      result = 1;
    }

  char buf[3];
  strcpy (buf, "jk");
  if (mq_send (q, buf, 2, 4) != 0)
    {
      printf ("mq_send failed: %m\n");
      result = 1;
    }

  if (mq_send (q, buf + 1, 1, 5) != 0)
    {
      printf ("mq_send failed: %m\n");
      result = 1;
    }

  if (mq_getattr (q2, &attr) != 0)
    {
      printf ("mq_getattr failed: %m\n");
      result = 1;
    }

  if ((attr.mq_flags & O_NONBLOCK)
      || attr.mq_maxmsg != 2
      || attr.mq_msgsize != 2
      || attr.mq_curmsgs != 2)
    {
      printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
	      ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
	      (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg,
	      (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs);
      result = 1;
    }

  struct timespec ts;
  if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
    ++ts.tv_sec;
  else
    {
      ts.tv_sec = time (NULL) + 1;
      ts.tv_nsec = 0;
    }

  if (mq_timedsend (q2, buf, 1, 1, &ts) == 0)
    {
      puts ("mq_timedsend unexpectedly succeeded");
      result = 1;
    }
  else if (errno != ETIMEDOUT)
    {
      printf ("mq_timedsend did not fail with ETIMEDOUT: %m\n");
      result = 1;
    }

  if (mq_close (q2) != 0)
    {
      printf ("mq_close failed: %m\n");
      result = 1;
    }

  q2 = mq_open (name, O_RDONLY, 0600);
  if (q2 == (mqd_t) -1)
    {
      printf ("mq_open without O_CREAT failed with %m\n");
      result = 1;
    }

  mqd_t q3 = mq_open (name, O_RDONLY, 0600);
  if (q3 == (mqd_t) -1)
    {
      printf ("mq_open without O_CREAT failed with %m\n");
      result = 1;
    }

  memset (buf, ' ', sizeof (buf));

  unsigned int prio;
  ssize_t rets = mq_receive (q2, buf, 2, &prio);
  if (rets != 1)
    {
      if (rets == -1)
	printf ("mq_receive failed with: %m\n");
      else
	printf ("mq_receive returned %zd != 1\n", rets);
      result = 1;
    }
  else if (prio != 5 || memcmp (buf, "k  ", 3) != 0)
    {
      printf ("mq_receive returned prio %u (2) buf \"%c%c%c\" (\"k  \")\n",
	      prio, buf[0], buf[1], buf[2]);
      result = 1;
    }

  if (mq_getattr (q3, &attr) != 0)
    {
      printf ("mq_getattr failed: %m\n");
      result = 1;
    }

  if ((attr.mq_flags & O_NONBLOCK)
      || attr.mq_maxmsg != 2
      || attr.mq_msgsize != 2
      || attr.mq_curmsgs != 1)
    {
      printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n"
	      ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n",
	      (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg,
	      (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs);
      result = 1;
    }

  rets = mq_receive (q3, buf, 2, NULL);
  if (rets != 2)
    {
      if (rets == -1)
	printf ("mq_receive failed with: %m\n");
      else
	printf ("mq_receive returned %zd != 2\n", rets);
      result = 1;
    }
  else if (memcmp (buf, "jk ", 3) != 0)
    {
      printf ("mq_receive returned buf \"%c%c%c\" != \"jk \"\n",
	      buf[0], buf[1], buf[2]);
      result = 1;
    }

  if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
    ++ts.tv_sec;
  else
    {
      ts.tv_sec = time (NULL) + 1;
      ts.tv_nsec = 0;
    }

  if (mq_timedreceive (q2, buf, 2, NULL, &ts) != -1)
    {
      puts ("mq_timedreceive on empty queue unexpectedly succeeded");
      result = 1;
    }
  else if (errno != ETIMEDOUT)
    {
      printf ("mq_timedreceive on empty queue did not fail with "
	      "ETIMEDOUT: %m\n");
      result = 1;
    }

  if (mq_unlink (name) != 0)
    {
      printf ("mq_unlink failed: %m\n");
      result = 1;
    }

  if (mq_close (q) != 0)
    {
      printf ("mq_close failed: %m\n");
      result = 1;
    }

  if (mq_close (q2) != 0)
    {
      printf ("mq_close failed: %m\n");
      result = 1;
    }

  if (mq_close (q3) != 0)
    {
      printf ("mq_close failed: %m\n");
      result = 1;
    }

  return result;
}