summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/src/shmticklib.c
blob: 6f5e301d2c708f42a8032d47db7bad313b8ba895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (c) 1999-2000 Sendmail, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 *
 * Contributed by Exactis.com, Inc.
 *
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Id: shmticklib.c,v 8.14 2001/09/11 04:05:16 gshapiro Exp $")

#if _FFR_SHM_STATUS
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/shm.h>

# include "statusd_shm.h"

/*
**  SHMTICK -- increment a shared memory variable
**
**	Parameters:
**		inc_me -- identity of shared memory segment
**		what -- which variable to increment
**
**	Returns:
**		none
*/

void
shmtick(inc_me, what)
	int inc_me;
	int what;
{
	static int shmid = -1;
	static STATUSD_SHM *sp = (STATUSD_SHM *)-1;
	static unsigned int cookie = 0;

	if (shmid < 0)
	{
		int size = sizeof(STATUSD_SHM);

		shmid = shmget(STATUSD_SHM_KEY, size, 0);
		if (shmid < 0)
			return;
	}
	if ((unsigned long *) sp == (unsigned long *)-1)
	{
		sp = (STATUSD_SHM *) shmat(shmid, NULL, 0);
		if ((unsigned long *) sp == (unsigned long *) -1)
			return;
	}
	if (sp->magic != STATUSD_MAGIC)
	{
		/*
		**  possible race condition, wait for
		**  statusd to initialize.
		*/

		return;
	}
	if (what >= STATUSD_LONGS)
		what = STATUSD_LONGS - 1;
	if (inc_me >= STATUSD_LONGS)
		inc_me = STATUSD_LONGS - 1;

	if (sp->ul[STATUSD_COOKIE] != cookie)
	{
		cookie = sp->ul[STATUSD_COOKIE];
		++(sp->ul[inc_me]);
	}
	++(sp->ul[what]);
}
#endif /* _FFR_SHM_STATUS */
OpenPOWER on IntegriCloud