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