3049241ddb3d87960d208c17ad49d8b2696a5952
[dragonfly.git] / share / doc / psd / 05.sysman / 1.1.t
1 .\" Copyright (c) 1983, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)1.1.t       8.1 (Berkeley) 6/8/93
33 .\"     $FreeBSD: src/share/doc/psd/05.sysman/1.1.t,v 1.1.1.1.14.1 2001/02/18 02:44:42 kris Exp $
34 .\"
35 .sh "Processes and protection
36 .NH 3
37 Host and process identifiers
38 .PP
39 Each UNIX host has associated with it a 32-bit host id, and a host
40 name of up to 256 characters (as defined by MAXHOSTNAMELEN in
41 \fI<sys/param.h>\fP).
42 These are set (by a privileged user)
43 and returned by the calls:
44 .DS
45 sethostid(hostid)
46 long hostid;
47
48 hostid = gethostid();
49 result long hostid;
50
51 sethostname(name, len)
52 char *name; int len;
53
54 len = gethostname(buf, buflen)
55 result int len; result char *buf; int buflen;
56 .DE
57 On each host runs a set of \fIprocesses\fP.
58 Each process is largely independent of other processes,
59 having its own protection domain, address space, timers, and
60 an independent set of references to system or user implemented objects.
61 .PP
62 Each process in a host is named by an integer
63 called the \fIprocess id\fP.  This number is
64 in the range 1-30000
65 and is returned by
66 the \fIgetpid\fP routine:
67 .DS
68 pid = getpid();
69 result int pid;
70 .DE
71 On each UNIX host this identifier is guaranteed to be unique;
72 in a multi-host environment, the (hostid, process id) pairs are
73 guaranteed unique.
74 .NH 3
75 Process creation and termination
76 .PP
77 A new process is created by making a logical duplicate of an
78 existing process:
79 .DS
80 pid = fork();
81 result int pid;
82 .DE
83 The \fIfork\fP call returns twice, once in the parent process, where
84 \fIpid\fP is the process identifier of the child,
85 and once in the child process where \fIpid\fP is 0.
86 The parent-child relationship induces a hierarchical structure on
87 the set of processes in the system.
88 .PP
89 A process may terminate by executing an \fIexit\fP call:
90 .DS
91 exit(status)
92 int status;
93 .DE
94 returning 8 bits of exit status to its parent.
95 .PP
96 When a child process exits or
97 terminates abnormally, the parent process receives
98 information about any
99 event which caused termination of the child process.  A
100 second call provides a non-blocking interface and may also be used
101 to retrieve information about resources consumed by the process during its
102 lifetime.
103 .DS
104 #include <sys/wait.h>
105
106 pid = wait(astatus);
107 result int pid; result union wait *astatus;
108
109 pid = wait3(astatus, options, arusage);
110 result int pid; result union waitstatus *astatus;
111 int options; result struct rusage *arusage;
112 .DE
113 .PP
114 A process can overlay itself with the memory image of another process,
115 passing the newly created process a set of parameters, using the call:
116 .DS
117 execve(name, argv, envp)
118 char *name, **argv, **envp;
119 .DE
120 The specified \fIname\fP must be a file which is in a format recognized
121 by the system, either a binary executable file or a file which causes
122 the execution of a specified interpreter program to process its contents.
123 .NH 3
124 User and group ids
125 .PP
126 Each process in the system has associated with it two user-id's:
127 a \fIreal user id\fP and a \fIeffective user id\fP, both 16 bit
128 unsigned integers (type \fBuid_t\fP).
129 Each process has an \fIreal accounting group id\fP and an \fIeffective
130 accounting group id\fP and a set of
131 \fIaccess group id's\fP.  The group id's are 16 bit unsigned integers
132 (type \fBgid_t\fP).
133 Each process may be in several different access groups, with the maximum
134 concurrent number of access groups a system compilation parameter,
135 the constant NGROUPS in the file \fI<sys/param.h>\fP,
136 guaranteed to be at least 8.
137 .PP
138 The real and effective user ids associated with a process are returned by:
139 .DS
140 ruid = getuid();
141 result uid_t ruid;
142
143 euid = geteuid();
144 result uid_t euid;
145 .DE
146 the real and effective accounting group ids by:
147 .DS
148 rgid = getgid();
149 result gid_t rgid;
150
151 egid = getegid();
152 result gid_t egid;
153 .DE
154 The access group id set is returned by a \fIgetgroups\fP call*:
155 .DS
156 ngroups = getgroups(gidsetsize, gidset);
157 result int ngroups; int gidsetsize; result int gidset[gidsetsize];
158 .DE
159 .FS
160 * The type of the gidset array in getgroups and setgroups
161 remains integer for compatibility with 4.2BSD.
162 It may change to \fBgid_t\fP in future releases.
163 .FE
164 .PP
165 The user and group id's
166 are assigned at login time using the \fIsetreuid\fP, \fIsetregid\fP,
167 and \fIsetgroups\fP calls:
168 .DS
169 setreuid(ruid, euid);
170 int ruid, euid;
171
172 setregid(rgid, egid);
173 int rgid, egid;
174
175 setgroups(gidsetsize, gidset)
176 int gidsetsize; int gidset[gidsetsize];
177 .DE
178 The \fIsetreuid\fP call sets both the real and effective user-id's,
179 while the \fIsetregid\fP call sets both the real
180 and effective accounting group id's.
181 Unless the caller is the super-user, \fIruid\fP
182 must be equal to either the current real or effective user-id,
183 and \fIrgid\fP equal to either the current real or effective
184 accounting group id.  The \fIsetgroups\fP call is restricted
185 to the super-user.
186 .NH 3
187 Process groups
188 .PP
189 Each process in the system is also normally associated with a \fIprocess
190 group\fP.  The group of processes in a process group is sometimes
191 referred to as a \fIjob\fP and manipulated by high-level system
192 software (such as the shell).
193 The current process group of a process is returned by the
194 \fIgetpgrp\fP call:
195 .DS
196 pgrp = getpgrp(pid);
197 result int pgrp; int pid;
198 .DE
199 When a process is in a specific process group it may receive
200 software interrupts affecting the group, causing the group to
201 suspend or resume execution or to be interrupted or terminated.
202 In particular, a system terminal has a process group and only processes
203 which are in the process group of the terminal may read from the
204 terminal, allowing arbitration of terminals among several different jobs.
205 .PP
206 The process group associated with a process may be changed by
207 the \fIsetpgrp\fP call:
208 .DS
209 setpgrp(pid, pgrp);
210 int pid, pgrp;
211 .DE
212 Newly created processes are assigned process id's distinct from all
213 processes and process groups, and the same process group as their
214 parent.  A normal (unprivileged) process may set its process group equal
215 to its process id.  A privileged process may set the process group of any
216 process to any value.