1/*
2 * Copyright (c) 2003, Intel Corporation. All rights reserved.
3 * Created by:  crystal.xiong REMOVE-THIS AT intel DOT com
4 * This file is licensed under the GPL license.  For the full content
5 * of this license, see the COPYING file at the top level of this
6 * source tree.
7 */
8
9/*
10 *  mq_notify() test plan:
11 *  mq_notify will fail with EBUSY if a process is already registered for
12 *  notification by the message queue.
13 *
14 *  2/17/2004   call mq_close and mq_unlink before exit to release mq
15 *		resources
16 */
17
18#include <stdio.h>
19#include <errno.h>
20#include <mqueue.h>
21#include <fcntl.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25#include <unistd.h>
26#include "posixtest.h"
27
28#define TEST "9-1"
29#define FUNCTION "mq_notify"
30#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
31
32#define NAMESIZE	50
33
34void mqclean(mqd_t queue, const char *qname)
35{
36	mq_close(queue);
37	mq_unlink(qname);
38}
39
40int main()
41{
42	char mqname[NAMESIZE];
43	mqd_t mqdes;
44	struct sigevent notification;
45	int pid;
46	int status;
47
48	sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid());
49
50	mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0);
51	if (mqdes == (mqd_t)-1) {
52		perror(ERROR_PREFIX "mq_open");
53		return PTS_UNRESOLVED;
54	}
55
56	notification.sigev_notify = SIGEV_SIGNAL;
57	notification.sigev_signo = SIGUSR1;
58	if (mq_notify(mqdes, &notification) != 0) {
59		printf("Test FAILED \n");
60		mqclean(mqdes, mqname);
61		return PTS_FAIL;
62	}
63	pid = fork();
64        if (pid == -1) {
65                perror(ERROR_PREFIX "fork");
66		mqclean(mqdes, mqname);
67                return PTS_UNRESOLVED;
68        }
69        if (pid == 0) {
70                //child process
71		if (mq_notify(mqdes, &notification) == -1) {
72			if (EBUSY == errno) {
73				printf("Test PASSED \n");
74				return PTS_PASS;
75			}
76			else {
77				printf("errno != EBUSY \n");
78				printf("Test FAILED \n");
79				return PTS_FAIL;
80			}
81		}
82		else {
83			printf("Test FAILED \n");
84			return PTS_FAIL;
85		}
86	}
87	else {
88		//parent process
89		wait(&status);
90		mqclean(mqdes, mqname);
91		return status;
92	}
93}
94