Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 16 Sep 1995 18:02:10 +0300 (EET DST)
From:      Jukka Ukkonen <jau@jau.csc.fi>
To:        hackers@freebsd.org, questions@freebsd.org
Subject:   Session ID and ps showing real SID...
Message-ID:  <199509161502.SAA02059@jau.csc.fi>

next in thread | raw e-mail | index | archive | help

	Hi all!

	It has been quite a while since offered I my idea of adding
	a system call named getsid() to FreeBSD. Afterwards I have
	added also a new TIOCGSID ioctl to the system and fixed
	a bit my initial getsid(). I also modified ps to show the SID
	instead of the addresses of the session structures. The old
	"ps -j" is now invoked with "ps -J" in my system. I think my
	current -j option is much more informative than the original
	version. Here is a sample of the output from "ps -jax"

(jau) 17:02 ~# ps -jax
USER       PID  PPID  PGID   SID JOBC STAT TT       TIME COMMAND
root         0     0     0   176    0 DLs  ??    0:00.00  (swapper)
root         1     0     1     1    0 Is   ??    0:00.06 /sbin/init --
root         2     0     0     0    0 DL   ??    0:01.37  (pagedaemon)
root         3     0     0     0    0 DL   ??    0:00.16  (vmdaemon)
root         4     0     0     0    0 DL   ??    0:00.54  (update)
root        56     1    56    56    0 Is   ??    0:00.31 syslogd
root        65     1    65    65    0 S<s  ??    0:00.57 xntpd -f /etc/xntp.dri
daemon      69     1    69    69    0 IWs  ??    0:00.02 /usr/local/sbin/portma
root        78     1    78    78    0 Is   ??    0:00.22 inetd
root        85     1    85    85    0 Ss   ??    0:00.10 cron
root        87     1    87    87    0 IWs  ??    0:00.05 lpd
root        92     1    92    92    0 Is   ??    0:00.05 sendmail: accepting co
uucp       143     1   143   143    0 IWs  ??    0:00.08 /usr/local/sbin/faxq
root       160   159   160   146    1 S    ??    0:19.24 X -auth /home/jau/.Xau
jau        177   172   177   177    0 Is   p0    0:01.99 -csh (tcsh)
jau        176   171   176   176    0 Is   p1    0:01.85 -csh (tcsh)
root       213   176   213   176    1 S    p1    0:01.98 -root (tcsh)
root       266   213   266   176    1 R+   p1    0:00.02 ps -jax
jau        178   170   178   178    0 Is+  p2    0:02.08 -csh (tcsh)
jau        146     1   146   146    0 IWs  v0    0:01.80 -tcsh (tcsh)
jau        152   146   152   146    1 IW+  v0    0:00.09 /bin/sh /usr/X11R6/bin
jau        159   152   152   146    1 I+   v0    0:00.12 xinit /home/jau/.xinit
jau        161   159   161   146    1 S    v0    0:01.64 tvtwm
jau        168   161   161   146    1 S    v0    0:01.40 xclock -update 1 -geom
jau        169   161   161   146    1 S    v0    0:00.41 xbiff -volume 0 -geome
root       170   161   161   146    1 I    v0    0:01.38 xterm -sb -132 -aw -rw
root       171   161   161   146    1 S    v0    0:01.55 xterm -sb -132 -aw -rw
root       172   161   161   146    1 S    v0    0:05.64 xterm -sb -132 -aw -rw
root       147     1   147   147    0 IWs+ v1    0:00.08 /usr/libexec/getty Pc
root       148     1   148   148    0 IWs+ v2    0:00.04 /usr/libexec/getty Pc
root       233     1   233   233    0 Is+  01    0:00.01 slattach -a -s 38400 -

	Well, before anyone gets too upset about my getsid() idea I should
	point out that I am not trying to push it to anyone or anywhere,
	but I have really enjoyed it being there and simply wish to share
	the benefits with others. The chance of having a more informative
	ps command is a nice example of when such a feature becomes handy.
	

	Cheers,
		// jau
------
  /    Jukka A. Ukkonen,       FUNET / Centre for Scientific Computing
 /__   M.Sc. (sw-eng & cs)               Tel:   (Home) +358-0-578628
   /   Internet: ukkonen@csc.fi                 (Work) +358-0-4573208
  /    Internet: jau@funet.fi                 (Mobile) +358-400-606671
 v     X.400:    c=fi, admd=fumail, no prmd, org=csc, pn=jukka.ukkonen


# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	getsid.patch
#	ps-sid.patch
#	TIOCGSID.patch
#
echo x - getsid.patch
sed 's/^X//' >getsid.patch << 'END-of-getsid.patch'
X*** /usr/src/lib/libc/sys/Makefile.inc.orig	Sat May 27 07:17:04 1995
X--- /usr/src/lib/libc/sys/Makefile.inc	Fri Jul  7 01:46:41 1995
X***************
X*** 17,23 ****
X  	fchflags.o fchmod.o fchown.o fcntl.o flock.o fpathconf.o fstat.o \
X  	fstatfs.o fsync.o getdirentries.o getdtablesize.o getegid.o \
X  	geteuid.o getfh.o getfsstat.o getgid.o getgroups.o getitimer.o \
X! 	getpeername.o getpgrp.o getpid.o getppid.o getpriority.o \
X  	getrlimit.o getrusage.o getsockname.o getsockopt.o gettimeofday.o \
X  	getuid.o ioctl.o kill.o ktrace.o lfs_bmapv.o lfs_markv.o \
X  	lfs_segclean.o lfs_segwait.o link.o listen.o lstat.o \
X--- 17,23 ----
X  	fchflags.o fchmod.o fchown.o fcntl.o flock.o fpathconf.o fstat.o \
X  	fstatfs.o fsync.o getdirentries.o getdtablesize.o getegid.o \
X  	geteuid.o getfh.o getfsstat.o getgid.o getgroups.o getitimer.o \
X! 	getpeername.o getpgrp.o getpid.o getppid.o getsid.o getpriority.o \
X  	getrlimit.o getrusage.o getsockname.o getsockopt.o gettimeofday.o \
X  	getuid.o ioctl.o kill.o ktrace.o lfs_bmapv.o lfs_markv.o \
X  	lfs_segclean.o lfs_segwait.o link.o listen.o lstat.o \
X*** /usr/include/sys/syscall.h.orig	Sun Apr 23 15:22:06 1995
X--- /usr/include/sys/syscall.h	Sat Jul  8 00:07:56 1995
X***************
X*** 191,193 ****
X--- 191,194 ----
X  #define	SYS___sysctl	202
X  #define	SYS_mlock	203
X  #define	SYS_munlock	204
X+ #define	SYS_getsid	205
X*** /usr/include/sys/syscall-hide.h.orig	Fri Jul  7 01:14:16 1995
X--- /usr/include/sys/syscall-hide.h	Fri Jul  7 01:13:22 1995
X***************
X*** 214,216 ****
X--- 214,217 ----
X  HIDE_BSD(__sysctl)
X  HIDE_BSD(mlock)
X  HIDE_BSD(munlock)
X+ HIDE_BSD(getsid)
X*** /sys/kern/init_sysent.c.orig	Fri Jul  7 09:27:51 1995
X--- /sys/kern/init_sysent.c	Fri Jul  7 09:28:25 1995
X***************
X*** 177,182 ****
X--- 177,183 ----
X  int	__sysctl();
X  int	mlock();
X  int	munlock();
X+ int	getsid();
X  int	lkmnosys();
X  
X  #ifdef COMPAT_43
X***************
X*** 484,490 ****
X  	{ 6, __sysctl },			/* 202 = __sysctl */
X  	{ 2, mlock },			/* 203 = mlock */
X  	{ 2, munlock },			/* 204 = munlock */
X! 	{ 0, nosys },			/* 205 = nosys */
X  	{ 0, nosys },			/* 206 = nosys */
X  	{ 0, nosys },			/* 207 = nosys */
X  	{ 0, nosys },			/* 208 = nosys */
X--- 485,492 ----
X  	{ 6, __sysctl },			/* 202 = __sysctl */
X  	{ 2, mlock },			/* 203 = mlock */
X  	{ 2, munlock },			/* 204 = munlock */
X! 	/* { 0, nosys },		205 = nosys */
X! 	{ 1, getsid },			/* 205 = getsid */
X  	{ 0, nosys },			/* 206 = nosys */
X  	{ 0, nosys },			/* 207 = nosys */
X  	{ 0, nosys },			/* 208 = nosys */
X*** /sys/kern/kern_proc.c.orig	Tue May 30 11:05:37 1995
X--- /sys/kern/kern_proc.c	Sun Jul  9 13:35:29 1995
X***************
X*** 211,216 ****
X--- 211,217 ----
X  			MALLOC(sess, struct session *, sizeof(struct session),
X  				M_SESSION, M_WAITOK);
X  			sess->s_leader = p;
X+ 			sess->s_sid = p->p_pid;
X  			sess->s_count = 1;
X  			sess->s_ttyvp = NULL;
X  			sess->s_ttyp = NULL;
X*** /sys/kern/kern_prot.c.orig	Fri Jul  7 09:27:51 1995
X--- /sys/kern/kern_prot.c	Mon Jul 10 00:00:16 1995
X***************
X*** 95,100 ****
X--- 95,149 ----
X  	return (0);
X  }
X  
X+ /*
X+  *  External signature: pid_t	getsid (pid_t);
X+  *
X+  *  SVR4 style system call getsid() exists only for
X+  *  compatibility because this is a trick which is practically
X+  *  impossible to do from within a user space subroutine.
X+  *  Often this kind of information is useful to have though,
X+  *  and probably X/Open will require this anyway.
X+  */
X+ 
X+ struct getsid_args {
X+     pid_t   pid;
X+ };
X+ 
X+ /* ARGSUSED */
X+ int
X+ getsid (p, uap, retval)
X+     struct proc		*p;
X+     struct getsid_args	*uap;
X+     int			*retval;
X+ {
X+     register struct proc    *targp;		/* taget process */
X+ 
X+     if (! uap->pid || (uap->pid == p->p_pid))
X+ 	targp = p;
X+     else {
X+ 	if (! (targp = pfind(uap->pid)))
X+ 	    return (ESRCH);
X+ 
X+ 	/*
X+ 	 *  For true pedantics only...
X+ 	 *  1.	Either current proc must be owned by root,
X+ 	 *  2.	or be part of the same session as the target,
X+ 	 *  3.	or be owned by the same effective uid as the target,
X+ 	 *  4.  or target must be a descendant of the caller.
X+ 	 */
X+ 	if (p->p_cred->pc_ucred->cr_uid
X+ 	    && (targp->p_session != p->p_session)
X+ 	    && (targp->p_cred->pc_ucred->cr_uid 
X+ 		!= p->p_cred->pc_ucred->cr_uid)
X+ 	    && ! inferior(targp))
X+ 	    return (EPERM);
X+     }
X+ 
X+     *retval = targp->p_session->s_sid;
X+ 
X+     return (0);
X+ }
X+ 
X  /* ARGSUSED */
X  int
X  getuid(p, uap, retval)
X*** /sys/kern/syscalls.c.orig	Fri Jul  7 09:27:51 1995
X--- /sys/kern/syscalls.c	Fri Jul  7 09:28:25 1995
X***************
X*** 246,252 ****
X  	"__sysctl",			/* 202 = __sysctl */
X  	"mlock",			/* 203 = mlock */
X  	"munlock",			/* 204 = munlock */
X! 	"#205",			/* 205 = nosys */
X  	"#206",			/* 206 = nosys */
X  	"#207",			/* 207 = nosys */
X  	"#208",			/* 208 = nosys */
X--- 246,253 ----
X  	"__sysctl",			/* 202 = __sysctl */
X  	"mlock",			/* 203 = mlock */
X  	"munlock",			/* 204 = munlock */
X! 	/* "#205",			205 = nosys */
X! 	"getsid",			/* 205 = getsid */
X  	"#206",			/* 206 = nosys */
X  	"#207",			/* 207 = nosys */
X  	"#208",			/* 208 = nosys */
X*** /sys/kern/syscalls.master.orig	Fri Jul  7 09:27:51 1995
X--- /sys/kern/syscalls.master	Fri Jul  7 09:28:25 1995
X***************
X*** 277,283 ****
X  ; here allows to avoid one in libc/sys/Makefile.inc.
X  203	STD	2 BSD	mlock
X  204	STD	2 BSD	munlock
X! 205	UNIMPL	0 NOHIDE nosys
X  206	UNIMPL	0 NOHIDE nosys
X  207	UNIMPL	0 NOHIDE nosys
X  208	UNIMPL	0 NOHIDE nosys
X--- 277,284 ----
X  ; here allows to avoid one in libc/sys/Makefile.inc.
X  203	STD	2 BSD	mlock
X  204	STD	2 BSD	munlock
X! ; 205	UNIMPL	0 NOHIDE nosys
X! 205	STD	1 BSD	getsid
X  206	UNIMPL	0 NOHIDE nosys
X  207	UNIMPL	0 NOHIDE nosys
X  208	UNIMPL	0 NOHIDE nosys
X*** /usr/include/unistd.h.orig	Sun Jun  4 16:45:57 1995
X--- /usr/include/unistd.h	Fri Jul  7 08:25:45 1995
X***************
X*** 76,81 ****
X--- 76,82 ----
X  pid_t	 getpgrp __P((void));
X  pid_t	 getpid __P((void));
X  pid_t	 getppid __P((void));
X+ pid_t	 getsid __P((pid_t));
X  uid_t	 getuid __P((void));
X  int	 isatty __P((int));
X  int	 link __P((const char *, const char *));
END-of-getsid.patch
echo x - ps-sid.patch
sed 's/^X//' >ps-sid.patch << 'END-of-ps-sid.patch'
X*** extern.h.orig	Sat Sep 24 02:56:42 1994
X--- extern.h	Sun Jul 30 10:32:15 1995
X***************
X*** 50,55 ****
X--- 50,56 ----
X  void	 cputime __P((KINFO *, VARENT *));
X  int	 donlist __P((void));
X  void	 evar __P((KINFO *, VARENT *));
X+ void	 psid __P((KINFO *, VARENT *));
X  char	*fmt_argv __P((char **, char *, int));
X  double	 getpcpu __P((KINFO *));
X  double	 getpmem __P((KINFO *));
X*** keyword.c.orig	Sun Oct  2 08:33:28 1994
X--- keyword.c	Sun Jul 30 10:59:36 1995
X***************
X*** 143,148 ****
X--- 143,149 ----
X  		ULONG, UIDFMT},
X  	{"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
X  	{"sess", "SESS", NULL, 0, evar, 6, EOFF(e_sess), KPTR, "x"},
X+ 	{"sid", "SID", NULL, 0, psid, PIDLEN, POFF(p_pid), LONG, PIDFMT},
X  	{"sig", "PENDING", NULL, 0, pvar, 8, POFF(p_siglist), LONG, "x"},
X  	{"sigcatch", "CAUGHT", NULL, 0, pvar, 8, POFF(p_sigcatch), LONG, "x"},
X  	{"sigignore", "IGNORED",
X***************
X*** 160,165 ****
X--- 161,167 ----
X  	{"time", "TIME", NULL, USER, cputime, 9},
X  	{"tpgid", "TPGID", NULL, 0, evar, 4, EOFF(e_tpgid), ULONG, PIDFMT},
X  	{"tsess", "TSESS", NULL, 0, evar, 6, EOFF(e_tsess), KPTR, "x"},
X+ /*	{"tsess", "TSESS", NULL, 0, psid, 6, EOFF(e_tsess), LONG, "d"},	*/
X  	{"tsiz", "TSIZ", NULL, 0, tsize, 4},
X  	{"tt", "TT", NULL, LJUST, tname, 3},
X  	{"tty", "TTY", NULL, LJUST, longtname, 8},
X***************
X*** 229,234 ****
X--- 231,237 ----
X  	{"rtprio", "RTPRIO", NULL, 0, pvar, 7, POFF(p_rtprio), LONG, "d"},
X  	{"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
X  	{"sess", "SESS", NULL, 0, evar, 6, EOFF(e_sess), KPTR, "x"},
X+ 	{"sid", "SID", NULL, 0, psid, PIDLEN, POFF(p_pid), LONG, PIDFMT},
X  	{"sig", "PENDING", NULL, 0, pvar, 8, POFF(p_sig), LONG, "x"},
X  	{"sigcatch", "CAUGHT", NULL, 0, pvar, 8, POFF(p_sigcatch), LONG, "x"},
X  	{"sigignore", "IGNORED",
X***************
X*** 247,252 ****
X--- 250,256 ----
X  	{"tpgid", "TPGID", NULL, 0, evar, 4, EOFF(e_tpgid), ULONG, PIDFMT},
X  	{"trs", "TRS", NULL, 0, trss, 3},
X  	{"tsess", "TSESS", NULL, 0, evar, 6, EOFF(e_tsess), KPTR, "x"},
X+ /*	{"tsess", "TSESS", NULL, 0, psid, 6, EOFF(e_tsess), LONG, "d"},	*/
X  	{"tsiz", "TSIZ", NULL, 0, tsize, 4},
X  	{"tt", "TT", NULL, LJUST, tname, 3},
X  	{"tty", "TTY", NULL, LJUST, longtname, 8},
X*** print.c.orig	Tue May 30 00:07:04 1995
X--- print.c	Sun Jul 30 10:17:58 1995
X***************
X*** 742,744 ****
X--- 742,757 ----
X  	else
X  		(void)printf("%*s", v->width, "-");
X  }
X+ 
X+ void
X+ psid(k, ve)
X+ 	KINFO *k;
X+ 	VARENT *ve;
X+ {
X+ 	VAR	*v;
X+ 	pid_t	sid = getsid (k->ki_p->kp_proc.p_pid);
X+ 
X+ 	v = ve->var;
X+ 	printval((char *) &sid, v);
X+ }
X+ 
X*** ps.c.orig	Tue May 30 03:07:05 1995
X--- ps.c	Tue Aug  1 19:48:42 1995
X***************
X*** 92,98 ****
X  static void	 usage __P((void));
X  
X  char dfmt[] = "pid tt state time command";
X! char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
X  char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
X  char   o1[] = "pid";
X  char   o2[] = "tt state time command";
X--- 92,99 ----
X  static void	 usage __P((void));
X  
X  char dfmt[] = "pid tt state time command";
X! char Jfmt[] = "user pid ppid pgid sess jobc state tt time command";
X! char jfmt[] = "user pid ppid pgid sid jobc state tt time command";
X  char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
X  char   o1[] = "pid";
X  char   o2[] = "tt state time command";
X***************
X*** 133,139 ****
X  	ttydev = NODEV;
X  	memf = nlistf = swapf = NULL;
X  	while ((ch = getopt(argc, argv,
X! 	    "aCeghjLlM:mN:O:o:p:rSTt:uvW:wx")) != EOF)
X  		switch((char)ch) {
X  		case 'a':
X  			all = 1;
X--- 134,140 ----
X  	ttydev = NODEV;
X  	memf = nlistf = swapf = NULL;
X  	while ((ch = getopt(argc, argv,
X! 	    "aCeghJjLlM:mN:O:o:p:rSTt:uvW:wx")) != EOF)
X  		switch((char)ch) {
X  		case 'a':
X  			all = 1;
X***************
X*** 148,153 ****
X--- 149,159 ----
X  			break;			/* no-op */
X  		case 'h':
X  			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
X+ 			break;
X+ 		case 'J':
X+ 			parsefmt(Jfmt);
X+ 			fmt = 1;
X+ 			jfmt[0] = '\0';
X  			break;
X  		case 'j':
X  			parsefmt(jfmt);
END-of-ps-sid.patch
echo x - TIOCGSID.patch
sed 's/^X//' >TIOCGSID.patch << 'END-of-TIOCGSID.patch'
X*** /usr/include/sys/ttycom.h.orig	Tue May 30 11:14:42 1995
X--- /usr/include/sys/ttycom.h	Fri Aug 18 19:51:59 1995
X***************
X*** 90,95 ****
X--- 90,96 ----
X  #define	TIOCCBRK	 _IO('t', 122)		/* clear break bit */
X  #define	TIOCSDTR	 _IO('t', 121)		/* set data terminal ready */
X  #define	TIOCCDTR	 _IO('t', 120)		/* clear data terminal ready */
X+ #define	TIOCGSID	_IOR('t', 128, int)	/* get sid of a tty */
X  #define	TIOCGPGRP	_IOR('t', 119, int)	/* get pgrp of tty */
X  #define	TIOCSPGRP	_IOW('t', 118, int)	/* set pgrp of tty */
X  						/* 117-116 compat */
X*** /sys/kern/tty.c.orig	Mon Jun  5 09:11:05 1995
X--- /sys/kern/tty.c	Fri Aug 18 19:57:37 1995
X***************
X*** 771,776 ****
X--- 771,784 ----
X  			return (ENOTTY);
X  		*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
X  		break;
X+ #ifdef	TIOCGSID
X+ 	case TIOCGSID:			/* get sid of a tty */
X+ 		if (!isctty(p, tp))
X+ 			return (ENOTTY);
X+ 		*(int *)data = (tp->t_session
X+ 				? tp->t_t_session->s_sid : NO_PID);
X+ 		break;
X+ #endif
X  #ifdef TIOCHPCL
X  	case TIOCHPCL:			/* hang up on last close */
X  		s = spltty();
END-of-TIOCGSID.patch
exit




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199509161502.SAA02059>