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