xref: /illumos-gate/usr/src/cmd/ipcrm/ipcrm.c (revision 7c499da3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c499da3SKeerthi Kondaka  * Common Development and Distribution License (the "License").
6*7c499da3SKeerthi Kondaka  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*7c499da3SKeerthi Kondaka  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * ipcrm - IPC remove
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * Remove specified message queues,
347c478bd9Sstevel@tonic-gate  * semaphore sets and shared memory ids.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/ipc.h>
397c478bd9Sstevel@tonic-gate #include <sys/msg.h>
407c478bd9Sstevel@tonic-gate #include <sys/sem.h>
417c478bd9Sstevel@tonic-gate #include <sys/shm.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h>
447c478bd9Sstevel@tonic-gate #include <zone.h>
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <signal.h>
487c478bd9Sstevel@tonic-gate #include <locale.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	NULL_MSG	((struct msqid_ds *)NULL)
517c478bd9Sstevel@tonic-gate #define	NULL_SEM	((struct semid_ds *)NULL)
527c478bd9Sstevel@tonic-gate #define	NULL_SHM	((struct shmid_ds *)NULL)
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	USAGE	"usage: ipcrm [-z zone] [ [-q msqid] [-m shmid] " \
557c478bd9Sstevel@tonic-gate "[-s semid]\n\t [-Q msgkey] [-M shmkey] [-S semkey] ... ]\n"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	IPC_KEYMATCH(perm, zoneid, key) \
587c478bd9Sstevel@tonic-gate 	((perm).ipcx_key == (key) && (perm).ipcx_zoneid == (zoneid))
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static char opts[] = "z:q:m:s:Q:M:S:";	/* allowable options for getopt */
617c478bd9Sstevel@tonic-gate extern char	*optarg;	/* arg pointer for getopt */
627c478bd9Sstevel@tonic-gate extern int	optind;		/* option index for getopt */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static zoneid_t zoneid;
657c478bd9Sstevel@tonic-gate static int zflg;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static int *idlist, nids;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static void
oops(char * thing,char * arg)707c478bd9Sstevel@tonic-gate oops(char *thing, char *arg)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	char *e;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	switch (errno) {
757c478bd9Sstevel@tonic-gate 	case ENOENT:	/* key not found */
767c478bd9Sstevel@tonic-gate 	case EINVAL:	/* id not found */
777c478bd9Sstevel@tonic-gate 		e = "not found";
787c478bd9Sstevel@tonic-gate 		break;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	case EPERM:
817c478bd9Sstevel@tonic-gate 		e = "permission denied";
827c478bd9Sstevel@tonic-gate 		break;
837c478bd9Sstevel@tonic-gate 	default:
847c478bd9Sstevel@tonic-gate 		e = "unknown error";
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("ipcrm: %s(%s): %s\n"), thing, arg, e);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* convert string to numeric key */
917c478bd9Sstevel@tonic-gate static key_t
getkey(char * kp)927c478bd9Sstevel@tonic-gate getkey(char *kp)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	key_t k;
957c478bd9Sstevel@tonic-gate 	char *tp;	/* will point to char that terminates strtol scan */
967c478bd9Sstevel@tonic-gate 
97*7c499da3SKeerthi Kondaka 	if ((k = (key_t)strtoul(kp, &tp, 0)) == IPC_PRIVATE || *tp != '\0') {
987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("ipcrm: illegal key: %s\n"),
997c478bd9Sstevel@tonic-gate 		    kp);
1007c478bd9Sstevel@tonic-gate 		return (0);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	return (k);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Gets list of all IPC ids (of a particular type) visible in the
1077c478bd9Sstevel@tonic-gate  * caller's zone.  Returns number of ids retrieved.  On return, idlist
1087c478bd9Sstevel@tonic-gate  * is set to point to an array of ids at least as large as the number
1097c478bd9Sstevel@tonic-gate  * retrieved.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate static uint_t
getids(int (* idsfunc)(int *,uint_t,uint_t *))1127c478bd9Sstevel@tonic-gate getids(int (*idsfunc)(int *, uint_t, uint_t *))
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	uint_t n;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	for (;;) {
1177c478bd9Sstevel@tonic-gate 		if (idsfunc(idlist, nids, &n) != 0)
1187c478bd9Sstevel@tonic-gate 			goto err;	/* should never happen */
1197c478bd9Sstevel@tonic-gate 		if (n <= nids)
1207c478bd9Sstevel@tonic-gate 			break;
1217c478bd9Sstevel@tonic-gate 		idlist = realloc(idlist, (nids = n) * sizeof (int));
1227c478bd9Sstevel@tonic-gate 		if (idlist == NULL)
1237c478bd9Sstevel@tonic-gate 			goto err;
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	return (n);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate err:
1287c478bd9Sstevel@tonic-gate 	perror("ipcrm");
1297c478bd9Sstevel@tonic-gate 	exit(1);
1307c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static int
msggetid(char * arg)1347c478bd9Sstevel@tonic-gate msggetid(char *arg)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	int id = atol(arg);
1377c478bd9Sstevel@tonic-gate 	struct msqid_ds64 qds;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (!zflg)
1407c478bd9Sstevel@tonic-gate 		return (id);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (msgctl64(id, IPC_STAT64, &qds) < 0) {
1437c478bd9Sstevel@tonic-gate 		oops("msgctl", arg);
1447c478bd9Sstevel@tonic-gate 		return (-1);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	if (qds.msgx_perm.ipcx_zoneid != zoneid) {
1477c478bd9Sstevel@tonic-gate 		/*
1487c478bd9Sstevel@tonic-gate 		 * Not in right zone, pretend the call failed.
1497c478bd9Sstevel@tonic-gate 		 * Message should be the same as that returned if
1507c478bd9Sstevel@tonic-gate 		 * msggetid succeeds but the subsequent IPC_RMID fails
1517c478bd9Sstevel@tonic-gate 		 * with EINVAL.
1527c478bd9Sstevel@tonic-gate 		 */
1537c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1547c478bd9Sstevel@tonic-gate 		oops("msgctl", arg);
1557c478bd9Sstevel@tonic-gate 		return (-1);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	return (id);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static int
msggetkey(char * kp)1617c478bd9Sstevel@tonic-gate msggetkey(char *kp)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	key_t k;
1647c478bd9Sstevel@tonic-gate 	int id, i;
1657c478bd9Sstevel@tonic-gate 	uint_t n;
1667c478bd9Sstevel@tonic-gate 	struct msqid_ds64 qds;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if ((k = getkey(kp)) == 0)
1697c478bd9Sstevel@tonic-gate 		return (-1);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (!zflg) {
1727c478bd9Sstevel@tonic-gate 		/* lookup in local zone is simple */
1737c478bd9Sstevel@tonic-gate 		if ((id = msgget(k, 0)) == -1)
1747c478bd9Sstevel@tonic-gate 			oops("msgget", kp);
1757c478bd9Sstevel@tonic-gate 		return (id);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	n = getids(msgids);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/* search for right key and zone combination */
1817c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
1827c478bd9Sstevel@tonic-gate 		id = idlist[i];
1837c478bd9Sstevel@tonic-gate 		if (msgctl64(id, IPC_STAT64, &qds) < 0)
1847c478bd9Sstevel@tonic-gate 			continue;
1857c478bd9Sstevel@tonic-gate 		if (IPC_KEYMATCH(qds.msgx_perm, zoneid, k))
1867c478bd9Sstevel@tonic-gate 			return (id);	/* found it, no need to look further */
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("ipcrm: unknown key: %s\n"), kp);
1897c478bd9Sstevel@tonic-gate 	return (-1);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate static int
semgetid(char * arg)1937c478bd9Sstevel@tonic-gate semgetid(char *arg)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	int id = atol(arg);
1967c478bd9Sstevel@tonic-gate 	struct semid_ds64 sds;
1977c478bd9Sstevel@tonic-gate 	union semun {
1987c478bd9Sstevel@tonic-gate 		int val;
1997c478bd9Sstevel@tonic-gate 		struct semid_ds64 *buf;
2007c478bd9Sstevel@tonic-gate 		ushort_t *array;
2017c478bd9Sstevel@tonic-gate 	} semarg;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (!zflg)
2047c478bd9Sstevel@tonic-gate 		return (id);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	semarg.buf = &sds;
2077c478bd9Sstevel@tonic-gate 	if (semctl64(id, 0, IPC_STAT64, semarg) < 0) {
2087c478bd9Sstevel@tonic-gate 		oops("semctl", arg);
2097c478bd9Sstevel@tonic-gate 		return (-1);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	if (sds.semx_perm.ipcx_zoneid != zoneid) {
2127c478bd9Sstevel@tonic-gate 		/*
2137c478bd9Sstevel@tonic-gate 		 * Not in right zone, pretend the call failed.
2147c478bd9Sstevel@tonic-gate 		 * Message should be the same as that returned if
2157c478bd9Sstevel@tonic-gate 		 * semgetid succeeds but the subsequent IPC_RMID fails
2167c478bd9Sstevel@tonic-gate 		 * with EINVAL.
2177c478bd9Sstevel@tonic-gate 		 */
2187c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2197c478bd9Sstevel@tonic-gate 		oops("semctl", arg);
2207c478bd9Sstevel@tonic-gate 		return (-1);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 	return (id);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate static int
semgetkey(char * kp)2267c478bd9Sstevel@tonic-gate semgetkey(char *kp)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	key_t k;
2297c478bd9Sstevel@tonic-gate 	int id, i;
2307c478bd9Sstevel@tonic-gate 	uint_t n;
2317c478bd9Sstevel@tonic-gate 	struct semid_ds64 sds;
2327c478bd9Sstevel@tonic-gate 	union semun {
2337c478bd9Sstevel@tonic-gate 		int val;
2347c478bd9Sstevel@tonic-gate 		struct semid_ds64 *buf;
2357c478bd9Sstevel@tonic-gate 		ushort_t *array;
2367c478bd9Sstevel@tonic-gate 	} semarg;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if ((k = getkey(kp)) == 0)
2397c478bd9Sstevel@tonic-gate 		return (-1);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (!zflg) {
2427c478bd9Sstevel@tonic-gate 		/* lookup in local zone is simple */
2437c478bd9Sstevel@tonic-gate 		if ((id = semget(k, 0, 0)) == -1)
2447c478bd9Sstevel@tonic-gate 			oops("semget", kp);
2457c478bd9Sstevel@tonic-gate 		return (id);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	n = getids(semids);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	semarg.buf = &sds;
2517c478bd9Sstevel@tonic-gate 	/* search for right key and zone combination */
2527c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
2537c478bd9Sstevel@tonic-gate 		int id;
2547c478bd9Sstevel@tonic-gate 		id = idlist[i];
2557c478bd9Sstevel@tonic-gate 		if (semctl64(id, 0, IPC_STAT64, semarg) < 0)
2567c478bd9Sstevel@tonic-gate 			continue;
2577c478bd9Sstevel@tonic-gate 		if (IPC_KEYMATCH(sds.semx_perm, zoneid, k))
2587c478bd9Sstevel@tonic-gate 			return (id);	/* found it, no need to look further */
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("ipcrm: unknown key: %s\n"), kp);
2627c478bd9Sstevel@tonic-gate 	return (-1);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate static int
shmgetid(char * arg)2667c478bd9Sstevel@tonic-gate shmgetid(char *arg)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	int id = atol(arg);
2697c478bd9Sstevel@tonic-gate 	struct shmid_ds64 mds;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (!zflg)
2727c478bd9Sstevel@tonic-gate 		return (id);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	if (shmctl64(id, IPC_STAT64, &mds) < 0) {
2757c478bd9Sstevel@tonic-gate 		oops("shmctl", arg);
2767c478bd9Sstevel@tonic-gate 		return (-1);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 	if (mds.shmx_perm.ipcx_zoneid != zoneid) {
2797c478bd9Sstevel@tonic-gate 		/*
2807c478bd9Sstevel@tonic-gate 		 * Not in right zone, pretend the call failed.
2817c478bd9Sstevel@tonic-gate 		 * Message should be the same as that returned if
2827c478bd9Sstevel@tonic-gate 		 * shmgetid succeeds but the subsequent IPC_RMID fails
2837c478bd9Sstevel@tonic-gate 		 * with EINVAL.
2847c478bd9Sstevel@tonic-gate 		 */
2857c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2867c478bd9Sstevel@tonic-gate 		oops("shmctl", arg);
2877c478bd9Sstevel@tonic-gate 		return (-1);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	return (id);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate static int
shmgetkey(char * kp)2937c478bd9Sstevel@tonic-gate shmgetkey(char *kp)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	key_t k;
2967c478bd9Sstevel@tonic-gate 	int id, i;
2977c478bd9Sstevel@tonic-gate 	uint_t n;
2987c478bd9Sstevel@tonic-gate 	struct shmid_ds64 mds;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if ((k = getkey(kp)) == 0)
3017c478bd9Sstevel@tonic-gate 		return (-1);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (!zflg) {
3047c478bd9Sstevel@tonic-gate 		/* lookup in local zone is simple */
3057c478bd9Sstevel@tonic-gate 		if ((id = shmget(k, 0, 0)) == -1)
3067c478bd9Sstevel@tonic-gate 			oops("shmget", kp);
3077c478bd9Sstevel@tonic-gate 		return (id);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	n = getids(shmids);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/* search for right key and zone combination */
3137c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
3147c478bd9Sstevel@tonic-gate 		int id;
3157c478bd9Sstevel@tonic-gate 		id = idlist[i];
3167c478bd9Sstevel@tonic-gate 		if (shmctl64(id, IPC_STAT64, &mds) < 0)
3177c478bd9Sstevel@tonic-gate 			continue;
3187c478bd9Sstevel@tonic-gate 		if (IPC_KEYMATCH(mds.shmx_perm, zoneid, k))
3197c478bd9Sstevel@tonic-gate 			return (id);	/* found it, no need to look further */
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("ipcrm: unknown key: %s\n"), kp);
3227c478bd9Sstevel@tonic-gate 	return (-1);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /* convert string containing zone name or id to a numeric id */
3277c478bd9Sstevel@tonic-gate static zoneid_t
getzone(char * arg)3287c478bd9Sstevel@tonic-gate getzone(char *arg)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (zone_get_id(arg, &zoneid) != 0) {
3337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("ipcrm: unknown zone: %s\n"),
3347c478bd9Sstevel@tonic-gate 		    arg);
3357c478bd9Sstevel@tonic-gate 		exit(1);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 	return (zoneid);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)3417c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	int	o;		/* option flag */
3447c478bd9Sstevel@tonic-gate 	int	err;		/* error count */
3457c478bd9Sstevel@tonic-gate 	int	ipc_id;		/* id to remove */
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3487c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * If one or more of the IPC modules is not
3517c478bd9Sstevel@tonic-gate 	 * included in the kernel, the corresponding
3527c478bd9Sstevel@tonic-gate 	 * system calls will incur SIGSYS.  Ignoring
3537c478bd9Sstevel@tonic-gate 	 * that signal makes the system call appear
3547c478bd9Sstevel@tonic-gate 	 * to fail with errno == EINVAL, which can be
3557c478bd9Sstevel@tonic-gate 	 * interpreted appropriately in oops().
3567c478bd9Sstevel@tonic-gate 	 */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	(void) signal(SIGSYS, SIG_IGN);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/*
3617c478bd9Sstevel@tonic-gate 	 * If no -z argument is specified, only objects in the current
3627c478bd9Sstevel@tonic-gate 	 * zone can be removed with keys.
3637c478bd9Sstevel@tonic-gate 	 */
3647c478bd9Sstevel@tonic-gate 	zoneid = getzoneid();
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * Go through the options.  The first pass looks only for -z
3687c478bd9Sstevel@tonic-gate 	 * since this option can affect the processing of keys.  The
3697c478bd9Sstevel@tonic-gate 	 * second pass looks for the other options and ignores -z.
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 	err = 0;
3727c478bd9Sstevel@tonic-gate 	while ((o = getopt(argc, argv, opts)) != EOF) {
3737c478bd9Sstevel@tonic-gate 		switch (o) {
3747c478bd9Sstevel@tonic-gate 		case 'z':
3757c478bd9Sstevel@tonic-gate 			zflg++;
3767c478bd9Sstevel@tonic-gate 			zoneid = getzone(optarg);
3777c478bd9Sstevel@tonic-gate 			break;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		case 'q':	/* skip the rest of the flags */
3807c478bd9Sstevel@tonic-gate 		case 'm':
3817c478bd9Sstevel@tonic-gate 		case 's':
3827c478bd9Sstevel@tonic-gate 		case 'Q':
3837c478bd9Sstevel@tonic-gate 		case 'M':
3847c478bd9Sstevel@tonic-gate 		case 'S':
3857c478bd9Sstevel@tonic-gate 			break;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		case '?':	/* anything else is an error */
3887c478bd9Sstevel@tonic-gate 		default:
3897c478bd9Sstevel@tonic-gate 			err++;
3907c478bd9Sstevel@tonic-gate 			break;
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if (err || (optind < argc)) {
3957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(USAGE));
3967c478bd9Sstevel@tonic-gate 		return (err);
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	if (zflg > 1) {
4007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4017c478bd9Sstevel@tonic-gate 		    gettext("multiple -z options not allowed\n"));
4027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(USAGE));
4037c478bd9Sstevel@tonic-gate 		return (1);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	optind = 1;	/* rewind for pass 2 */
4077c478bd9Sstevel@tonic-gate 	while ((o = getopt(argc, argv, opts)) != EOF) {
4087c478bd9Sstevel@tonic-gate 		switch (o) {
4097c478bd9Sstevel@tonic-gate 		case 'z':	/* zone identifier */
4107c478bd9Sstevel@tonic-gate 			break;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		case 'q':	/* message queue */
4137c478bd9Sstevel@tonic-gate 			if ((ipc_id = msggetid(optarg)) < 0) {
4147c478bd9Sstevel@tonic-gate 				err++;
4157c478bd9Sstevel@tonic-gate 			} else if (msgctl(ipc_id, IPC_RMID, NULL_MSG) == -1) {
4167c478bd9Sstevel@tonic-gate 				oops("msgctl", optarg);
4177c478bd9Sstevel@tonic-gate 				err++;
4187c478bd9Sstevel@tonic-gate 			}
4197c478bd9Sstevel@tonic-gate 			break;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		case 'm':	/* shared memory */
4227c478bd9Sstevel@tonic-gate 			if ((ipc_id = shmgetid(optarg)) < 0) {
4237c478bd9Sstevel@tonic-gate 				err++;
4247c478bd9Sstevel@tonic-gate 			} else if (shmctl(ipc_id, IPC_RMID, NULL_SHM) == -1) {
4257c478bd9Sstevel@tonic-gate 				oops("shmctl", optarg);
4267c478bd9Sstevel@tonic-gate 				err++;
4277c478bd9Sstevel@tonic-gate 			}
4287c478bd9Sstevel@tonic-gate 			break;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		case 's':	/* semaphores */
4317c478bd9Sstevel@tonic-gate 			if ((ipc_id = semgetid(optarg)) < 0) {
4327c478bd9Sstevel@tonic-gate 				err++;
4337c478bd9Sstevel@tonic-gate 			} else if (semctl(ipc_id, 0, IPC_RMID, NULL_SEM) ==
4347c478bd9Sstevel@tonic-gate 			    -1) {
4357c478bd9Sstevel@tonic-gate 				oops("semctl", optarg);
4367c478bd9Sstevel@tonic-gate 				err++;
4377c478bd9Sstevel@tonic-gate 			}
4387c478bd9Sstevel@tonic-gate 			break;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		case 'Q':	/* message queue (by key) */
4417c478bd9Sstevel@tonic-gate 			if ((ipc_id = msggetkey(optarg)) == -1) {
4427c478bd9Sstevel@tonic-gate 				err++;
4437c478bd9Sstevel@tonic-gate 				break;
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 			if (msgctl(ipc_id, IPC_RMID, NULL_MSG) == -1) {
4467c478bd9Sstevel@tonic-gate 				oops("msgctl", optarg);
4477c478bd9Sstevel@tonic-gate 				err++;
4487c478bd9Sstevel@tonic-gate 			}
4497c478bd9Sstevel@tonic-gate 			break;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		case 'M':	/* shared memory (by key) */
4527c478bd9Sstevel@tonic-gate 			if ((ipc_id = shmgetkey(optarg)) == -1) {
4537c478bd9Sstevel@tonic-gate 				err++;
4547c478bd9Sstevel@tonic-gate 				break;
4557c478bd9Sstevel@tonic-gate 			}
4567c478bd9Sstevel@tonic-gate 			if (shmctl(ipc_id, IPC_RMID, NULL_SHM) == -1) {
4577c478bd9Sstevel@tonic-gate 				oops("shmctl", optarg);
4587c478bd9Sstevel@tonic-gate 				err++;
4597c478bd9Sstevel@tonic-gate 			}
4607c478bd9Sstevel@tonic-gate 			break;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		case 'S':	/* semaphores (by key) */
4637c478bd9Sstevel@tonic-gate 			if ((ipc_id = semgetkey(optarg)) == -1) {
4647c478bd9Sstevel@tonic-gate 				err++;
4657c478bd9Sstevel@tonic-gate 				break;
4667c478bd9Sstevel@tonic-gate 			}
4677c478bd9Sstevel@tonic-gate 			if (semctl(ipc_id, 0, IPC_RMID, NULL_SEM) == -1) {
4687c478bd9Sstevel@tonic-gate 				oops("semctl", optarg);
4697c478bd9Sstevel@tonic-gate 				err++;
4707c478bd9Sstevel@tonic-gate 			}
4717c478bd9Sstevel@tonic-gate 			break;
4727c478bd9Sstevel@tonic-gate 		}
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 	return (err);
4757c478bd9Sstevel@tonic-gate }
476