Implementing two new ipcs options -g and -u

Liam J. Foy liamfoy at sepulcrum.org
Sun Oct 31 14:45:05 PST 2004


Hey guys,

Here is a patch I want you guys to look at / test / moan over. The diff
message will explain what it is all about. The -u was obtained from
FreeBSD and the -g was created by yours truly.

Any problems, write back. If no objections are recieved, It will be committed
on the 2nd of November. Joerg has gave the nod, but I will let you guys look
also.

Patch starts here:

- Allow ipcs to display information about IPC mechanisms owned by
  a user. The option for this is -u user. User can be a username or
  a uid. Obtained from FreeBSD

- Allow ipcs to display information about IPC mechanisms owned by
  a group. The option for this is -g group. Group can be a group name or
  a gid. Wrote by me. 

Index: ipcs.1
===================================================================
RCS file: /home/dcvs/src/usr.bin/ipcs/ipcs.1,v
retrieving revision 1.2
diff -u -r1.2 ipcs.1
--- ipcs.1	17 Jun 2003 04:29:27 -0000	1.2
+++ ipcs.1	31 Oct 2004 23:30:09 -0000
@@ -30,7 +30,7 @@
 .\" $FreeBSD: src/usr.bin/ipcs/ipcs.1,v 1.9.2.4 2003/04/08 11:07:51 tjr Exp $
 .\" $DragonFly: src/usr.bin/ipcs/ipcs.1,v 1.2 2003/06/17 04:29:27 dillon Exp $
 .\"
-.Dd June 18, 1994
+.Dd Oct 31, 2004 
 .Dt "IPCS" 1
 .Os
 .Sh NAME
@@ -41,6 +41,8 @@
 .Op Fl abcmopqstMQST
 .Op Fl C Ar core
 .Op Fl N Ar system
+.Op Fl g Ar group
+.Op Fl u Ar user
 .Sh DESCRIPTION
 The
 .Nm
@@ -71,6 +73,11 @@
 .It Fl c
 Show the creator's name and group for active semaphores, message queues,
 and shared memory segments.
+.It Fl g Ar group
+Display information about IPC mechanisms owned by
+.Pa group . 
+ Group specification can be in the form of a numeric GID or
+a group name.
 .It Fl m
 Display information about active shared memory segments.
 .It Fl o
@@ -98,6 +105,11 @@
 the last send or receive of a message,
 the last attach or detach of a shared memory segment,
 or the last operation on a semaphore.
+.It Fl u Ar user 
+Display information about IPC mechanisms owned by
+.Pa user .
+User specification can be in the form of a numeric UID or
+a user name.
 .It Fl C Ar core
 Extract values associated with the name list from the specified
 core instead of the default
Index: ipcs.c
===================================================================
RCS file: /home/dcvs/src/usr.bin/ipcs/ipcs.c,v
retrieving revision 1.6
diff -u -r1.6 ipcs.c
--- ipcs.c	19 Jun 2004 18:55:48 -0000	1.6
+++ ipcs.c	31 Oct 2004 18:52:10 -0000
@@ -32,9 +32,11 @@
 
 #include <err.h>
 #include <fcntl.h>
+#include <grp.h>
 #include <kvm.h>
 #include <nlist.h>
 #include <paths.h>
+#include <pwd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -55,7 +57,9 @@
 struct shminfo	shminfo;
 struct shmid_ds	*shmsegs;
 
-void	usage(void);
+static void	usage(void);
+static uid_t	user2uid(const char *);
+static gid_t	grp2gid(const char *);
 
 static struct nlist symbols[] = {
 	{"_sema"},
@@ -128,10 +132,15 @@
 {
 	int     display = SHMINFO | MSGINFO | SEMINFO;
 	int     option = 0;
-	char   *core = NULL, *namelist = NULL;
+	char   *core, *namelist;
+	const char *user, *grp;
 	int     i;
+	uid_t	useruid;
+	gid_t	grpgid;
 
-	while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1)
+	core = namelist = NULL;
+
+	while ((i = getopt(argc, argv, "MmQqSsabC:cN:ou:g:ptT")) != -1)
 		switch (i) {
 		case 'M':
 			display = SHMTOTAL;
@@ -178,6 +187,16 @@
 		case 't':
 			option |= TIME;
 			break;
+		case 'u':
+			user = optarg;
+			grp = NULL;
+			useruid = user2uid(optarg);
+			break;
+		case 'g':
+			grp = optarg;
+			user = NULL;
+			grpgid = grp2gid(optarg);
+			break;
 		default:
 			usage();
 		}
@@ -252,6 +271,11 @@
 					        ctime_buf[100];
 					struct msqid_ds *msqptr = &xmsqids[i];
 
+					if (user && useruid != msqptr->msg_perm.uid)
+							continue;
+					if (grp && grpgid != msqptr->msg_perm.gid)
+							continue;
+	
 					cvt_time(msqptr->msg_stime, stime_buf);
 					cvt_time(msqptr->msg_rtime, rtime_buf);
 					cvt_time(msqptr->msg_ctime, ctime_buf);
@@ -340,6 +364,12 @@
 					        ctime_buf[100];
 					struct shmid_ds *shmptr = &xshmids[i];
 
+					if (user && useruid != shmptr->shm_perm.uid)
+						continue;
+
+					if (grp && grpgid != shmptr->shm_perm.gid)
+						continue;
+
 					cvt_time(shmptr->shm_atime, atime_buf);
 					cvt_time(shmptr->shm_dtime, dtime_buf);
 					cvt_time(shmptr->shm_ctime, ctime_buf);
@@ -431,6 +461,12 @@
 					char    ctime_buf[100], otime_buf[100];
 					struct semid_ds *semaptr = &xsema[i];
 
+					if (user && useruid != semaptr->sem_perm.uid)
+						continue;
+
+					if (grp && grpgid != semaptr->sem_perm.gid)
+						continue;
+
 					cvt_time(semaptr->sem_otime, otime_buf);
 					cvt_time(semaptr->sem_ctime, ctime_buf);
 
@@ -470,11 +506,43 @@
 	exit(0);
 }
 
-void
+static uid_t
+user2uid(const char *username)
+{
+	struct passwd *pwd;
+	uid_t uid;
+	char *r;
+
+	uid = strtoul(username, &r, 0);
+	if (!*r && r != username)
+		return (uid);
+	if ((pwd = getpwnam(username)) == NULL)
+		errx(1, "No such user");
+	endpwent();
+	return (pwd->pw_uid);
+}
+
+static gid_t
+grp2gid(const char *groupname)
+{
+	struct group *grp;
+	gid_t gid;
+	char *r;
+
+	gid = strtol(groupname, &r, 0);
+	if (!*r && r != groupname)
+		return (gid);
+	if ((grp = getgrnam(groupname)) == NULL)
+		errx(1, "No such group");
+	endgrent();
+	return (grp->gr_gid);
+}	
+
+static void
 usage(void)
 {
 
 	fprintf(stderr,
-	    "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist]\n");
+	    "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist] [-u user] [-g group]\n");
 	exit(1);
 }




-- 
- Liam J. Foy
liamfoy at xxxxxxxxxxxxx





More information about the Kernel mailing list