Home | History | Annotate | Download | only in pending
      1 /* ipcrm.c - remove msg que, sem or shared memory
      2  *
      3  * Copyright 2014 Ashwini Kumar <ak.ashwini1981 (at) gmail.com>
      4  *
      5  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ipcrm.html
      6 
      7 
      8 USE_IPCRM(NEWTOY(ipcrm, "m*M*s*S*q*Q*", TOYFLAG_USR|TOYFLAG_BIN))
      9 
     10 config IPCRM
     11   bool "ipcrm"
     12   default n
     13   help
     14     usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]
     15               [-Q msgkey] [-M shmkey] [-S semkey] ... ]
     16 
     17     -mM Remove memory segment after last detach
     18     -qQ Remove message queue
     19     -sS Remove semaphore
     20 */
     21 
     22 #define FOR_ipcrm
     23 #include "toys.h"
     24 #include <sys/ipc.h>
     25 #include <sys/shm.h>
     26 #include <sys/sem.h>
     27 #include <sys/msg.h>
     28 
     29 GLOBALS(
     30   struct arg_list *qkey;
     31   struct arg_list *qid;
     32   struct arg_list *skey;
     33   struct arg_list *sid;
     34   struct arg_list *mkey;
     35   struct arg_list *mid;
     36 )
     37 
     38 static void do_ipcrm(int key, int ipc, char *name)
     39 {
     40   char *c;
     41   int id, ret = 0;
     42 
     43   id = strtol(name, &c, 0);
     44   if (*c) {
     45     error_msg("invalid number :%s", name);
     46     return;
     47   }
     48 
     49   if (key) {
     50     if (id == IPC_PRIVATE) {
     51       error_msg("illegal key (%s)", name);
     52       return;
     53     }
     54     id = ((ipc == 1)?shmget(id, 0, 0) :
     55          (ipc == 2)? msgget(id, 0): semget(id, 0, 0));
     56     if (id < 0) {
     57       perror_msg("key (%s)", name);
     58       return;
     59     }
     60   }
     61 
     62   if (ipc == 1) ret = shmctl(id, IPC_RMID, NULL);
     63   else if (ipc == 2) ret = msgctl(id, IPC_RMID, NULL);
     64   else if (ipc == 3) ret = semctl(id, 0, IPC_RMID, NULL);
     65 
     66   if (ret < 0) perror_msg("%s (%s)", ((key)? "key": "id"), name);
     67 }
     68 
     69 void ipcrm_main(void)
     70 {
     71   ++toys.argv;
     72   if (toys.optc && (!strcmp(*toys.argv, "shm") ||
     73         !strcmp(*toys.argv, "sem") || !strcmp(*toys.argv, "msg"))) {
     74     int t = (toys.argv[0][1] == 'h')? 1 : (toys.argv[0][1] == 's')? 2:3;
     75 
     76     while (*(++toys.argv)) do_ipcrm(0, t, *toys.argv);
     77   } else {
     78     struct arg_list *tmp;
     79 
     80     for (tmp = TT.mkey; tmp; tmp = tmp->next) do_ipcrm(1, 1, tmp->arg);
     81     for (tmp = TT.mid; tmp; tmp = tmp->next) do_ipcrm(0, 1, tmp->arg);
     82     for (tmp = TT.qkey; tmp; tmp = tmp->next) do_ipcrm(1, 2, tmp->arg);
     83     for (tmp = TT.qid; tmp; tmp = tmp->next) do_ipcrm(0, 2, tmp->arg);
     84     for (tmp = TT.skey; tmp; tmp = tmp->next) do_ipcrm(1, 3, tmp->arg);
     85     for (tmp = TT.sid; tmp; tmp = tmp->next) do_ipcrm(0, 3, tmp->arg);
     86     if (toys.optc) help_exit("unknown argument: %s", *toys.optargs);
     87   }
     88 }
     89