allocreporter.c (2474B)
1 /************************************************************************* 2 * This file is part of Crosroads 1.23, a load balancer and fail over 3 * utility for TCP. Copyright (c) Karel Kubat, distributed under GPL. 4 * Visit http://crossroads.e-tunity.com for information. 5 *************************************************************************/ 6 #include "crossroads.h" 7 8 void alloc_reporter (Service *s, int first) { 9 int shmid; 10 struct shmid_ds shmbuf; 11 12 /* Get an ID to work on, depending on the create mode. 13 Get a corresponding semaphore too. 14 */ 15 msg ("Obtaining reporting memory for service %s (key %d)", 16 s->name, s->shmkey); 17 18 if (first) { 19 /* First time 'round. We're allocating for the first time here. */ 20 if ( (shmid = shmget (s->shmkey, sizeof(Servicereport), 21 0644 | IPC_CREAT) ) >= 0) 22 msg ("Created new shm at id %d", shmid); 23 else 24 error ("Cannot create shared memory for service %s (key %d: %s)", 25 s->name, s->shmkey, strerror(errno)); 26 27 /* Make sure that we're the owner of the segment. */ 28 if (shmctl (shmid, IPC_STAT, &shmbuf) == -1) 29 error ("Failed to get status of shared memory: %s", 30 strerror(errno)); 31 shmbuf.shm_perm.uid = getuid(); 32 shmbuf.shm_perm.gid = getgid(); 33 if (shmctl (shmid, IPC_SET, &shmbuf) == -1) 34 error ("Failed to set status of shared memory: %s", 35 strerror(errno)); 36 37 if ( (semid = semget (s->shmkey, 1, 0644 | IPC_CREAT)) >= 0 ) 38 msg ("Created new sem at id %d", semid); 39 else 40 error ("Cannot create semaphore for service %s (key %d: %s)", 41 s->name, s->shmkey, strerror(errno)); 42 } else { 43 /* Second time 'round. We're getting a handle on previously 44 * created shared memory and semaphores. */ 45 if ( (shmid = shmget (s->shmkey, sizeof(Servicereport), 46 0644) ) >= 0) 47 msg ("Got existing shm at id %d", shmid); 48 else 49 error ("Cannot get shared memory for service %s, " 50 "key %d: %s. " 51 "Maybe crossroads isn't running, or the configuration " 52 "has changed.", 53 s->name, s->shmkey, strerror(errno)); 54 if ( (semid = semget (s->shmkey, 1, 0644)) >= 0 ) 55 msg ("Got existing sem at id %d", semid); 56 else 57 error ("Cannot get semaphore for service %s (key %d: %s)", 58 s->name, s->shmkey, strerror(errno)); 59 } 60 61 /* Attach to our memory segment. */ 62 if ( ((servicereport = shmat (shmid, 0, 0))) == (void *) -1 ) 63 error ("Cannot attach shared memory for service %s: %s", 64 s->name, strerror(errno)); 65 }