xref: /freebsd/contrib/sendmail/src/shmticklib.c (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1 /*
2  * Copyright (c) 1999-2000 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  * Contributed by Exactis.com, Inc.
10  *
11  */
12 
13 #include <sm/gen.h>
14 SM_RCSID("@(#)$Id: shmticklib.c,v 8.15 2013-11-22 20:51:56 ca Exp $")
15 
16 #if _FFR_SHM_STATUS
17 # include <sys/types.h>
18 # include <sys/ipc.h>
19 # include <sys/shm.h>
20 
21 # include "statusd_shm.h"
22 
23 /*
24 **  SHMTICK -- increment a shared memory variable
25 **
26 **	Parameters:
27 **		inc_me -- identity of shared memory segment
28 **		what -- which variable to increment
29 **
30 **	Returns:
31 **		none
32 */
33 
34 void
35 shmtick(inc_me, what)
36 	int inc_me;
37 	int what;
38 {
39 	static int shmid = -1;
40 	static STATUSD_SHM *sp = (STATUSD_SHM *)-1;
41 	static unsigned int cookie = 0;
42 
43 	if (shmid < 0)
44 	{
45 		int size = sizeof(STATUSD_SHM);
46 
47 		shmid = shmget(STATUSD_SHM_KEY, size, 0);
48 		if (shmid < 0)
49 			return;
50 	}
51 	if ((unsigned long *) sp == (unsigned long *)-1)
52 	{
53 		sp = (STATUSD_SHM *) shmat(shmid, NULL, 0);
54 		if ((unsigned long *) sp == (unsigned long *) -1)
55 			return;
56 	}
57 	if (sp->magic != STATUSD_MAGIC)
58 	{
59 		/*
60 		**  possible race condition, wait for
61 		**  statusd to initialize.
62 		*/
63 
64 		return;
65 	}
66 	if (what >= STATUSD_LONGS)
67 		what = STATUSD_LONGS - 1;
68 	if (inc_me >= STATUSD_LONGS)
69 		inc_me = STATUSD_LONGS - 1;
70 
71 	if (sp->ul[STATUSD_COOKIE] != cookie)
72 	{
73 		cookie = sp->ul[STATUSD_COOKIE];
74 		++(sp->ul[inc_me]);
75 	}
76 	++(sp->ul[what]);
77 }
78 #endif /* _FFR_SHM_STATUS */
79