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