Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / IPCTestId.c
1 /* IPCTestId.c
2  * 
3  * Copyright (C) 2001 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  * Program IPCTestId.c: 
22  * Program to test IPC identifiers
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
27  * Usage: ipctestid -h give all info's
28  *
29  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
33 #include <errno.h>       /* error definitions and routines */ 
34 #include <stdlib.h>      /* C standard library */
35 #include <unistd.h>      /* unix standard library */
36 #include <stdio.h>       /* standard I/O library */
37 #include <string.h>      /* C strings library */
38 #include <sys/ipc.h>     /* SysV IPC functions */
39 #include <sys/msg.h>     /* SysV message queues */
40 #include <sys/sem.h>     /* SysV semaphores */
41 #include <sys/shm.h>     /* SysV shared memory */
42
43 #include "macros.h"      /* My macros */
44 /* Help printing routine */
45 void usage(void);
46
47 int main(int argc, char *argv[])
48 {
49 /* 
50  * Variables definition  
51  */
52     int i; 
53     int n = 3;                                      /* default is 3 times */
54     char type='q';                                  /* default is use queues */
55     int id;
56     /*
57      * Input section: decode command line parameters 
58      * Use getopt function
59      */
60     opterr = 0;  /* don't want writing to stderr */
61     while ( (i = getopt(argc, argv, "hqsmn:")) != -1) {
62         switch (i) {
63         /* 
64          * Handling options 
65          */ 
66         case 'h':   /* help option */
67             printf("Wrong -h option use\n");
68             usage();
69             return -1;
70             break;
71         case 'q':   /* Message Queue */
72             debug("Message Queue\n");
73             type = i;
74             break;
75         case 's':   /* Semaphore */
76             debug("Semaphore\n");
77             type = i;
78             break;
79         case 'm':   /* Shared Memory */
80             debug("Shared Memory\n");
81             type = i;
82             break;
83         case 'n':   /* Number of tryes */
84             debug("Number of tryes\n");
85             n = strtol(optarg, NULL, 10);
86             break;
87         default:    /* should not reached */
88             usage();
89         }
90     }
91     /* ***********************************************************
92      * 
93      *           Options processing completed
94      *
95      *                Main code beginning
96      * 
97      * ***********************************************************/
98     switch (type) {
99     case 'q':   /* Message Queue */
100         debug("Message Queue Try\n");
101         for (i=0; i<n; i++) {
102             id = msgget(IPC_PRIVATE, IPC_CREAT|0666);
103             printf("Identifier Value %d \n", id);
104             msgctl(id, IPC_RMID, NULL);
105         }
106         break;
107     case 's':   /* Semaphore */
108         debug("Semaphore\n");
109         for (i=0; i<n; i++) {
110             id = semget(IPC_PRIVATE, 1, IPC_CREAT|0666);
111             printf("Identifier Value %d \n", id);
112             semctl(id, 0, IPC_RMID);
113         }
114         break;
115     case 'm':   /* Shared Memory */
116         debug("Shared Memory\n");
117         for (i=0; i<n; i++) {
118             id = shmget(IPC_PRIVATE, 1000, IPC_CREAT|0666);
119             printf("Identifier Value %d \n", id);
120             shmctl(id, IPC_RMID, NULL);
121         }
122         break;
123     default:    /* should not reached */
124         return -1;
125     }
126     return 0;
127 }
128 /*
129  * routine to print usage info and exit
130  */
131 void usage(void) {
132     printf("Program ipctestid : test IPC identifier number assignment\n");
133     printf("Usage:\n");
134     printf("  ipctestid [-h] [-qsm] [-n N] \n");
135     printf("  -h     print this help\n");
136     printf("  -q     try for message queue identifiers\n");
137     printf("  -s     try for semaphore identifiers\n");
138     printf("  -m     try for shared memory identifiers\n");
139     printf("  -n XX  try XX times\n");
140     
141     exit(1);
142 }
143