Adding signal handler to remove shared mem and semaphore
[gapil.git] / sources / message_getter.c
1 /* message_getter.c
2  * 
3  * Copyright (C) 2010 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /*****************************************************************************
20  *
21  * File message_setter: Get amd print a message in a shared memory segment,
22  * protecting it with a semaphore. 
23  *
24  * Author: S. Piccardi Jan. 2010
25  *
26  *****************************************************************************/
27 #include <sys/types.h>   /* primitive system data types */
28 #include <sys/stat.h>    /* file characteristics constants and functions */
29 #include <dirent.h>      /* directory operation constants and functions */  
30 #include <unistd.h>      /* unix standard library */
31 #include <stdlib.h>      /* C standard library */
32 #include <string.h>      /* C strings library */
33 #include <time.h>
34
35 #include <semaphore.h>
36 #include "Gapil.h"
37 /*
38  * Program message_setter
39  *
40  * List files and their size inside a given directory
41  */
42 /* Help printing routine */
43 void usage(void);
44 void HandSigInt(int sig);
45
46 #define MSGMAXSIZE 256
47 char *shmname = "messages";
48 char *semname = "messages";
49
50 int main(int argc, char *argv[]) 
51 {
52     int i;
53     sem_t * sem;
54     time_t t;
55     char * res;
56     void * shm_ptr;
57     /*
58      * Input section: decode command line parameters 
59      * Use getopt function
60      */
61     opterr = 0;  /* don't want writing to stderr */
62     while ( (i = getopt(argc, argv, "hf:s:")) != -1) {
63         switch (i) {
64         /* 
65          * Handling options 
66          */ 
67         case 'h':                                            /* help option */
68             printf("Wrong -h option use\n");
69             usage();
70             return -1;
71             break;
72         case 'f':
73             shmname = optarg;
74             break;
75         case 's':
76             semname = optarg;
77             break;
78         case '?':                                    /* unrecognized options */
79             printf("Unrecognized options -%c\n",optopt);
80             usage();
81         default:                                       /* should not reached */
82             usage();
83         }
84     }
85     /* ***********************************************************
86      * 
87      *           Options processing completed
88      *
89      *                Main code beginning
90      * 
91      * ***********************************************************/
92     if ((argc - optind) != 1) {          /* There must be remaing parameters */
93         printf("Wrong number of arguments %d\n", argc - optind);
94         usage();
95     }
96     Signal(SIGINT, HandSigInt);
97     // Get shared memory segment
98     shm_ptr = CreateShm(shmname, MSGMAXSIZE, 0666, 0);
99     if ( shm_ptr == NULL) {
100         perror("Cannot find shared memory");
101         exit(1);
102     }
103     // set initial string
104     strncpy((char *) shm_ptr, argv[optind], MSGMAXSIZE);
105     // open the semaphore or create it locked
106     if ( (sem = sem_open(semname, O_CREAT, 0666, 0)) == SEM_FAILED ) {
107         perror("Cannot open semaphore");
108         exit(1);
109     }
110     // check if first time creation, and unlock
111     if ( sem_getvalue(sem, &i) != 0) {
112         perror("cannot get initial semaphore value");
113         exit(1);
114     } else {
115         if (i == 0) {
116             if ( sem_post(sem) != 0) {
117                 perror("cannot do semaphore initial release");
118                 exit(1);
119             }
120         }
121     }   
122     while(1) {
123         // acquire semaphore
124         if ( sem_wait(sem) != 0) {
125             perror("cannot use semaphore");
126             exit(1);
127         }
128         t = time(NULL);
129         printf("%s", ctime(&t));
130         sem_getvalue(sem, &i);
131         printf("sem=%i,  ", i);
132         printf("message: %s\n", (char *) shm_ptr );
133         if ( sem_post(sem) != 0) {
134             perror("cannot release semaphore");
135             exit(1);
136         }
137         sleep(1);
138    }
139 exit(0);    
140 }
141
142 /*
143  * routine to print usage info and exit
144  */
145 void usage(void) {
146     printf("Program message_setter: put a message in shared memory segment \n");
147     printf("Usage:\n");
148     printf("  message_setter [-h] [-f shmname]  message \n");
149     printf("  -h           print this help\n");
150     printf("  -f shmname   use shmname memory segment\n");
151     printf("  -s semname   use semname semaphore\n");
152     exit(1);
153 }
154
155 void HandSigInt(int sig)
156 {
157     RemoveShm(shmname);
158     sem_unlink(semname);
159     exit(0);
160 }