Compare commits

...

2 commits

Author SHA1 Message Date
Jonas 'Sortie' Termansen
b9c49648c4 fixup! Add init groups. 2024-06-18 23:03:47 +02:00
Jonas 'Sortie' Termansen
e2a55139ad fixup! Add init groups. 2024-06-18 22:55:26 +02:00
12 changed files with 107 additions and 13 deletions

View file

@ -4297,7 +4297,7 @@ int main(int argc, char* argv[])
}
// Prevent recursive init without care.
if ( getinit() != getpid() )
if ( getinit(0) != getpid() )
fatal("System is already managed by an init process");
// Register handler that shuts down the system when init exits.

View file

@ -97,7 +97,7 @@ int sys_getentropy(void*, size_t);
uid_t sys_geteuid(void);
gid_t sys_getgid(void);
int sys_gethostname(char*, size_t);
pid_t sys_getinit(void);
pid_t sys_getinit(pid_t);
size_t sys_getpagesize(void);
int sys_getpeername(int, void*, size_t*);
pid_t sys_getpgid(pid_t);

View file

@ -1624,7 +1624,8 @@ pid_t sys_getpgid(pid_t pid)
pid_t sys_getsid(pid_t pid)
{
ScopedLock lock(&process_family_lock);
Process* process = !pid ? CurrentProcess() : CurrentProcess()->GetPTable()->Get(pid);
Process* process =
!pid ? CurrentProcess() : CurrentProcess()->GetPTable()->Get(pid);
if ( !process )
return errno = ESRCH, -1;
if ( !process->session )
@ -1632,10 +1633,11 @@ pid_t sys_getsid(pid_t pid)
return process->session->pid;
}
pid_t sys_getinit(void)
pid_t sys_getinit(pid_t pid)
{
ScopedLock lock(&process_family_lock);
Process* process = CurrentProcess();
Process* process =
!pid ? CurrentProcess() : CurrentProcess()->GetPTable()->Get(pid);
if ( !process->init )
return errno = ESRCH, -1;
return process->init->pid;

View file

@ -781,6 +781,8 @@ MANPAGES2=\
scram/scram.2 \
sys/dnsconfig/getdnsconfig.2 \
sys/dnsconfig/setdnsconfig.2 \
unistd/setinit.2 \
unistd/getinit.2 \
MANPAGES3=\
time/add_leap_seconds.3 \

View file

@ -562,7 +562,7 @@ int exit_thread(int, int, const struct exit_thread*);
int fchdirat(int, const char*);
int fchroot(int);
int fchrootat(int, const char*);
pid_t getinit(void);
pid_t getinit(pid_t);
int memstat(size_t* memused, size_t* memtotal);
int mkpartition(int fd, off_t start, off_t length);
pid_t setinit(void);

1
libc/unistd/getinit.2 Symbolic link
View file

@ -0,0 +1 @@
setinit.2

View file

@ -21,10 +21,9 @@
#include <unistd.h>
DEFN_SYSCALL0(pid_t, sys_getinit, SYSCALL_GETINIT);
DEFN_SYSCALL1(pid_t, sys_getinit, SYSCALL_GETINIT, pid_t);
// TODO: Should this accept a pid like getsid?
pid_t getinit(void)
pid_t getinit(pid_t pid)
{
return sys_getinit();
return sys_getinit(pid);
}

75
libc/unistd/setinit.2 Normal file
View file

@ -0,0 +1,75 @@
.Dd June 18, 2024
.Dt SETINIT 2
.Os
.Sh NAME
.Nm setinit
.Nd become and locate init
.Sh SYNOPSIS
.In unistd.h
.Ft int
.Fn getinit "pid_t pid"
.Ft int
.Fn setinit void
.Sh DESCRIPTION
.Fn setinit
sets the current process as the init process for itself and its subsequently
created descendant processes.
.Fn setinit
runs
.Xr setsid 2
to create a new session (and process group) and can fail for the same reasons as
.Xr setsid 2 .
.Pp
.Fn getinit
returns the init process for the process specified in
.Fa pid ,
or the current process if
.Fa pid
is zero.
.Pp
Orphaned descendant processes are reparanted to their init process.
If an init process exits, all descendant processes atomically receive the
.Dv SIGKILL
signal and become unable to create new processes and threads.
.Sh RETURN VALUES
.Fn setinit
returns the pid of the init process (the current process) on success, or -1 on
error and
.Va error
is set appropriately.
.Pp
.Fn getinit
returns the returns the pid of the init process, or -1 on
error and
.Va error
is set appropriately.
.Sh ERRORS
.Fn setinit
will fail if:
.Bl -tag -width "12345678"
.It Er EPERM
The process is already a process group leader, a session leader, or an init
process.
.El
.Pp
.Fn getinit
will fail if:
.Bl -tag -width "12345678"
.It Er ESRCH
The process specified in
.Fa pid
does not exist.
.El
.Sh SEE ALSO
.Xr getpgrp 2 ,
.Xr getsid 2 ,
.Xr psctl 2 ,
.Xr setpgrp 2 ,
.Xr setsid 2 ,
.Xr init 8
.Sh HISTORY
The
.Fn getinit
and
.Fn setinit
system calls originally appeared in Sortix 1.1.

View file

@ -69,6 +69,21 @@ releasing Sortix x.y, foo." to allow the maintainer to easily
.Xr grep 1
for it after a release.
.Sh CHANGES
.Ss Add init groups
The
.Xr setinit 2
and
.Xr getinit 2
system calls have been added for nested init groups.
The
.Dv TIOCSCTTY
.Xr ioctl 2
for acquiring a controlling terminal has gained a force flag essential to
transferring terminals into nested sessions, and the new
.Dv TIOCUCTTY
.Xr ioctl 2
releases a controlling terminal.
This is a minor compatible ABI change.
.Ss Add tix-repository(8)
The new
.Xr tix-repository 8

View file

@ -39,7 +39,7 @@ int main(int argc, char* argv[])
if ( optind < argc )
errx(1, "extra operand: %s", argv[optind]);
pid_t init_pid = getinit();
pid_t init_pid = getinit(0);
if ( kill(init_pid, SIGQUIT) < 0 )
err(1, "kill: %" PRIdPID, init_pid);

View file

@ -39,7 +39,7 @@ int main(int argc, char* argv[])
if ( optind < argc )
errx(1, "extra operand: %s", argv[optind]);
pid_t init_pid = getinit();
pid_t init_pid = getinit(0);
if ( kill(init_pid, SIGTERM) < 0 )
err(1, "kill: %" PRIdPID, init_pid);

View file

@ -39,7 +39,7 @@ int main(int argc, char* argv[])
if ( optind < argc )
errx(1, "extra operand: %s", argv[optind]);
pid_t init_pid = getinit();
pid_t init_pid = getinit(0);
if ( kill(init_pid, SIGINT) < 0 )
err(1, "kill: %" PRIdPID, init_pid);