shmticklib.c revision 64562
164562Sgshapiro/* 264562Sgshapiro * Copyright (c) 1999-2000 Sendmail, Inc. and its suppliers. 364562Sgshapiro * All rights reserved. 464562Sgshapiro * 564562Sgshapiro * By using this file, you agree to the terms and conditions set 664562Sgshapiro * forth in the LICENSE file which can be found at the top level of 764562Sgshapiro * the sendmail distribution. 864562Sgshapiro * 964562Sgshapiro * Contributed by Exactis.com, Inc. 1064562Sgshapiro * 1164562Sgshapiro */ 1264562Sgshapiro 1364562Sgshapiro#ifndef lint 1464562Sgshapirostatic char id[] = "@(#)$Id: shmticklib.c,v 8.6 2000/02/26 01:32:27 gshapiro Exp $"; 1564562Sgshapiro#endif /* ! lint */ 1664562Sgshapiro 1764562Sgshapiro#if _FFR_SHM_STATUS 1864562Sgshapiro# if SFIO 1964562Sgshapiro# include <sfio/stdio.h> 2064562Sgshapiro# else /* !SFIO */ 2164562Sgshapiro# include <stdio.h> 2264562Sgshapiro# endif /* SFIO */ 2364562Sgshapiro# include <sys/types.h> 2464562Sgshapiro# include <sys/ipc.h> 2564562Sgshapiro# include <sys/shm.h> 2664562Sgshapiro 2764562Sgshapiro# include "statusd_shm.h" 2864562Sgshapiro 2964562Sgshapiro/* 3064562Sgshapiro** SHMTICK -- increment a shared memory variable 3164562Sgshapiro** 3264562Sgshapiro** Parameters: 3364562Sgshapiro** inc_me -- identity of shared memory segment 3464562Sgshapiro** what -- which variable to increment 3564562Sgshapiro** 3664562Sgshapiro** Returns: 3764562Sgshapiro** none 3864562Sgshapiro*/ 3964562Sgshapiro 4064562Sgshapirovoid 4164562Sgshapiroshmtick(inc_me, what) 4264562Sgshapiro int inc_me; 4364562Sgshapiro int what; 4464562Sgshapiro{ 4564562Sgshapiro static int shmid = -1; 4664562Sgshapiro static STATUSD_SHM *sp = (STATUSD_SHM *)-1; 4764562Sgshapiro static unsigned int cookie = 0; 4864562Sgshapiro 4964562Sgshapiro if (shmid < 0) 5064562Sgshapiro { 5164562Sgshapiro int size = sizeof(STATUSD_SHM); 5264562Sgshapiro 5364562Sgshapiro shmid = shmget(STATUSD_SHM_KEY, size, 0); 5464562Sgshapiro if (shmid < 0) 5564562Sgshapiro return; 5664562Sgshapiro } 5764562Sgshapiro if ((unsigned long *)sp == (unsigned long *)-1) 5864562Sgshapiro { 5964562Sgshapiro sp = (STATUSD_SHM *)shmat(shmid, NULL, 0); 6064562Sgshapiro if ((unsigned long *)sp == (unsigned long *)-1) 6164562Sgshapiro return; 6264562Sgshapiro } 6364562Sgshapiro if (sp->magic != STATUSD_MAGIC) 6464562Sgshapiro { 6564562Sgshapiro /* 6664562Sgshapiro ** possible race condition, wait for 6764562Sgshapiro ** statusd to initialize. 6864562Sgshapiro */ 6964562Sgshapiro 7064562Sgshapiro return; 7164562Sgshapiro } 7264562Sgshapiro if (what >= STATUSD_LONGS) 7364562Sgshapiro what = STATUSD_LONGS - 1; 7464562Sgshapiro if (inc_me >= STATUSD_LONGS) 7564562Sgshapiro inc_me = STATUSD_LONGS - 1; 7664562Sgshapiro 7764562Sgshapiro if (sp->ul[STATUSD_COOKIE] != cookie) 7864562Sgshapiro { 7964562Sgshapiro cookie = sp->ul[STATUSD_COOKIE]; 8064562Sgshapiro ++(sp->ul[inc_me]); 8164562Sgshapiro } 8264562Sgshapiro ++(sp->ul[what]); 8364562Sgshapiro} 8464562Sgshapiro#endif /* _FFR_SHM_STATUS */ 85