1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000-2002, 2004, 2005 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate * All rights reserved.
4*7c478bd9Sstevel@tonic-gate *
5*7c478bd9Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set
6*7c478bd9Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of
7*7c478bd9Sstevel@tonic-gate * the sendmail distribution.
8*7c478bd9Sstevel@tonic-gate */
9*7c478bd9Sstevel@tonic-gate
10*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
11*7c478bd9Sstevel@tonic-gate
12*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
13*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: t-shm.c,v 1.22 2005/01/14 02:14:10 ca Exp $")
14*7c478bd9Sstevel@tonic-gate
15*7c478bd9Sstevel@tonic-gate #include <stdio.h>
16*7c478bd9Sstevel@tonic-gate
17*7c478bd9Sstevel@tonic-gate #if SM_CONF_SHM
18*7c478bd9Sstevel@tonic-gate # include <stdlib.h>
19*7c478bd9Sstevel@tonic-gate # include <unistd.h>
20*7c478bd9Sstevel@tonic-gate # include <sys/wait.h>
21*7c478bd9Sstevel@tonic-gate
22*7c478bd9Sstevel@tonic-gate # include <sm/heap.h>
23*7c478bd9Sstevel@tonic-gate # include <sm/string.h>
24*7c478bd9Sstevel@tonic-gate # include <sm/test.h>
25*7c478bd9Sstevel@tonic-gate # include <sm/shm.h>
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate # define SHMSIZE 1024
28*7c478bd9Sstevel@tonic-gate # define SHM_MAX 6400000
29*7c478bd9Sstevel@tonic-gate # define T_SHMKEY 21
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate ** SHMINTER -- interactive testing of shared memory
34*7c478bd9Sstevel@tonic-gate **
35*7c478bd9Sstevel@tonic-gate ** Parameters:
36*7c478bd9Sstevel@tonic-gate ** owner -- create segment.
37*7c478bd9Sstevel@tonic-gate **
38*7c478bd9Sstevel@tonic-gate ** Returns:
39*7c478bd9Sstevel@tonic-gate ** 0 on success
40*7c478bd9Sstevel@tonic-gate ** < 0 on failure.
41*7c478bd9Sstevel@tonic-gate */
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate int shminter __P((bool));
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate int
shminter(owner)46*7c478bd9Sstevel@tonic-gate shminter(owner)
47*7c478bd9Sstevel@tonic-gate bool owner;
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate int *shm, shmid;
50*7c478bd9Sstevel@tonic-gate int i, t;
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate shm = (int *) sm_shmstart(T_SHMKEY, SHMSIZE, 0, &shmid, owner);
53*7c478bd9Sstevel@tonic-gate if (shm == (int *) 0)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate perror("shminit failed");
56*7c478bd9Sstevel@tonic-gate return -1;
57*7c478bd9Sstevel@tonic-gate }
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate while ((t = getchar()) != EOF)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate switch (t)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate case 'c':
64*7c478bd9Sstevel@tonic-gate *shm = 0;
65*7c478bd9Sstevel@tonic-gate break;
66*7c478bd9Sstevel@tonic-gate case 'i':
67*7c478bd9Sstevel@tonic-gate ++*shm;
68*7c478bd9Sstevel@tonic-gate break;
69*7c478bd9Sstevel@tonic-gate case 'd':
70*7c478bd9Sstevel@tonic-gate --*shm;
71*7c478bd9Sstevel@tonic-gate break;
72*7c478bd9Sstevel@tonic-gate case 's':
73*7c478bd9Sstevel@tonic-gate sleep(1);
74*7c478bd9Sstevel@tonic-gate break;
75*7c478bd9Sstevel@tonic-gate case 'l':
76*7c478bd9Sstevel@tonic-gate t = *shm;
77*7c478bd9Sstevel@tonic-gate for (i = 0; i < SHM_MAX; i++)
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate ++*shm;
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate if (*shm != SHM_MAX + t)
82*7c478bd9Sstevel@tonic-gate fprintf(stderr, "error: %d != %d\n",
83*7c478bd9Sstevel@tonic-gate *shm, SHM_MAX + t);
84*7c478bd9Sstevel@tonic-gate break;
85*7c478bd9Sstevel@tonic-gate case 'v':
86*7c478bd9Sstevel@tonic-gate printf("shmval: %d\n", *shm);
87*7c478bd9Sstevel@tonic-gate break;
88*7c478bd9Sstevel@tonic-gate case 'S':
89*7c478bd9Sstevel@tonic-gate i = sm_shmsetowner(shmid, getuid(), getgid(), 0644);
90*7c478bd9Sstevel@tonic-gate printf("sm_shmsetowner=%d\n", i);
91*7c478bd9Sstevel@tonic-gate break;
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate return sm_shmstop((void *) shm, shmid, owner);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate
98*7c478bd9Sstevel@tonic-gate /*
99*7c478bd9Sstevel@tonic-gate ** SHMBIG -- testing of shared memory
100*7c478bd9Sstevel@tonic-gate **
101*7c478bd9Sstevel@tonic-gate ** Parameters:
102*7c478bd9Sstevel@tonic-gate ** owner -- create segment.
103*7c478bd9Sstevel@tonic-gate ** size -- size of segment.
104*7c478bd9Sstevel@tonic-gate **
105*7c478bd9Sstevel@tonic-gate ** Returns:
106*7c478bd9Sstevel@tonic-gate ** 0 on success
107*7c478bd9Sstevel@tonic-gate ** < 0 on failure.
108*7c478bd9Sstevel@tonic-gate */
109*7c478bd9Sstevel@tonic-gate
110*7c478bd9Sstevel@tonic-gate int shmbig __P((bool, int));
111*7c478bd9Sstevel@tonic-gate
112*7c478bd9Sstevel@tonic-gate int
shmbig(owner,size)113*7c478bd9Sstevel@tonic-gate shmbig(owner, size)
114*7c478bd9Sstevel@tonic-gate bool owner;
115*7c478bd9Sstevel@tonic-gate int size;
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate int *shm, shmid;
118*7c478bd9Sstevel@tonic-gate int i;
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate shm = (int *) sm_shmstart(T_SHMKEY, size, 0, &shmid, owner);
121*7c478bd9Sstevel@tonic-gate if (shm == (int *) 0)
122*7c478bd9Sstevel@tonic-gate {
123*7c478bd9Sstevel@tonic-gate perror("shminit failed");
124*7c478bd9Sstevel@tonic-gate return -1;
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate
127*7c478bd9Sstevel@tonic-gate for (i = 0; i < size / sizeof(int); i++)
128*7c478bd9Sstevel@tonic-gate shm[i] = i;
129*7c478bd9Sstevel@tonic-gate for (i = 0; i < size / sizeof(int); i++)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate if (shm[i] != i)
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate fprintf(stderr, "failed at %d: %d", i, shm[i]);
134*7c478bd9Sstevel@tonic-gate }
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate return sm_shmstop((void *) shm, shmid, owner);
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate /*
142*7c478bd9Sstevel@tonic-gate ** SHMTEST -- test of shared memory
143*7c478bd9Sstevel@tonic-gate **
144*7c478bd9Sstevel@tonic-gate ** Parameters:
145*7c478bd9Sstevel@tonic-gate ** owner -- create segment.
146*7c478bd9Sstevel@tonic-gate **
147*7c478bd9Sstevel@tonic-gate ** Returns:
148*7c478bd9Sstevel@tonic-gate ** 0 on success
149*7c478bd9Sstevel@tonic-gate ** < 0 on failure.
150*7c478bd9Sstevel@tonic-gate */
151*7c478bd9Sstevel@tonic-gate
152*7c478bd9Sstevel@tonic-gate # define MAX_CNT 10
153*7c478bd9Sstevel@tonic-gate
154*7c478bd9Sstevel@tonic-gate int shmtest __P((int));
155*7c478bd9Sstevel@tonic-gate
156*7c478bd9Sstevel@tonic-gate int
shmtest(owner)157*7c478bd9Sstevel@tonic-gate shmtest(owner)
158*7c478bd9Sstevel@tonic-gate int owner;
159*7c478bd9Sstevel@tonic-gate {
160*7c478bd9Sstevel@tonic-gate int *shm, shmid;
161*7c478bd9Sstevel@tonic-gate int cnt = 0;
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate shm = (int *) sm_shmstart(T_SHMKEY, SHMSIZE, 0, &shmid, owner);
164*7c478bd9Sstevel@tonic-gate if (shm == (int *) 0)
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate perror("shminit failed");
167*7c478bd9Sstevel@tonic-gate return -1;
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate
170*7c478bd9Sstevel@tonic-gate if (owner)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate int r;
173*7c478bd9Sstevel@tonic-gate
174*7c478bd9Sstevel@tonic-gate r = sm_shmsetowner(shmid, getuid(), getgid(), 0660);
175*7c478bd9Sstevel@tonic-gate SM_TEST(r == 0);
176*7c478bd9Sstevel@tonic-gate *shm = 1;
177*7c478bd9Sstevel@tonic-gate while (*shm == 1 && cnt++ < MAX_CNT)
178*7c478bd9Sstevel@tonic-gate sleep(1);
179*7c478bd9Sstevel@tonic-gate SM_TEST(cnt <= MAX_CNT);
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate /* release and re-acquire the segment */
182*7c478bd9Sstevel@tonic-gate r = sm_shmstop((void *) shm, shmid, owner);
183*7c478bd9Sstevel@tonic-gate SM_TEST(r == 0);
184*7c478bd9Sstevel@tonic-gate shm = (int *) sm_shmstart(T_SHMKEY, SHMSIZE, 0, &shmid, owner);
185*7c478bd9Sstevel@tonic-gate SM_TEST(shm != (int *) 0);
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate else
188*7c478bd9Sstevel@tonic-gate {
189*7c478bd9Sstevel@tonic-gate while (*shm != 1 && cnt++ < MAX_CNT)
190*7c478bd9Sstevel@tonic-gate sleep(1);
191*7c478bd9Sstevel@tonic-gate SM_TEST(cnt <= MAX_CNT);
192*7c478bd9Sstevel@tonic-gate *shm = 2;
193*7c478bd9Sstevel@tonic-gate
194*7c478bd9Sstevel@tonic-gate /* wait a momemt so the segment is still in use */
195*7c478bd9Sstevel@tonic-gate sleep(2);
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate return sm_shmstop((void *) shm, shmid, owner);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate
200*7c478bd9Sstevel@tonic-gate int
main(argc,argv)201*7c478bd9Sstevel@tonic-gate main(argc, argv)
202*7c478bd9Sstevel@tonic-gate int argc;
203*7c478bd9Sstevel@tonic-gate char *argv[];
204*7c478bd9Sstevel@tonic-gate {
205*7c478bd9Sstevel@tonic-gate bool interactive = false;
206*7c478bd9Sstevel@tonic-gate bool owner = false;
207*7c478bd9Sstevel@tonic-gate int big = -1;
208*7c478bd9Sstevel@tonic-gate int ch;
209*7c478bd9Sstevel@tonic-gate int r = 0;
210*7c478bd9Sstevel@tonic-gate int status;
211*7c478bd9Sstevel@tonic-gate extern char *optarg;
212*7c478bd9Sstevel@tonic-gate
213*7c478bd9Sstevel@tonic-gate # define OPTIONS "b:io"
214*7c478bd9Sstevel@tonic-gate while ((ch = getopt(argc, argv, OPTIONS)) != -1)
215*7c478bd9Sstevel@tonic-gate {
216*7c478bd9Sstevel@tonic-gate switch ((char) ch)
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate case 'b':
219*7c478bd9Sstevel@tonic-gate big = atoi(optarg);
220*7c478bd9Sstevel@tonic-gate break;
221*7c478bd9Sstevel@tonic-gate
222*7c478bd9Sstevel@tonic-gate case 'i':
223*7c478bd9Sstevel@tonic-gate interactive = true;
224*7c478bd9Sstevel@tonic-gate break;
225*7c478bd9Sstevel@tonic-gate
226*7c478bd9Sstevel@tonic-gate case 'o':
227*7c478bd9Sstevel@tonic-gate owner = true;
228*7c478bd9Sstevel@tonic-gate break;
229*7c478bd9Sstevel@tonic-gate
230*7c478bd9Sstevel@tonic-gate default:
231*7c478bd9Sstevel@tonic-gate break;
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate
235*7c478bd9Sstevel@tonic-gate if (interactive)
236*7c478bd9Sstevel@tonic-gate r = shminter(owner);
237*7c478bd9Sstevel@tonic-gate else if (big > 0)
238*7c478bd9Sstevel@tonic-gate r = shmbig(true, big);
239*7c478bd9Sstevel@tonic-gate else
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate pid_t pid;
242*7c478bd9Sstevel@tonic-gate extern int SmTestNumErrors;
243*7c478bd9Sstevel@tonic-gate
244*7c478bd9Sstevel@tonic-gate if ((pid = fork()) < 0)
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate perror("fork failed\n");
247*7c478bd9Sstevel@tonic-gate return -1;
248*7c478bd9Sstevel@tonic-gate }
249*7c478bd9Sstevel@tonic-gate
250*7c478bd9Sstevel@tonic-gate sm_test_begin(argc, argv, "test shared memory");
251*7c478bd9Sstevel@tonic-gate if (pid == 0)
252*7c478bd9Sstevel@tonic-gate {
253*7c478bd9Sstevel@tonic-gate /* give the parent the chance to setup data */
254*7c478bd9Sstevel@tonic-gate sleep(1);
255*7c478bd9Sstevel@tonic-gate r = shmtest(false);
256*7c478bd9Sstevel@tonic-gate }
257*7c478bd9Sstevel@tonic-gate else
258*7c478bd9Sstevel@tonic-gate {
259*7c478bd9Sstevel@tonic-gate r = shmtest(true);
260*7c478bd9Sstevel@tonic-gate (void) wait(&status);
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate SM_TEST(r == 0);
263*7c478bd9Sstevel@tonic-gate if (SmTestNumErrors > 0)
264*7c478bd9Sstevel@tonic-gate printf("add -DSM_CONF_SHM=0 to confENVDEF in devtools/Site/site.config.m4\nand start over.\n");
265*7c478bd9Sstevel@tonic-gate return sm_test_end();
266*7c478bd9Sstevel@tonic-gate }
267*7c478bd9Sstevel@tonic-gate return r;
268*7c478bd9Sstevel@tonic-gate }
269*7c478bd9Sstevel@tonic-gate #else /* SM_CONF_SHM */
270*7c478bd9Sstevel@tonic-gate int
271*7c478bd9Sstevel@tonic-gate main(argc, argv)
272*7c478bd9Sstevel@tonic-gate int argc;
273*7c478bd9Sstevel@tonic-gate char *argv[];
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate printf("No support for shared memory configured on this machine\n");
276*7c478bd9Sstevel@tonic-gate return 0;
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SHM */
279