Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does OS X support POSIX sessions?

I am working on a project which could make good use of POSIX sessions (as described here) but I'm now uncertain whether I'm misunderstanding the concept and/or whether OS X even supports process sessions.

OS X 10.9 has a setsid(2) system call but calling it with arguments 0, 0 seems to do noting. And looking at the output of ps -e -o pid,pgid,sess shows that all processes have a session ID of 0:

  PID  PGID   SESS
     1     1      0
    11    11      0
    12    12      0
    13    13      0
    14    14      0
    15    15      0
    16    16      0
    17    17      0
[...]

Does OS X support POSIX sessions for processes and, if not, is there another mechanism in place that e.g. a shell can use to send a signal to all processes started from that shell?


Besides looking at active processes on my system, I used the ersatz-setsid command line tool to run a process in a new session, but without success:

$ ./setsid sleep 1000
$ ps -eo pid,pgid,sess,command
  PID  PGID   SESS COMMAND
[...]
10650 10650      0 sleep 1000
[...]
like image 677
Feuermurmel Avatar asked Dec 25 '22 09:12

Feuermurmel


1 Answers

As asynchronos pointed out, macOS does support the notion of POSIX sessions, but it does not have provisions for querying the session id via ps(1) or top(8); that's why you see 0.

To add some detail:

ps uses sysctl(3) to gather up a list of running processes. That list is returned as an array of (for 64bit machines) struct user64_kinfo_proc (xnu/bsd/sys/sysctl.h). The user-land definition is struct kinfo_proc (/usr/include/sys/sysctl.h).

The kernel function that handles the filling of that array is sysctl_prochandle() (xnu/bsd/kern/kern_sysctl.c).

It, in turn, calls fill_user64_eproc to fill in struct user64_eproc, a member of user64_kinfo_proc.

user64_eproc is the data structure that (I'm guessing) would have a e_psid member denoting the session id of the given process. As such, it has no member. fill_user64_eproc uses the process's struct session to set some flags and record the process's group id into user64_eproc, but it doesn't record the session id because, well, there's nowhere to put it.

Interestingly, the source for ps has a section ifdef'd out that accesses kinfo_proc.ki_sid. This member, of course, also doesn't exist.

So, I'm sure how it's not returned, but not why, unfortunately.

like image 90
Joshua Lynch Avatar answered Jan 06 '23 16:01

Joshua Lynch