1 /* 2 * Implement the SYSV ipcrm command. 3 * 4 * $Id: ipcrm.c,v 1.1 1993/11/14 16:28:00 cgd Exp $ 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <limits.h> 10 #include <sys/types.h> 11 #include <sys/ipc.h> 12 #include <sys/sem.h> 13 #include <sys/msg.h> 14 #include <sys/shm.h> 15 #include <errno.h> 16 17 #define OMIT_SHM /* don't handle shm stuff; untested */ 18 19 int 20 getnumber(char *num, long *lptr) 21 { 22 char *end; 23 24 if (num == NULL) { 25 fprintf(stderr, "ipcrm: missing numeric parameter\n"); 26 return (0); 27 } 28 29 *lptr = strtol(num, &end, 0); 30 if (*num == '\0' || *end != '\0') { 31 fprintf(stderr, "ipcrm: can't convert \"%s\" to an integer\n", 32 num); 33 return (0); 34 } 35 if (*lptr == LONG_MAX || *lptr == LONG_MIN) { 36 fprintf(stderr, "ipcrm: absurd numeric parameter \"%s\"\n", 37 num); 38 return (0); 39 } 40 return (1); 41 } 42 43 main(int argc, char **argv) 44 { 45 char *parm; 46 char tbuf[10000]; 47 int errcnt = 0; 48 long id; 49 long key; 50 51 while ((parm = *++argv) != NULL) { 52 53 if (strcmp(parm, "-q") == 0) { 54 if (getnumber(*++argv, &id)) { 55 if (msgctl(id, IPC_RMID, NULL) == 0) { 56 printf("msqid %s deleted\n", *argv); 57 } else if (errno == ENOSYS) { 58 fprintf(stderr, "ipcrm: SYSV message passing not configured in kernel\n"); 59 errcnt += 1; 60 } else { 61 strcpy(tbuf, "can't remove msqid "); 62 strcat(tbuf, *argv); 63 perror(tbuf); 64 errcnt += 1; 65 } 66 } else { 67 errcnt += 1; 68 } 69 } else if (strcmp(parm, "-m") == 0) { 70 if (getnumber(*++argv, &id)) { 71 #ifndef OMIT_SHM 72 %%%untested %%% 73 if (shmctl(id, IPC_RMID, NULL) == 0) { 74 printf("shmid %s deleted\n", *argv); 75 } else if (errno == ENOSYS) { 76 fprintf(stderr, "ipcrm: SYSV shared memory not configured in kernel\n"); 77 errcnt += 1; 78 } else { 79 strcpy(tbuf, "can't remove shmid "); 80 strcat(tbuf, *argv); 81 perror(tbuf); 82 errcnt += 1; 83 } 84 #else 85 fprintf(stderr, "ipcrm: sorry, this version of ipcrm doesn't support shared memory\n"); 86 errcnt += 1; 87 #endif 88 } else { 89 errcnt += 1; 90 } 91 } else if (strcmp(parm, "-s") == 0) { 92 if (getnumber(*++argv, &id)) { 93 union semun junk; 94 95 if (semctl(id, 0, IPC_RMID, junk) == 0) { 96 printf("semid %s deleted\n", *argv); 97 } else if (errno == ENOSYS) { 98 fprintf(stderr, "ipcrm: SYSV semaphores not configured in kernel\n"); 99 errcnt += 1; 100 } else { 101 strcpy(tbuf, "can't remove semid "); 102 strcat(tbuf, *argv); 103 perror(tbuf); 104 errcnt += 1; 105 } 106 } else { 107 errcnt += 1; 108 } 109 } else if (strcmp(parm, "-Q") == 0) { 110 if (getnumber(*++argv, &key)) { 111 if (key == IPC_PRIVATE) { 112 fprintf(stderr, "ipcrm: can't remove private message queues\n"); 113 } else { 114 strcpy(tbuf, "can't access msq key "); 115 strcat(tbuf, *argv); 116 if ((id = msgget(key, 0000)) >= 0) { 117 strcpy(tbuf, 118 "can't remove msq key "); 119 strcat(tbuf, *argv); 120 if (msgctl(id, IPC_RMID, NULL) 121 == 0) { 122 printf("msq key %s deleted\n", 123 *argv); 124 } else { 125 perror(tbuf); 126 errcnt += 1; 127 } 128 } else if (errno == ENOSYS) { 129 fprintf(stderr, "ipcrm: SYSV message passing not configured in kernel\n"); 130 errcnt += 1; 131 } else { 132 perror(tbuf); 133 errcnt += 1; 134 } 135 } 136 } else { 137 errcnt += 1; 138 } 139 } else if (strcmp(parm, "-M") == 0) { 140 if (getnumber(*++argv, &key)) { 141 #ifndef OMIT_SHM 142 %%%untested %%% 143 if (key == IPC_PRIVATE) { 144 fprintf(stderr, "ipcrm: can't remove private shared memory segments\n"); 145 } else { 146 strcpy(tbuf, "can't access shm key "); 147 strcat(tbuf, *argv); 148 if ((id = shmget(key, 0, 0000)) >= 0) { 149 strcpy(tbuf, "can't remove shm key "); 150 strcat(tbuf, *argv); 151 if (shmctl(id, IPC_RMID, NULL) == 0) { 152 printf("shm key %s deleted\n", 153 *argv); 154 } else { 155 perror(tbuf); 156 errcnt += 1; 157 } 158 } else if (errno == ENOSYS) { 159 fprintf(stderr, "ipcrm: SYSV shared memory not configured in kernel\n"); 160 errcnt += 1; 161 } else { 162 perror(tbuf); 163 errcnt += 1; 164 } 165 } 166 #else 167 fprintf(stderr, "ipcrm: sorry, this version of ipcrm doesn't support shared memory\n"); 168 errcnt += 1; 169 #endif 170 } else { 171 errcnt += 1; 172 } 173 } else if (strcmp(parm, "-S") == 0) { 174 if (getnumber(*++argv, &key)) { 175 if (key == IPC_PRIVATE) { 176 fprintf(stderr, "ipcrm: can't remove private semaphores\n"); 177 } else { 178 strcpy(tbuf, "can't access sem key "); 179 strcat(tbuf, *argv); 180 if ((id = semget(key, 0, 0000)) >= 0) { 181 union semun junk; 182 183 strcpy(tbuf, "can't remove sem key "); 184 strcat(tbuf, *argv); 185 if (semctl(id, 0, IPC_RMID, junk) == 0) { 186 printf("sem key %s deleted\n", *argv); 187 } else { 188 perror(tbuf); 189 errcnt += 1; 190 } 191 } else if (errno == ENOSYS) { 192 fprintf(stderr, "ipcrm: SYSV semaphores not configured in kernel\n"); 193 errcnt += 1; 194 } else { 195 perror(tbuf); 196 errcnt += 1; 197 } 198 } 199 } else { 200 errcnt += 1; 201 } 202 } else { 203 fprintf(stderr, "ipcrm: illegal parameter \"%s\" - bye!\n", parm); 204 exit(1); 205 } 206 207 } 208 209 if (errcnt == 0) { 210 exit(0); 211 } else { 212 exit(1); 213 } 214 } 215