xref: /freebsd/contrib/sendmail/libsm/t-notify.c (revision 10ff414c14eef433d8157f0c17904d740693933b)
1 /*
2  * Copyright (c) 2016 Proofpoint, Inc. and its suppliers.
3  *      All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  */
9 
10 #include <sm/gen.h>
11 
12 #include <stdio.h>
13 
14 # include <stdlib.h>
15 # include <unistd.h>
16 
17 # include <sm/heap.h>
18 # include <sm/string.h>
19 # include <sm/test.h>
20 # include <sm/notify.h>
21 
22 # define MAX_CNT	10
23 
24 /*
25 **  MSGTEST -- test of message queue.
26 **
27 **	Parameters:
28 **		owner -- create message queue.
29 **
30 **	Returns:
31 **		0 on success
32 **		< 0 on failure.
33 */
34 
35 static int
36 notifytest(owner)
37 	int owner;
38 {
39 	int r;
40 	size_t len;
41 	char buf[64];
42 #define TSTSTR "qf0001"
43 
44 	r = sm_notify_start(owner, 0);
45 	if (r < 0)
46 	{
47 		perror("sm_notify_start failed");
48 		return -1;
49 	}
50 
51 	if (!owner)
52 	{
53 		len = sm_strlcpy(buf, TSTSTR, sizeof(buf));
54 		r = sm_notify_snd(buf, len);
55 		SM_TEST(r >= 0);
56 		if (r < 0)
57 			goto end;
58 
59   end:
60 		return r;
61 	}
62 	else
63 	{
64 		r = sm_notify_rcv(buf, sizeof(buf), 5);
65 		SM_TEST(r >= 0);
66 		if (r < 0)
67 			return r;
68 		if (r > 0 && r < sizeof(buf))
69 			buf[r] = '\0';
70 		buf[sizeof(buf) - 1] = '\0';
71 		SM_TEST(strcmp(buf, TSTSTR) == 0);
72 		fprintf(stderr, "buf=\"%s\"\n", buf);
73 	}
74 	return 0;
75 }
76 
77 int
78 main(argc, argv)
79 	int argc;
80 	char *argv[];
81 {
82 	int ch;
83 	int r = 0;
84 	pid_t pid;
85 
86 # define OPTIONS	""
87 	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
88 	{
89 		switch ((char) ch)
90 		{
91 		  default:
92 			break;
93 		}
94 	}
95 
96 	r = sm_notify_init(0);
97 	if (r < 0)
98 	{
99 		perror("sm_notify_init failed\n");
100 		return -1;
101 	}
102 
103 	if ((pid = fork()) < 0)
104 	{
105 		perror("fork failed\n");
106 		return -1;
107 	}
108 
109 	sm_test_begin(argc, argv, "test notify");
110 	if (pid == 0)
111 	{
112 		/* give the parent the chance to setup data */
113 		sleep(1);
114 		r = notifytest(false);
115 	}
116 	else
117 	{
118 		r = notifytest(true);
119 	}
120 	SM_TEST(r >= 0);
121 	return sm_test_end();
122 }
123