Commit | Line | Data |
---|---|---|
b56c252b | 1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
1bbd0b84 | 19 | |
0f2d19dd | 20 | \f |
dbb605f5 | 21 | #ifdef HAVE_CONFIG_H |
2546b5c3 RB |
22 | # include <config.h> |
23 | #endif | |
0f2d19dd | 24 | |
3731192f | 25 | #include <stdlib.h> |
0f2d19dd | 26 | #include <stdio.h> |
e6e2e95a | 27 | #include <errno.h> |
889975e5 | 28 | #include <uniconv.h> |
e6e2e95a | 29 | |
22072f21 LC |
30 | #ifdef HAVE_SCHED_H |
31 | # include <sched.h> | |
32 | #endif | |
33 | ||
a0599745 | 34 | #include "libguile/_scm.h" |
b28f5b3c | 35 | #include "libguile/dynwind.h" |
a0599745 MD |
36 | #include "libguile/fports.h" |
37 | #include "libguile/scmsigs.h" | |
38 | #include "libguile/feature.h" | |
39 | #include "libguile/strings.h" | |
c44ca4fe | 40 | #include "libguile/srfi-13.h" |
a17d2654 | 41 | #include "libguile/srfi-14.h" |
a0599745 | 42 | #include "libguile/vectors.h" |
4ea9429e | 43 | #include "libguile/values.h" |
a0599745 MD |
44 | |
45 | #include "libguile/validate.h" | |
46 | #include "libguile/posix.h" | |
b89c4943 | 47 | #include "libguile/gettext.h" |
9de87eea | 48 | #include "libguile/threads.h" |
0f2d19dd JB |
49 | \f |
50 | ||
02b754d3 GH |
51 | #ifdef HAVE_STRING_H |
52 | #include <string.h> | |
53 | #endif | |
0f2d19dd JB |
54 | #ifdef TIME_WITH_SYS_TIME |
55 | # include <sys/time.h> | |
56 | # include <time.h> | |
57 | #else | |
58 | # if HAVE_SYS_TIME_H | |
59 | # include <sys/time.h> | |
60 | # else | |
61 | # include <time.h> | |
62 | # endif | |
63 | #endif | |
64 | ||
65 | #ifdef HAVE_UNISTD_H | |
66 | #include <unistd.h> | |
95b88819 GH |
67 | #else |
68 | #ifndef ttyname | |
69 | extern char *ttyname(); | |
70 | #endif | |
0f2d19dd JB |
71 | #endif |
72 | ||
3594582b | 73 | #ifdef LIBC_H_WITH_UNISTD_H |
bab0f4e5 JB |
74 | #include <libc.h> |
75 | #endif | |
76 | ||
8cc71382 | 77 | #include <sys/types.h> |
0f2d19dd JB |
78 | #include <sys/stat.h> |
79 | #include <fcntl.h> | |
80 | ||
82893676 | 81 | #ifdef HAVE_PWD_H |
0f2d19dd | 82 | #include <pwd.h> |
82893676 MG |
83 | #endif |
84 | #ifdef HAVE_IO_H | |
85 | #include <io.h> | |
86 | #endif | |
f87c105a | 87 | #ifdef HAVE_WINSOCK2_H |
82893676 MG |
88 | #include <winsock2.h> |
89 | #endif | |
90 | ||
91 | #ifdef __MINGW32__ | |
92 | /* Some defines for Windows here. */ | |
7beabedb | 93 | # include <process.h> |
82893676 MG |
94 | # define pipe(fd) _pipe (fd, 256, O_BINARY) |
95 | #endif /* __MINGW32__ */ | |
0f2d19dd JB |
96 | |
97 | #if HAVE_SYS_WAIT_H | |
98 | # include <sys/wait.h> | |
99 | #endif | |
100 | #ifndef WEXITSTATUS | |
101 | # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) | |
102 | #endif | |
103 | #ifndef WIFEXITED | |
104 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) | |
105 | #endif | |
106 | ||
107 | #include <signal.h> | |
108 | ||
82893676 | 109 | #ifdef HAVE_GRP_H |
0f2d19dd | 110 | #include <grp.h> |
82893676 MG |
111 | #endif |
112 | #ifdef HAVE_SYS_UTSNAME_H | |
0f2d19dd | 113 | #include <sys/utsname.h> |
82893676 | 114 | #endif |
0f2d19dd | 115 | |
0f2d19dd JB |
116 | #ifdef HAVE_SETLOCALE |
117 | #include <locale.h> | |
118 | #endif | |
119 | ||
b89c4943 LC |
120 | #if (defined HAVE_NEWLOCALE) && (defined HAVE_STRCOLL_L) |
121 | # define USE_GNU_LOCALE_API | |
122 | #endif | |
123 | ||
a2f00b9b LC |
124 | #if (defined USE_GNU_LOCALE_API) && (defined HAVE_XLOCALE_H) |
125 | # include <xlocale.h> | |
126 | #endif | |
127 | ||
534bbcc1 | 128 | #ifdef HAVE_CRYPT_H |
94e6d793 MG |
129 | # include <crypt.h> |
130 | #endif | |
131 | ||
0eaf1055 KR |
132 | #ifdef HAVE_NETDB_H |
133 | #include <netdb.h> /* for MAXHOSTNAMELEN on Solaris */ | |
134 | #endif | |
135 | ||
136 | #ifdef HAVE_SYS_PARAM_H | |
137 | #include <sys/param.h> /* for MAXHOSTNAMELEN */ | |
138 | #endif | |
139 | ||
94e6d793 MG |
140 | #if HAVE_SYS_RESOURCE_H |
141 | # include <sys/resource.h> | |
142 | #endif | |
143 | ||
837b0ae0 | 144 | #include <sys/file.h> /* from Gnulib */ |
f0c0141f | 145 | #include <nproc.h> |
eb7e1603 | 146 | |
bab0f4e5 JB |
147 | /* Some Unix systems don't define these. CPP hair is dangerous, but |
148 | this seems safe enough... */ | |
149 | #ifndef R_OK | |
150 | #define R_OK 4 | |
151 | #endif | |
152 | ||
153 | #ifndef W_OK | |
154 | #define W_OK 2 | |
155 | #endif | |
156 | ||
157 | #ifndef X_OK | |
158 | #define X_OK 1 | |
159 | #endif | |
160 | ||
161 | #ifndef F_OK | |
162 | #define F_OK 0 | |
163 | #endif | |
398609a5 | 164 | |
8ab3d8a0 KR |
165 | /* No prototype for this on Solaris 10. The man page says it's in |
166 | <unistd.h> ... but it lies. */ | |
167 | #if ! HAVE_DECL_SETHOSTNAME | |
168 | int sethostname (char *name, size_t namelen); | |
169 | #endif | |
170 | ||
398609a5 JB |
171 | /* On NextStep, <utime.h> doesn't define struct utime, unless we |
172 | #define _POSIX_SOURCE before #including it. I think this is less | |
173 | of a kludge than defining struct utimbuf ourselves. */ | |
174 | #ifdef UTIMBUF_NEEDS_POSIX | |
175 | #define _POSIX_SOURCE | |
176 | #endif | |
177 | ||
178 | #ifdef HAVE_SYS_UTIME_H | |
179 | #include <sys/utime.h> | |
180 | #endif | |
181 | ||
182 | #ifdef HAVE_UTIME_H | |
183 | #include <utime.h> | |
184 | #endif | |
185 | ||
186 | /* Please don't add any more #includes or #defines here. The hack | |
187 | above means that _POSIX_SOURCE may be #defined, which will | |
eb7e1603 KR |
188 | encourage header files to do strange things. |
189 | ||
190 | FIXME: Maybe should undef _POSIX_SOURCE after it's done its job. | |
191 | ||
192 | FIXME: Probably should do all the includes first, then all the fallback | |
193 | declarations and defines, in case things are not in the header we | |
194 | imagine. */ | |
195 | ||
196 | ||
197 | ||
198 | ||
0f2d19dd | 199 | \f |
f015614a MV |
200 | |
201 | /* Two often used patterns | |
202 | */ | |
203 | ||
204 | #define WITH_STRING(str,cstr,code) \ | |
205 | do { \ | |
206 | char *cstr = scm_to_locale_string (str); \ | |
207 | code; \ | |
208 | free (cstr); \ | |
209 | } while (0) | |
210 | ||
211 | #define STRING_SYSCALL(str,cstr,code) \ | |
212 | do { \ | |
213 | int eno; \ | |
214 | char *cstr = scm_to_locale_string (str); \ | |
215 | SCM_SYSCALL (code); \ | |
216 | eno = errno; free (cstr); errno = eno; \ | |
217 | } while (0) | |
218 | ||
219 | ||
220 | \f | |
bc45012d JB |
221 | SCM_SYMBOL (sym_read_pipe, "read pipe"); |
222 | SCM_SYMBOL (sym_write_pipe, "write pipe"); | |
0f2d19dd | 223 | |
a1ec6916 | 224 | SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0, |
1bbd0b84 | 225 | (), |
1e6808ea MG |
226 | "Return a newly created pipe: a pair of ports which are linked\n" |
227 | "together on the local machine. The @emph{car} is the input\n" | |
228 | "port and the @emph{cdr} is the output port. Data written (and\n" | |
229 | "flushed) to the output port can be read from the input port.\n" | |
230 | "Pipes are commonly used for communication with a newly forked\n" | |
231 | "child process. The need to flush the output port can be\n" | |
232 | "avoided by making it unbuffered using @code{setvbuf}.\n" | |
233 | "\n" | |
234 | "Writes occur atomically provided the size of the data in bytes\n" | |
235 | "is not greater than the value of @code{PIPE_BUF}. Note that\n" | |
236 | "the output port is likely to block if too much data (typically\n" | |
237 | "equal to @code{PIPE_BUF}) has been written but not yet read\n" | |
238 | "from the input port.") | |
1bbd0b84 | 239 | #define FUNC_NAME s_scm_pipe |
0f2d19dd JB |
240 | { |
241 | int fd[2], rv; | |
0f2d19dd | 242 | SCM p_rd, p_wt; |
02b754d3 | 243 | |
0f2d19dd JB |
244 | rv = pipe (fd); |
245 | if (rv) | |
1bbd0b84 | 246 | SCM_SYSERROR; |
ee149d03 JB |
247 | |
248 | p_rd = scm_fdes_to_port (fd[0], "r", sym_read_pipe); | |
249 | p_wt = scm_fdes_to_port (fd[1], "w", sym_write_pipe); | |
0f2d19dd JB |
250 | return scm_cons (p_rd, p_wt); |
251 | } | |
1bbd0b84 | 252 | #undef FUNC_NAME |
0f2d19dd JB |
253 | |
254 | ||
0e958795 | 255 | #ifdef HAVE_GETGROUPS |
a1ec6916 | 256 | SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0, |
1bbd0b84 | 257 | (), |
1e6808ea | 258 | "Return a vector of integers representing the current\n" |
bb2c02f2 | 259 | "supplementary group IDs.") |
1bbd0b84 | 260 | #define FUNC_NAME s_scm_getgroups |
0f2d19dd | 261 | { |
1d1559ce | 262 | SCM result; |
66460dfb | 263 | int ngroups; |
1be6b49c | 264 | size_t size; |
66460dfb DH |
265 | GETGROUPS_T *groups; |
266 | ||
267 | ngroups = getgroups (0, NULL); | |
268 | if (ngroups <= 0) | |
1bbd0b84 | 269 | SCM_SYSERROR; |
66460dfb DH |
270 | |
271 | size = ngroups * sizeof (GETGROUPS_T); | |
4c9419ac | 272 | groups = scm_malloc (size); |
634aa8de | 273 | ngroups = getgroups (ngroups, groups); |
66460dfb | 274 | |
f360a962 MV |
275 | result = scm_c_make_vector (ngroups, SCM_BOOL_F); |
276 | while (--ngroups >= 0) | |
4057a3e0 | 277 | SCM_SIMPLE_VECTOR_SET (result, ngroups, scm_from_ulong (groups[ngroups])); |
66460dfb | 278 | |
4c9419ac | 279 | free (groups); |
1d1559ce | 280 | return result; |
1bbd0b84 GB |
281 | } |
282 | #undef FUNC_NAME | |
0e958795 | 283 | #endif |
0f2d19dd | 284 | |
f360a962 MV |
285 | #ifdef HAVE_SETGROUPS |
286 | SCM_DEFINE (scm_setgroups, "setgroups", 1, 0, 0, | |
287 | (SCM group_vec), | |
fcc3adc2 KR |
288 | "Set the current set of supplementary group IDs to the integers\n" |
289 | "in the given vector @var{vec}. The return value is\n" | |
290 | "unspecified.\n" | |
291 | "\n" | |
292 | "Generally only the superuser can set the process group IDs.") | |
f360a962 MV |
293 | #define FUNC_NAME s_scm_setgroups |
294 | { | |
295 | size_t ngroups; | |
296 | size_t size; | |
297 | size_t i; | |
298 | int result; | |
299 | int save_errno; | |
300 | GETGROUPS_T *groups; | |
301 | ||
302 | SCM_VALIDATE_VECTOR (SCM_ARG1, group_vec); | |
303 | ||
4057a3e0 | 304 | ngroups = SCM_SIMPLE_VECTOR_LENGTH (group_vec); |
f360a962 MV |
305 | |
306 | /* validate before allocating, so we don't have to worry about leaks */ | |
307 | for (i = 0; i < ngroups; i++) | |
308 | { | |
309 | unsigned long ulong_gid; | |
310 | GETGROUPS_T gid; | |
4057a3e0 MV |
311 | SCM_VALIDATE_ULONG_COPY (1, SCM_SIMPLE_VECTOR_REF (group_vec, i), |
312 | ulong_gid); | |
f360a962 MV |
313 | gid = ulong_gid; |
314 | if (gid != ulong_gid) | |
4057a3e0 | 315 | SCM_OUT_OF_RANGE (1, SCM_SIMPLE_VECTOR_REF (group_vec, i)); |
f360a962 MV |
316 | } |
317 | ||
318 | size = ngroups * sizeof (GETGROUPS_T); | |
2eb78d06 | 319 | if (size / sizeof (GETGROUPS_T) != ngroups) |
e11e83f3 | 320 | SCM_OUT_OF_RANGE (SCM_ARG1, scm_from_int (ngroups)); |
f360a962 MV |
321 | groups = scm_malloc (size); |
322 | for(i = 0; i < ngroups; i++) | |
4057a3e0 | 323 | groups [i] = SCM_NUM2ULONG (1, SCM_SIMPLE_VECTOR_REF (group_vec, i)); |
f360a962 MV |
324 | |
325 | result = setgroups (ngroups, groups); | |
326 | save_errno = errno; /* don't let free() touch errno */ | |
327 | free (groups); | |
328 | errno = save_errno; | |
329 | if (result < 0) | |
330 | SCM_SYSERROR; | |
331 | return SCM_UNSPECIFIED; | |
332 | } | |
333 | #undef FUNC_NAME | |
334 | #endif | |
335 | ||
82893676 | 336 | #ifdef HAVE_GETPWENT |
a1ec6916 | 337 | SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0, |
1bbd0b84 | 338 | (SCM user), |
b380b885 MD |
339 | "Look up an entry in the user database. @var{obj} can be an integer,\n" |
340 | "a string, or omitted, giving the behaviour of getpwuid, getpwnam\n" | |
341 | "or getpwent respectively.") | |
1bbd0b84 | 342 | #define FUNC_NAME s_scm_getpwuid |
0f2d19dd | 343 | { |
0f2d19dd | 344 | struct passwd *entry; |
0f2d19dd | 345 | |
1d1559ce | 346 | SCM result = scm_c_make_vector (7, SCM_UNSPECIFIED); |
7888309b | 347 | if (SCM_UNBNDP (user) || scm_is_false (user)) |
0f2d19dd | 348 | { |
0f2d19dd | 349 | SCM_SYSCALL (entry = getpwent ()); |
3d781d49 JB |
350 | if (! entry) |
351 | { | |
3d781d49 JB |
352 | return SCM_BOOL_F; |
353 | } | |
0f2d19dd | 354 | } |
e11e83f3 | 355 | else if (scm_is_integer (user)) |
0f2d19dd | 356 | { |
e11e83f3 | 357 | entry = getpwuid (scm_to_int (user)); |
0f2d19dd JB |
358 | } |
359 | else | |
360 | { | |
f015614a MV |
361 | WITH_STRING (user, c_user, |
362 | entry = getpwnam (c_user)); | |
0f2d19dd JB |
363 | } |
364 | if (!entry) | |
1bbd0b84 | 365 | SCM_MISC_ERROR ("entry not found", SCM_EOL); |
02b754d3 | 366 | |
4057a3e0 MV |
367 | SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->pw_name)); |
368 | SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->pw_passwd)); | |
369 | SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->pw_uid)); | |
370 | SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_ulong (entry->pw_gid)); | |
371 | SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (entry->pw_gecos)); | |
0f2d19dd | 372 | if (!entry->pw_dir) |
4057a3e0 | 373 | SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string ("")); |
0f2d19dd | 374 | else |
4057a3e0 | 375 | SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (entry->pw_dir)); |
0f2d19dd | 376 | if (!entry->pw_shell) |
4057a3e0 | 377 | SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string ("")); |
0f2d19dd | 378 | else |
4057a3e0 | 379 | SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (entry->pw_shell)); |
1d1559ce | 380 | return result; |
0f2d19dd | 381 | } |
1bbd0b84 | 382 | #undef FUNC_NAME |
82893676 | 383 | #endif /* HAVE_GETPWENT */ |
0f2d19dd JB |
384 | |
385 | ||
0e958795 | 386 | #ifdef HAVE_SETPWENT |
a1ec6916 | 387 | SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0, |
1bbd0b84 | 388 | (SCM arg), |
b380b885 MD |
389 | "If called with a true argument, initialize or reset the password data\n" |
390 | "stream. Otherwise, close the stream. The @code{setpwent} and\n" | |
391 | "@code{endpwent} procedures are implemented on top of this.") | |
1bbd0b84 | 392 | #define FUNC_NAME s_scm_setpwent |
0f2d19dd | 393 | { |
7888309b | 394 | if (SCM_UNBNDP (arg) || scm_is_false (arg)) |
0f2d19dd JB |
395 | endpwent (); |
396 | else | |
397 | setpwent (); | |
398 | return SCM_UNSPECIFIED; | |
399 | } | |
1bbd0b84 | 400 | #undef FUNC_NAME |
0e958795 | 401 | #endif |
0f2d19dd JB |
402 | |
403 | ||
82893676 | 404 | #ifdef HAVE_GETGRENT |
0f2d19dd | 405 | /* Combines getgrgid and getgrnam. */ |
a1ec6916 | 406 | SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0, |
1bbd0b84 | 407 | (SCM name), |
b380b885 MD |
408 | "Look up an entry in the group database. @var{obj} can be an integer,\n" |
409 | "a string, or omitted, giving the behaviour of getgrgid, getgrnam\n" | |
410 | "or getgrent respectively.") | |
1bbd0b84 | 411 | #define FUNC_NAME s_scm_getgrgid |
0f2d19dd | 412 | { |
0f2d19dd | 413 | struct group *entry; |
1d1559ce | 414 | SCM result = scm_c_make_vector (4, SCM_UNSPECIFIED); |
34d19ef6 | 415 | |
7888309b | 416 | if (SCM_UNBNDP (name) || scm_is_false (name)) |
3d781d49 JB |
417 | { |
418 | SCM_SYSCALL (entry = getgrent ()); | |
419 | if (! entry) | |
420 | { | |
3d781d49 JB |
421 | return SCM_BOOL_F; |
422 | } | |
423 | } | |
e11e83f3 MV |
424 | else if (scm_is_integer (name)) |
425 | SCM_SYSCALL (entry = getgrgid (scm_to_int (name))); | |
0f2d19dd | 426 | else |
f015614a MV |
427 | STRING_SYSCALL (name, c_name, |
428 | entry = getgrnam (c_name)); | |
0f2d19dd | 429 | if (!entry) |
1bbd0b84 | 430 | SCM_SYSERROR; |
02b754d3 | 431 | |
4057a3e0 MV |
432 | SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->gr_name)); |
433 | SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->gr_passwd)); | |
434 | SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->gr_gid)); | |
435 | SCM_SIMPLE_VECTOR_SET(result, 3, scm_makfromstrs (-1, entry->gr_mem)); | |
1d1559ce | 436 | return result; |
0f2d19dd | 437 | } |
1bbd0b84 | 438 | #undef FUNC_NAME |
0f2d19dd JB |
439 | |
440 | ||
441 | ||
a1ec6916 | 442 | SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0, |
1bbd0b84 | 443 | (SCM arg), |
b380b885 MD |
444 | "If called with a true argument, initialize or reset the group data\n" |
445 | "stream. Otherwise, close the stream. The @code{setgrent} and\n" | |
446 | "@code{endgrent} procedures are implemented on top of this.") | |
1bbd0b84 | 447 | #define FUNC_NAME s_scm_setgrent |
0f2d19dd | 448 | { |
7888309b | 449 | if (SCM_UNBNDP (arg) || scm_is_false (arg)) |
0f2d19dd JB |
450 | endgrent (); |
451 | else | |
452 | setgrent (); | |
453 | return SCM_UNSPECIFIED; | |
454 | } | |
1bbd0b84 | 455 | #undef FUNC_NAME |
82893676 | 456 | #endif /* HAVE_GETGRENT */ |
0f2d19dd JB |
457 | |
458 | ||
4ea9429e AW |
459 | #ifdef HAVE_GETRLIMIT |
460 | #ifdef RLIMIT_AS | |
461 | SCM_SYMBOL (sym_as, "as"); | |
462 | #endif | |
463 | #ifdef RLIMIT_CORE | |
464 | SCM_SYMBOL (sym_core, "core"); | |
465 | #endif | |
466 | #ifdef RLIMIT_CPU | |
467 | SCM_SYMBOL (sym_cpu, "cpu"); | |
468 | #endif | |
469 | #ifdef RLIMIT_DATA | |
470 | SCM_SYMBOL (sym_data, "data"); | |
471 | #endif | |
472 | #ifdef RLIMIT_FSIZE | |
473 | SCM_SYMBOL (sym_fsize, "fsize"); | |
474 | #endif | |
475 | #ifdef RLIMIT_MEMLOCK | |
476 | SCM_SYMBOL (sym_memlock, "memlock"); | |
477 | #endif | |
478 | #ifdef RLIMIT_MSGQUEUE | |
479 | SCM_SYMBOL (sym_msgqueue, "msgqueue"); | |
480 | #endif | |
481 | #ifdef RLIMIT_NICE | |
482 | SCM_SYMBOL (sym_nice, "nice"); | |
483 | #endif | |
484 | #ifdef RLIMIT_NOFILE | |
485 | SCM_SYMBOL (sym_nofile, "nofile"); | |
486 | #endif | |
487 | #ifdef RLIMIT_NPROC | |
488 | SCM_SYMBOL (sym_nproc, "nproc"); | |
489 | #endif | |
490 | #ifdef RLIMIT_RSS | |
491 | SCM_SYMBOL (sym_rss, "rss"); | |
492 | #endif | |
493 | #ifdef RLIMIT_RTPRIO | |
494 | SCM_SYMBOL (sym_rtprio, "rtprio"); | |
495 | #endif | |
496 | #ifdef RLIMIT_RTPRIO | |
497 | SCM_SYMBOL (sym_rttime, "rttime"); | |
498 | #endif | |
499 | #ifdef RLIMIT_SIGPENDING | |
500 | SCM_SYMBOL (sym_sigpending, "sigpending"); | |
501 | #endif | |
502 | #ifdef RLIMIT_STACK | |
503 | SCM_SYMBOL (sym_stack, "stack"); | |
504 | #endif | |
505 | ||
506 | static int | |
507 | scm_to_resource (SCM s, const char *func, int pos) | |
508 | { | |
509 | if (scm_is_number (s)) | |
510 | return scm_to_int (s); | |
511 | ||
512 | SCM_ASSERT_TYPE (scm_is_symbol (s), s, pos, func, "symbol"); | |
513 | ||
514 | #ifdef RLIMIT_AS | |
515 | if (s == sym_as) | |
516 | return RLIMIT_AS; | |
517 | #endif | |
518 | #ifdef RLIMIT_CORE | |
519 | if (s == sym_core) | |
520 | return RLIMIT_CORE; | |
521 | #endif | |
522 | #ifdef RLIMIT_CPU | |
523 | if (s == sym_cpu) | |
524 | return RLIMIT_CPU; | |
525 | #endif | |
526 | #ifdef RLIMIT_DATA | |
527 | if (s == sym_data) | |
528 | return RLIMIT_DATA; | |
529 | #endif | |
530 | #ifdef RLIMIT_FSIZE | |
531 | if (s == sym_fsize) | |
532 | return RLIMIT_FSIZE; | |
533 | #endif | |
534 | #ifdef RLIMIT_MEMLOCK | |
535 | if (s == sym_memlock) | |
536 | return RLIMIT_MEMLOCK; | |
537 | #endif | |
538 | #ifdef RLIMIT_MSGQUEUE | |
539 | if (s == sym_msgqueue) | |
540 | return RLIMIT_MSGQUEUE; | |
541 | #endif | |
542 | #ifdef RLIMIT_NICE | |
543 | if (s == sym_nice) | |
544 | return RLIMIT_NICE; | |
545 | #endif | |
546 | #ifdef RLIMIT_NOFILE | |
547 | if (s == sym_nofile) | |
548 | return RLIMIT_NOFILE; | |
549 | #endif | |
550 | #ifdef RLIMIT_NPROC | |
551 | if (s == sym_nproc) | |
552 | return RLIMIT_NPROC; | |
553 | #endif | |
554 | #ifdef RLIMIT_RSS | |
555 | if (s == sym_rss) | |
556 | return RLIMIT_RSS; | |
557 | #endif | |
558 | #ifdef RLIMIT_RTPRIO | |
559 | if (s == sym_rtprio) | |
560 | return RLIMIT_RTPRIO; | |
561 | #endif | |
562 | #ifdef RLIMIT_RTPRIO | |
563 | if (s == sym_rttime) | |
564 | return RLIMIT_RTPRIO; | |
565 | #endif | |
566 | #ifdef RLIMIT_SIGPENDING | |
567 | if (s == sym_sigpending) | |
568 | return RLIMIT_SIGPENDING; | |
569 | #endif | |
570 | #ifdef RLIMIT_STACK | |
571 | if (s == sym_stack) | |
572 | return RLIMIT_STACK; | |
573 | #endif | |
574 | ||
575 | scm_misc_error (func, "invalid rlimit resource ~A", scm_list_1 (s)); | |
576 | return 0; | |
577 | } | |
578 | ||
579 | SCM_DEFINE (scm_getrlimit, "getrlimit", 1, 0, 0, | |
580 | (SCM resource), | |
581 | "Get a resource limit for this process. @var{resource} identifies the resource,\n" | |
582 | "either as an integer or as a symbol. For example, @code{(getrlimit 'stack)}\n" | |
583 | "gets the limits associated with @code{RLIMIT_STACK}.\n\n" | |
584 | "@code{getrlimit} returns two values, the soft and the hard limit. If no\n" | |
585 | "limit is set for the resource in question, the returned limit will be @code{#f}.") | |
586 | #define FUNC_NAME s_scm_getrlimit | |
587 | { | |
588 | int iresource; | |
589 | struct rlimit lim = { 0, 0 }; | |
590 | ||
591 | iresource = scm_to_resource (resource, FUNC_NAME, 1); | |
592 | ||
593 | if (getrlimit (iresource, &lim) != 0) | |
594 | scm_syserror (FUNC_NAME); | |
595 | ||
596 | return scm_values (scm_list_2 ((lim.rlim_cur == RLIM_INFINITY) ? SCM_BOOL_F | |
597 | : scm_from_long (lim.rlim_cur), | |
598 | (lim.rlim_max == RLIM_INFINITY) ? SCM_BOOL_F | |
599 | : scm_from_long (lim.rlim_max))); | |
600 | } | |
601 | #undef FUNC_NAME | |
602 | ||
603 | ||
604 | #ifdef HAVE_SETRLIMIT | |
605 | SCM_DEFINE (scm_setrlimit, "setrlimit", 3, 0, 0, | |
606 | (SCM resource, SCM soft, SCM hard), | |
607 | "Set a resource limit for this process. @var{resource} identifies the resource,\n" | |
608 | "either as an integer or as a symbol. @var{soft} and @var{hard} should be integers,\n" | |
609 | "or @code{#f} to indicate no limit (i.e., @code{RLIM_INFINITY}).\n\n" | |
610 | "For example, @code{(setrlimit 'stack 150000 300000)} sets the @code{RLIMIT_STACK}\n" | |
611 | "limit to 150 kilobytes, with a hard limit of 300 kB.") | |
612 | #define FUNC_NAME s_scm_setrlimit | |
613 | { | |
614 | int iresource; | |
615 | struct rlimit lim = { 0, 0 }; | |
616 | ||
617 | iresource = scm_to_resource (resource, FUNC_NAME, 1); | |
618 | ||
619 | lim.rlim_cur = (soft == SCM_BOOL_F) ? RLIM_INFINITY : scm_to_long (soft); | |
620 | lim.rlim_max = (hard == SCM_BOOL_F) ? RLIM_INFINITY : scm_to_long (hard); | |
621 | ||
622 | if (setrlimit (iresource, &lim) != 0) | |
623 | scm_syserror (FUNC_NAME); | |
624 | ||
625 | return SCM_UNSPECIFIED; | |
626 | } | |
627 | #undef FUNC_NAME | |
628 | #endif /* HAVE_SETRLIMIT */ | |
629 | #endif /* HAVE_GETRLIMIT */ | |
630 | ||
631 | ||
a1ec6916 | 632 | SCM_DEFINE (scm_kill, "kill", 2, 0, 0, |
1bbd0b84 | 633 | (SCM pid, SCM sig), |
b380b885 MD |
634 | "Sends a signal to the specified process or group of processes.\n\n" |
635 | "@var{pid} specifies the processes to which the signal is sent:\n\n" | |
636 | "@table @r\n" | |
637 | "@item @var{pid} greater than 0\n" | |
638 | "The process whose identifier is @var{pid}.\n" | |
639 | "@item @var{pid} equal to 0\n" | |
640 | "All processes in the current process group.\n" | |
641 | "@item @var{pid} less than -1\n" | |
642 | "The process group whose identifier is -@var{pid}\n" | |
643 | "@item @var{pid} equal to -1\n" | |
644 | "If the process is privileged, all processes except for some special\n" | |
645 | "system processes. Otherwise, all processes with the current effective\n" | |
646 | "user ID.\n" | |
647 | "@end table\n\n" | |
648 | "@var{sig} should be specified using a variable corresponding to\n" | |
649 | "the Unix symbolic name, e.g.,\n\n" | |
650 | "@defvar SIGHUP\n" | |
651 | "Hang-up signal.\n" | |
652 | "@end defvar\n\n" | |
653 | "@defvar SIGINT\n" | |
654 | "Interrupt signal.\n" | |
655 | "@end defvar") | |
1bbd0b84 | 656 | #define FUNC_NAME s_scm_kill |
0f2d19dd | 657 | { |
0f2d19dd | 658 | /* Signal values are interned in scm_init_posix(). */ |
82893676 | 659 | #ifdef HAVE_KILL |
a55c2b68 | 660 | if (kill (scm_to_int (pid), scm_to_int (sig)) != 0) |
23d72566 | 661 | SCM_SYSERROR; |
82893676 | 662 | #else |
23d72566 KR |
663 | /* Mingw has raise(), but not kill(). (Other raw DOS environments might |
664 | be similar.) Use raise() when the requested pid is our own process, | |
665 | otherwise bomb. */ | |
a55c2b68 | 666 | if (scm_to_int (pid) == getpid ()) |
23d72566 KR |
667 | { |
668 | if (raise (scm_to_int (sig)) != 0) | |
669 | { | |
670 | err: | |
671 | SCM_SYSERROR; | |
672 | } | |
673 | else | |
674 | { | |
675 | errno = ENOSYS; | |
676 | goto err; | |
677 | } | |
678 | } | |
82893676 | 679 | #endif |
02b754d3 | 680 | return SCM_UNSPECIFIED; |
0f2d19dd | 681 | } |
1bbd0b84 | 682 | #undef FUNC_NAME |
0f2d19dd | 683 | |
d00ae47e | 684 | #ifdef HAVE_WAITPID |
a1ec6916 | 685 | SCM_DEFINE (scm_waitpid, "waitpid", 1, 1, 0, |
1bbd0b84 | 686 | (SCM pid, SCM options), |
b380b885 MD |
687 | "This procedure collects status information from a child process which\n" |
688 | "has terminated or (optionally) stopped. Normally it will\n" | |
689 | "suspend the calling process until this can be done. If more than one\n" | |
690 | "child process is eligible then one will be chosen by the operating system.\n\n" | |
691 | "The value of @var{pid} determines the behaviour:\n\n" | |
692 | "@table @r\n" | |
693 | "@item @var{pid} greater than 0\n" | |
694 | "Request status information from the specified child process.\n" | |
695 | "@item @var{pid} equal to -1 or WAIT_ANY\n" | |
696 | "Request status information for any child process.\n" | |
697 | "@item @var{pid} equal to 0 or WAIT_MYPGRP\n" | |
698 | "Request status information for any child process in the current process\n" | |
699 | "group.\n" | |
700 | "@item @var{pid} less than -1\n" | |
701 | "Request status information for any child process whose process group ID\n" | |
702 | "is -@var{PID}.\n" | |
703 | "@end table\n\n" | |
704 | "The @var{options} argument, if supplied, should be the bitwise OR of the\n" | |
705 | "values of zero or more of the following variables:\n\n" | |
706 | "@defvar WNOHANG\n" | |
707 | "Return immediately even if there are no child processes to be collected.\n" | |
708 | "@end defvar\n\n" | |
709 | "@defvar WUNTRACED\n" | |
710 | "Report status information for stopped processes as well as terminated\n" | |
711 | "processes.\n" | |
712 | "@end defvar\n\n" | |
713 | "The return value is a pair containing:\n\n" | |
714 | "@enumerate\n" | |
715 | "@item\n" | |
716 | "The process ID of the child process, or 0 if @code{WNOHANG} was\n" | |
717 | "specified and no process was collected.\n" | |
718 | "@item\n" | |
719 | "The integer status value.\n" | |
720 | "@end enumerate") | |
1bbd0b84 | 721 | #define FUNC_NAME s_scm_waitpid |
0f2d19dd JB |
722 | { |
723 | int i; | |
724 | int status; | |
725 | int ioptions; | |
0f2d19dd JB |
726 | if (SCM_UNBNDP (options)) |
727 | ioptions = 0; | |
728 | else | |
729 | { | |
0f2d19dd | 730 | /* Flags are interned in scm_init_posix. */ |
a55c2b68 | 731 | ioptions = scm_to_int (options); |
0f2d19dd | 732 | } |
a55c2b68 | 733 | SCM_SYSCALL (i = waitpid (scm_to_int (pid), &status, ioptions)); |
02b754d3 | 734 | if (i == -1) |
1bbd0b84 | 735 | SCM_SYSERROR; |
a55c2b68 | 736 | return scm_cons (scm_from_int (i), scm_from_int (status)); |
0f2d19dd | 737 | } |
1bbd0b84 | 738 | #undef FUNC_NAME |
d00ae47e | 739 | #endif /* HAVE_WAITPID */ |
0f2d19dd | 740 | |
82893676 | 741 | #ifndef __MINGW32__ |
a1ec6916 | 742 | SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0, |
1bbd0b84 | 743 | (SCM status), |
1e6808ea MG |
744 | "Return the exit status value, as would be set if a process\n" |
745 | "ended normally through a call to @code{exit} or @code{_exit},\n" | |
746 | "if any, otherwise @code{#f}.") | |
1bbd0b84 | 747 | #define FUNC_NAME s_scm_status_exit_val |
67ec3667 | 748 | { |
e67dc2be JB |
749 | int lstatus; |
750 | ||
e67dc2be | 751 | /* On Ultrix, the WIF... macros assume their argument is an lvalue; |
e11e83f3 | 752 | go figure. */ |
a55c2b68 | 753 | lstatus = scm_to_int (status); |
e67dc2be | 754 | if (WIFEXITED (lstatus)) |
a55c2b68 | 755 | return (scm_from_int (WEXITSTATUS (lstatus))); |
67ec3667 GH |
756 | else |
757 | return SCM_BOOL_F; | |
758 | } | |
1bbd0b84 | 759 | #undef FUNC_NAME |
e67dc2be | 760 | |
a1ec6916 | 761 | SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0, |
1bbd0b84 | 762 | (SCM status), |
1e6808ea MG |
763 | "Return the signal number which terminated the process, if any,\n" |
764 | "otherwise @code{#f}.") | |
1bbd0b84 | 765 | #define FUNC_NAME s_scm_status_term_sig |
67ec3667 | 766 | { |
e67dc2be JB |
767 | int lstatus; |
768 | ||
a55c2b68 | 769 | lstatus = scm_to_int (status); |
e67dc2be | 770 | if (WIFSIGNALED (lstatus)) |
a55c2b68 | 771 | return scm_from_int (WTERMSIG (lstatus)); |
67ec3667 GH |
772 | else |
773 | return SCM_BOOL_F; | |
774 | } | |
1bbd0b84 | 775 | #undef FUNC_NAME |
0f2d19dd | 776 | |
a1ec6916 | 777 | SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0, |
1bbd0b84 | 778 | (SCM status), |
1e6808ea MG |
779 | "Return the signal number which stopped the process, if any,\n" |
780 | "otherwise @code{#f}.") | |
1bbd0b84 | 781 | #define FUNC_NAME s_scm_status_stop_sig |
67ec3667 | 782 | { |
e67dc2be JB |
783 | int lstatus; |
784 | ||
a55c2b68 | 785 | lstatus = scm_to_int (status); |
e67dc2be | 786 | if (WIFSTOPPED (lstatus)) |
a55c2b68 | 787 | return scm_from_int (WSTOPSIG (lstatus)); |
67ec3667 GH |
788 | else |
789 | return SCM_BOOL_F; | |
790 | } | |
1bbd0b84 | 791 | #undef FUNC_NAME |
82893676 | 792 | #endif /* __MINGW32__ */ |
0f2d19dd | 793 | |
82893676 | 794 | #ifdef HAVE_GETPPID |
a1ec6916 | 795 | SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0, |
1bbd0b84 | 796 | (), |
1e6808ea MG |
797 | "Return an integer representing the process ID of the parent\n" |
798 | "process.") | |
1bbd0b84 | 799 | #define FUNC_NAME s_scm_getppid |
0f2d19dd | 800 | { |
e11e83f3 | 801 | return scm_from_int (getppid ()); |
0f2d19dd | 802 | } |
1bbd0b84 | 803 | #undef FUNC_NAME |
82893676 | 804 | #endif /* HAVE_GETPPID */ |
0f2d19dd JB |
805 | |
806 | ||
82893676 | 807 | #ifndef __MINGW32__ |
a1ec6916 | 808 | SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0, |
1bbd0b84 | 809 | (), |
1e6808ea | 810 | "Return an integer representing the current real user ID.") |
1bbd0b84 | 811 | #define FUNC_NAME s_scm_getuid |
0f2d19dd | 812 | { |
e11e83f3 | 813 | return scm_from_int (getuid ()); |
0f2d19dd | 814 | } |
1bbd0b84 | 815 | #undef FUNC_NAME |
0f2d19dd JB |
816 | |
817 | ||
818 | ||
a1ec6916 | 819 | SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0, |
1bbd0b84 | 820 | (), |
1e6808ea | 821 | "Return an integer representing the current real group ID.") |
1bbd0b84 | 822 | #define FUNC_NAME s_scm_getgid |
0f2d19dd | 823 | { |
e11e83f3 | 824 | return scm_from_int (getgid ()); |
0f2d19dd | 825 | } |
1bbd0b84 | 826 | #undef FUNC_NAME |
0f2d19dd JB |
827 | |
828 | ||
829 | ||
a1ec6916 | 830 | SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0, |
1bbd0b84 | 831 | (), |
1e6808ea | 832 | "Return an integer representing the current effective user ID.\n" |
b380b885 | 833 | "If the system does not support effective IDs, then the real ID\n" |
480fa28d | 834 | "is returned. @code{(provided? 'EIDs)} reports whether the\n" |
1e6808ea | 835 | "system supports effective IDs.") |
1bbd0b84 | 836 | #define FUNC_NAME s_scm_geteuid |
0f2d19dd JB |
837 | { |
838 | #ifdef HAVE_GETEUID | |
e11e83f3 | 839 | return scm_from_int (geteuid ()); |
0f2d19dd | 840 | #else |
e11e83f3 | 841 | return scm_from_int (getuid ()); |
0f2d19dd JB |
842 | #endif |
843 | } | |
1bbd0b84 | 844 | #undef FUNC_NAME |
0f2d19dd JB |
845 | |
846 | ||
a1ec6916 | 847 | SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0, |
1bbd0b84 | 848 | (), |
1e6808ea | 849 | "Return an integer representing the current effective group ID.\n" |
b380b885 | 850 | "If the system does not support effective IDs, then the real ID\n" |
480fa28d | 851 | "is returned. @code{(provided? 'EIDs)} reports whether the\n" |
1e6808ea | 852 | "system supports effective IDs.") |
1bbd0b84 | 853 | #define FUNC_NAME s_scm_getegid |
0f2d19dd JB |
854 | { |
855 | #ifdef HAVE_GETEUID | |
e11e83f3 | 856 | return scm_from_int (getegid ()); |
0f2d19dd | 857 | #else |
e11e83f3 | 858 | return scm_from_int (getgid ()); |
0f2d19dd JB |
859 | #endif |
860 | } | |
1bbd0b84 | 861 | #undef FUNC_NAME |
0f2d19dd JB |
862 | |
863 | ||
a1ec6916 | 864 | SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0, |
1bbd0b84 | 865 | (SCM id), |
b380b885 MD |
866 | "Sets both the real and effective user IDs to the integer @var{id}, provided\n" |
867 | "the process has appropriate privileges.\n" | |
868 | "The return value is unspecified.") | |
1bbd0b84 | 869 | #define FUNC_NAME s_scm_setuid |
0f2d19dd | 870 | { |
a55c2b68 | 871 | if (setuid (scm_to_int (id)) != 0) |
1bbd0b84 | 872 | SCM_SYSERROR; |
02b754d3 | 873 | return SCM_UNSPECIFIED; |
0f2d19dd | 874 | } |
1bbd0b84 | 875 | #undef FUNC_NAME |
0f2d19dd | 876 | |
a1ec6916 | 877 | SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0, |
1bbd0b84 | 878 | (SCM id), |
b380b885 MD |
879 | "Sets both the real and effective group IDs to the integer @var{id}, provided\n" |
880 | "the process has appropriate privileges.\n" | |
881 | "The return value is unspecified.") | |
1bbd0b84 | 882 | #define FUNC_NAME s_scm_setgid |
0f2d19dd | 883 | { |
a55c2b68 | 884 | if (setgid (scm_to_int (id)) != 0) |
1bbd0b84 | 885 | SCM_SYSERROR; |
02b754d3 | 886 | return SCM_UNSPECIFIED; |
0f2d19dd | 887 | } |
1bbd0b84 | 888 | #undef FUNC_NAME |
0f2d19dd | 889 | |
a1ec6916 | 890 | SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0, |
1bbd0b84 | 891 | (SCM id), |
b380b885 MD |
892 | "Sets the effective user ID to the integer @var{id}, provided the process\n" |
893 | "has appropriate privileges. If effective IDs are not supported, the\n" | |
480fa28d | 894 | "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n" |
b380b885 MD |
895 | "system supports effective IDs.\n" |
896 | "The return value is unspecified.") | |
1bbd0b84 | 897 | #define FUNC_NAME s_scm_seteuid |
0f2d19dd | 898 | { |
02b754d3 GH |
899 | int rv; |
900 | ||
0f2d19dd | 901 | #ifdef HAVE_SETEUID |
a55c2b68 | 902 | rv = seteuid (scm_to_int (id)); |
0f2d19dd | 903 | #else |
a55c2b68 | 904 | rv = setuid (scm_to_int (id)); |
0f2d19dd | 905 | #endif |
02b754d3 | 906 | if (rv != 0) |
1bbd0b84 | 907 | SCM_SYSERROR; |
02b754d3 | 908 | return SCM_UNSPECIFIED; |
0f2d19dd | 909 | } |
1bbd0b84 | 910 | #undef FUNC_NAME |
7beabedb MG |
911 | #endif /* __MINGW32__ */ |
912 | ||
0f2d19dd | 913 | |
0e958795 | 914 | #ifdef HAVE_SETEGID |
a1ec6916 | 915 | SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0, |
1bbd0b84 | 916 | (SCM id), |
b380b885 MD |
917 | "Sets the effective group ID to the integer @var{id}, provided the process\n" |
918 | "has appropriate privileges. If effective IDs are not supported, the\n" | |
480fa28d | 919 | "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n" |
b380b885 MD |
920 | "system supports effective IDs.\n" |
921 | "The return value is unspecified.") | |
1bbd0b84 | 922 | #define FUNC_NAME s_scm_setegid |
0f2d19dd | 923 | { |
02b754d3 GH |
924 | int rv; |
925 | ||
0f2d19dd | 926 | #ifdef HAVE_SETEUID |
a55c2b68 | 927 | rv = setegid (scm_to_int (id)); |
0f2d19dd | 928 | #else |
a55c2b68 | 929 | rv = setgid (scm_to_int (id)); |
0f2d19dd | 930 | #endif |
02b754d3 | 931 | if (rv != 0) |
1bbd0b84 | 932 | SCM_SYSERROR; |
02b754d3 GH |
933 | return SCM_UNSPECIFIED; |
934 | ||
0f2d19dd | 935 | } |
1bbd0b84 | 936 | #undef FUNC_NAME |
0e958795 | 937 | #endif |
0f2d19dd | 938 | |
82893676 MG |
939 | |
940 | #ifdef HAVE_GETPGRP | |
a1ec6916 | 941 | SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0, |
1bbd0b84 | 942 | (), |
1e6808ea | 943 | "Return an integer representing the current process group ID.\n" |
b380b885 | 944 | "This is the POSIX definition, not BSD.") |
1bbd0b84 | 945 | #define FUNC_NAME s_scm_getpgrp |
0f2d19dd JB |
946 | { |
947 | int (*fn)(); | |
4625e44f | 948 | fn = (int (*) ()) getpgrp; |
e11e83f3 | 949 | return scm_from_int (fn (0)); |
0f2d19dd | 950 | } |
1bbd0b84 | 951 | #undef FUNC_NAME |
82893676 MG |
952 | #endif /* HAVE_GETPGRP */ |
953 | ||
0f2d19dd | 954 | |
f25f761d | 955 | #ifdef HAVE_SETPGID |
a1ec6916 | 956 | SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0, |
1bbd0b84 | 957 | (SCM pid, SCM pgid), |
b380b885 MD |
958 | "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n" |
959 | "@var{pgid} must be integers: they can be zero to indicate the ID of the\n" | |
960 | "current process.\n" | |
961 | "Fails on systems that do not support job control.\n" | |
962 | "The return value is unspecified.") | |
1bbd0b84 | 963 | #define FUNC_NAME s_scm_setpgid |
0f2d19dd | 964 | { |
02b754d3 | 965 | /* FIXME(?): may be known as setpgrp. */ |
a55c2b68 | 966 | if (setpgid (scm_to_int (pid), scm_to_int (pgid)) != 0) |
1bbd0b84 | 967 | SCM_SYSERROR; |
02b754d3 | 968 | return SCM_UNSPECIFIED; |
0f2d19dd | 969 | } |
1bbd0b84 | 970 | #undef FUNC_NAME |
f25f761d | 971 | #endif /* HAVE_SETPGID */ |
0f2d19dd | 972 | |
f25f761d | 973 | #ifdef HAVE_SETSID |
a1ec6916 | 974 | SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0, |
1bbd0b84 | 975 | (), |
b380b885 MD |
976 | "Creates a new session. The current process becomes the session leader\n" |
977 | "and is put in a new process group. The process will be detached\n" | |
978 | "from its controlling terminal if it has one.\n" | |
979 | "The return value is an integer representing the new process group ID.") | |
1bbd0b84 | 980 | #define FUNC_NAME s_scm_setsid |
0f2d19dd JB |
981 | { |
982 | pid_t sid = setsid (); | |
02b754d3 | 983 | if (sid == -1) |
1bbd0b84 | 984 | SCM_SYSERROR; |
02b754d3 | 985 | return SCM_UNSPECIFIED; |
0f2d19dd | 986 | } |
1bbd0b84 | 987 | #undef FUNC_NAME |
f25f761d | 988 | #endif /* HAVE_SETSID */ |
0f2d19dd | 989 | |
3e5aed1c NJ |
990 | #ifdef HAVE_GETSID |
991 | SCM_DEFINE (scm_getsid, "getsid", 1, 0, 0, | |
992 | (SCM pid), | |
993 | "Returns the session ID of process @var{pid}. (The session\n" | |
994 | "ID of a process is the process group ID of its session leader.)") | |
995 | #define FUNC_NAME s_scm_getsid | |
996 | { | |
997 | return scm_from_int (getsid (scm_to_int (pid))); | |
998 | } | |
999 | #undef FUNC_NAME | |
1000 | #endif /* HAVE_GETSID */ | |
1001 | ||
e8a59063 KR |
1002 | |
1003 | /* ttyname returns its result in a single static buffer, hence | |
1004 | scm_i_misc_mutex for thread safety. In glibc 2.3.2 two threads | |
1005 | continuously calling ttyname will otherwise get an overwrite quite | |
1006 | easily. | |
1007 | ||
1008 | ttyname_r (when available) could be used instead of scm_i_misc_mutex, but | |
1009 | there's probably little to be gained in either speed or parallelism. */ | |
1010 | ||
82893676 | 1011 | #ifdef HAVE_TTYNAME |
a1ec6916 | 1012 | SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0, |
1bbd0b84 | 1013 | (SCM port), |
1e6808ea MG |
1014 | "Return a string with the name of the serial terminal device\n" |
1015 | "underlying @var{port}.") | |
1bbd0b84 | 1016 | #define FUNC_NAME s_scm_ttyname |
0f2d19dd | 1017 | { |
1d1559ce | 1018 | char *result; |
e8a59063 | 1019 | int fd, err; |
a1ef7406 | 1020 | SCM ret = SCM_BOOL_F; |
78446828 MV |
1021 | |
1022 | port = SCM_COERCE_OUTPORT (port); | |
34d19ef6 | 1023 | SCM_VALIDATE_OPPORT (1, port); |
a98bddfd | 1024 | if (!SCM_FPORTP (port)) |
0f2d19dd | 1025 | return SCM_BOOL_F; |
ee149d03 | 1026 | fd = SCM_FPORT_FDES (port); |
e8a59063 | 1027 | |
9de87eea | 1028 | scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex); |
a1ef7406 | 1029 | |
1d1559ce | 1030 | SCM_SYSCALL (result = ttyname (fd)); |
e8a59063 | 1031 | err = errno; |
a1ef7406 LC |
1032 | if (result != NULL) |
1033 | result = strdup (result); | |
1034 | ||
9de87eea | 1035 | scm_i_pthread_mutex_unlock (&scm_i_misc_mutex); |
e8a59063 | 1036 | |
1d1559ce | 1037 | if (!result) |
e8a59063 KR |
1038 | { |
1039 | errno = err; | |
1040 | SCM_SYSERROR; | |
1041 | } | |
a1ef7406 LC |
1042 | else |
1043 | ret = scm_take_locale_string (result); | |
1044 | ||
e8a59063 | 1045 | return ret; |
0f2d19dd | 1046 | } |
1bbd0b84 | 1047 | #undef FUNC_NAME |
82893676 | 1048 | #endif /* HAVE_TTYNAME */ |
0f2d19dd | 1049 | |
e8a59063 | 1050 | |
0a9d83b0 KR |
1051 | /* For thread safety "buf" is used instead of NULL for the ctermid static |
1052 | buffer. Actually it's unlikely the controlling terminal will change | |
1053 | during program execution, and indeed on glibc (2.3.2) it's always just | |
1054 | "/dev/tty", but L_ctermid on the stack is easy and fast and guarantees | |
1055 | safety everywhere. */ | |
f25f761d | 1056 | #ifdef HAVE_CTERMID |
a1ec6916 | 1057 | SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0, |
1bbd0b84 | 1058 | (), |
1e6808ea MG |
1059 | "Return a string containing the file name of the controlling\n" |
1060 | "terminal for the current process.") | |
1bbd0b84 | 1061 | #define FUNC_NAME s_scm_ctermid |
0f2d19dd | 1062 | { |
0a9d83b0 KR |
1063 | char buf[L_ctermid]; |
1064 | char *result = ctermid (buf); | |
02b754d3 | 1065 | if (*result == '\0') |
1bbd0b84 | 1066 | SCM_SYSERROR; |
cc95e00a | 1067 | return scm_from_locale_string (result); |
0f2d19dd | 1068 | } |
1bbd0b84 | 1069 | #undef FUNC_NAME |
f25f761d | 1070 | #endif /* HAVE_CTERMID */ |
0f2d19dd | 1071 | |
f25f761d | 1072 | #ifdef HAVE_TCGETPGRP |
a1ec6916 | 1073 | SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0, |
1bbd0b84 | 1074 | (SCM port), |
1e6808ea MG |
1075 | "Return the process group ID of the foreground process group\n" |
1076 | "associated with the terminal open on the file descriptor\n" | |
1077 | "underlying @var{port}.\n" | |
1078 | "\n" | |
b380b885 MD |
1079 | "If there is no foreground process group, the return value is a\n" |
1080 | "number greater than 1 that does not match the process group ID\n" | |
1081 | "of any existing process group. This can happen if all of the\n" | |
1082 | "processes in the job that was formerly the foreground job have\n" | |
1083 | "terminated, and no other job has yet been moved into the\n" | |
1084 | "foreground.") | |
1bbd0b84 | 1085 | #define FUNC_NAME s_scm_tcgetpgrp |
0f2d19dd JB |
1086 | { |
1087 | int fd; | |
1088 | pid_t pgid; | |
78446828 MV |
1089 | |
1090 | port = SCM_COERCE_OUTPORT (port); | |
1091 | ||
34d19ef6 | 1092 | SCM_VALIDATE_OPFPORT (1, port); |
ee149d03 JB |
1093 | fd = SCM_FPORT_FDES (port); |
1094 | if ((pgid = tcgetpgrp (fd)) == -1) | |
1bbd0b84 | 1095 | SCM_SYSERROR; |
e11e83f3 | 1096 | return scm_from_int (pgid); |
1bbd0b84 GB |
1097 | } |
1098 | #undef FUNC_NAME | |
f25f761d | 1099 | #endif /* HAVE_TCGETPGRP */ |
0f2d19dd | 1100 | |
f25f761d | 1101 | #ifdef HAVE_TCSETPGRP |
a1ec6916 | 1102 | SCM_DEFINE (scm_tcsetpgrp, "tcsetpgrp", 2, 0, 0, |
1bbd0b84 | 1103 | (SCM port, SCM pgid), |
b380b885 MD |
1104 | "Set the foreground process group ID for the terminal used by the file\n" |
1105 | "descriptor underlying @var{port} to the integer @var{pgid}.\n" | |
1106 | "The calling process\n" | |
1107 | "must be a member of the same session as @var{pgid} and must have the same\n" | |
1108 | "controlling terminal. The return value is unspecified.") | |
1bbd0b84 | 1109 | #define FUNC_NAME s_scm_tcsetpgrp |
0f2d19dd JB |
1110 | { |
1111 | int fd; | |
78446828 MV |
1112 | |
1113 | port = SCM_COERCE_OUTPORT (port); | |
1114 | ||
34d19ef6 | 1115 | SCM_VALIDATE_OPFPORT (1, port); |
ee149d03 | 1116 | fd = SCM_FPORT_FDES (port); |
a55c2b68 | 1117 | if (tcsetpgrp (fd, scm_to_int (pgid)) == -1) |
1bbd0b84 | 1118 | SCM_SYSERROR; |
02b754d3 | 1119 | return SCM_UNSPECIFIED; |
1bbd0b84 GB |
1120 | } |
1121 | #undef FUNC_NAME | |
f25f761d | 1122 | #endif /* HAVE_TCSETPGRP */ |
0f2d19dd | 1123 | |
a1ec6916 | 1124 | SCM_DEFINE (scm_execl, "execl", 1, 0, 1, |
1bbd0b84 | 1125 | (SCM filename, SCM args), |
b380b885 MD |
1126 | "Executes the file named by @var{path} as a new process image.\n" |
1127 | "The remaining arguments are supplied to the process; from a C program\n" | |
bb2c02f2 | 1128 | "they are accessible as the @code{argv} argument to @code{main}.\n" |
b380b885 | 1129 | "Conventionally the first @var{arg} is the same as @var{path}.\n" |
8f85c0c6 | 1130 | "All arguments must be strings.\n\n" |
b380b885 MD |
1131 | "If @var{arg} is missing, @var{path} is executed with a null\n" |
1132 | "argument list, which may have system-dependent side-effects.\n\n" | |
1133 | "This procedure is currently implemented using the @code{execv} system\n" | |
1134 | "call, but we call it @code{execl} because of its Scheme calling interface.") | |
1bbd0b84 | 1135 | #define FUNC_NAME s_scm_execl |
0f2d19dd | 1136 | { |
7f03f970 MV |
1137 | char *exec_file; |
1138 | char **exec_argv; | |
1139 | ||
661ae7ab | 1140 | scm_dynwind_begin (0); |
7f03f970 MV |
1141 | |
1142 | exec_file = scm_to_locale_string (filename); | |
661ae7ab | 1143 | scm_dynwind_free (exec_file); |
7f03f970 MV |
1144 | |
1145 | exec_argv = scm_i_allocate_string_pointers (args); | |
7f03f970 | 1146 | |
8ab3d8a0 KR |
1147 | execv (exec_file, |
1148 | #ifdef __MINGW32__ | |
1149 | /* extra "const" in mingw formals, provokes warning from gcc */ | |
1150 | (const char * const *) | |
1151 | #endif | |
1152 | exec_argv); | |
1bbd0b84 | 1153 | SCM_SYSERROR; |
7f03f970 | 1154 | |
02b754d3 | 1155 | /* not reached. */ |
661ae7ab | 1156 | scm_dynwind_end (); |
02b754d3 | 1157 | return SCM_BOOL_F; |
0f2d19dd | 1158 | } |
1bbd0b84 | 1159 | #undef FUNC_NAME |
0f2d19dd | 1160 | |
a1ec6916 | 1161 | SCM_DEFINE (scm_execlp, "execlp", 1, 0, 1, |
1bbd0b84 | 1162 | (SCM filename, SCM args), |
b380b885 MD |
1163 | "Similar to @code{execl}, however if\n" |
1164 | "@var{filename} does not contain a slash\n" | |
1165 | "then the file to execute will be located by searching the\n" | |
1166 | "directories listed in the @code{PATH} environment variable.\n\n" | |
6bcd01fc | 1167 | "This procedure is currently implemented using the @code{execvp} system\n" |
b380b885 | 1168 | "call, but we call it @code{execlp} because of its Scheme calling interface.") |
1bbd0b84 | 1169 | #define FUNC_NAME s_scm_execlp |
0f2d19dd | 1170 | { |
7f03f970 MV |
1171 | char *exec_file; |
1172 | char **exec_argv; | |
1173 | ||
661ae7ab | 1174 | scm_dynwind_begin (0); |
7f03f970 MV |
1175 | |
1176 | exec_file = scm_to_locale_string (filename); | |
661ae7ab | 1177 | scm_dynwind_free (exec_file); |
7f03f970 MV |
1178 | |
1179 | exec_argv = scm_i_allocate_string_pointers (args); | |
7f03f970 | 1180 | |
8ab3d8a0 KR |
1181 | execvp (exec_file, |
1182 | #ifdef __MINGW32__ | |
1183 | /* extra "const" in mingw formals, provokes warning from gcc */ | |
1184 | (const char * const *) | |
1185 | #endif | |
1186 | exec_argv); | |
1bbd0b84 | 1187 | SCM_SYSERROR; |
7f03f970 | 1188 | |
02b754d3 | 1189 | /* not reached. */ |
661ae7ab | 1190 | scm_dynwind_end (); |
02b754d3 | 1191 | return SCM_BOOL_F; |
0f2d19dd | 1192 | } |
1bbd0b84 | 1193 | #undef FUNC_NAME |
0f2d19dd | 1194 | |
6afcd3b2 | 1195 | |
807f353f KR |
1196 | /* OPTIMIZE-ME: scm_execle doesn't need malloced copies of the environment |
1197 | list strings the way environ_list_to_c gives. */ | |
1198 | ||
a1ec6916 | 1199 | SCM_DEFINE (scm_execle, "execle", 2, 0, 1, |
1bbd0b84 | 1200 | (SCM filename, SCM env, SCM args), |
b380b885 MD |
1201 | "Similar to @code{execl}, but the environment of the new process is\n" |
1202 | "specified by @var{env}, which must be a list of strings as returned by the\n" | |
1203 | "@code{environ} procedure.\n\n" | |
1204 | "This procedure is currently implemented using the @code{execve} system\n" | |
1205 | "call, but we call it @code{execle} because of its Scheme calling interface.") | |
1bbd0b84 | 1206 | #define FUNC_NAME s_scm_execle |
6afcd3b2 | 1207 | { |
7f03f970 | 1208 | char **exec_argv; |
6afcd3b2 | 1209 | char **exec_env; |
7f03f970 | 1210 | char *exec_file; |
6afcd3b2 | 1211 | |
661ae7ab | 1212 | scm_dynwind_begin (0); |
7f03f970 MV |
1213 | |
1214 | exec_file = scm_to_locale_string (filename); | |
661ae7ab | 1215 | scm_dynwind_free (exec_file); |
7f03f970 MV |
1216 | |
1217 | exec_argv = scm_i_allocate_string_pointers (args); | |
7f03f970 | 1218 | exec_env = scm_i_allocate_string_pointers (env); |
7f03f970 | 1219 | |
8ab3d8a0 KR |
1220 | execve (exec_file, |
1221 | #ifdef __MINGW32__ | |
1222 | /* extra "const" in mingw formals, provokes warning from gcc */ | |
1223 | (const char * const *) | |
1224 | #endif | |
1225 | exec_argv, | |
1226 | #ifdef __MINGW32__ | |
1227 | /* extra "const" in mingw formals, provokes warning from gcc */ | |
1228 | (const char * const *) | |
1229 | #endif | |
1230 | exec_env); | |
1bbd0b84 | 1231 | SCM_SYSERROR; |
7f03f970 | 1232 | |
6afcd3b2 | 1233 | /* not reached. */ |
661ae7ab | 1234 | scm_dynwind_end (); |
6afcd3b2 GH |
1235 | return SCM_BOOL_F; |
1236 | } | |
1bbd0b84 | 1237 | #undef FUNC_NAME |
6afcd3b2 | 1238 | |
82893676 | 1239 | #ifdef HAVE_FORK |
a1ec6916 | 1240 | SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0, |
1bbd0b84 | 1241 | (), |
b380b885 MD |
1242 | "Creates a new \"child\" process by duplicating the current \"parent\" process.\n" |
1243 | "In the child the return value is 0. In the parent the return value is\n" | |
1244 | "the integer process ID of the child.\n\n" | |
1245 | "This procedure has been renamed from @code{fork} to avoid a naming conflict\n" | |
1246 | "with the scsh fork.") | |
1bbd0b84 | 1247 | #define FUNC_NAME s_scm_fork |
0f2d19dd | 1248 | { |
bab0f4e5 | 1249 | int pid; |
0f2d19dd JB |
1250 | pid = fork (); |
1251 | if (pid == -1) | |
1bbd0b84 | 1252 | SCM_SYSERROR; |
e11e83f3 | 1253 | return scm_from_int (pid); |
0f2d19dd | 1254 | } |
1bbd0b84 | 1255 | #undef FUNC_NAME |
82893676 | 1256 | #endif /* HAVE_FORK */ |
0f2d19dd | 1257 | |
4f68365d MV |
1258 | #ifdef __MINGW32__ |
1259 | # include "win32-uname.h" | |
1260 | #endif | |
1261 | ||
1262 | #if defined (HAVE_UNAME) || defined (__MINGW32__) | |
a1ec6916 | 1263 | SCM_DEFINE (scm_uname, "uname", 0, 0, 0, |
1bbd0b84 | 1264 | (), |
1e6808ea MG |
1265 | "Return an object with some information about the computer\n" |
1266 | "system the program is running on.") | |
1bbd0b84 | 1267 | #define FUNC_NAME s_scm_uname |
0f2d19dd | 1268 | { |
0f2d19dd | 1269 | struct utsname buf; |
1d1559ce | 1270 | SCM result = scm_c_make_vector (5, SCM_UNSPECIFIED); |
e1a191a8 | 1271 | if (uname (&buf) < 0) |
1bbd0b84 | 1272 | SCM_SYSERROR; |
4057a3e0 MV |
1273 | SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (buf.sysname)); |
1274 | SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (buf.nodename)); | |
1275 | SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_locale_string (buf.release)); | |
1276 | SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_locale_string (buf.version)); | |
1277 | SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (buf.machine)); | |
0f2d19dd | 1278 | /* |
02b754d3 | 1279 | a linux special? |
4057a3e0 | 1280 | SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (buf.domainname)); |
0f2d19dd | 1281 | */ |
1d1559ce | 1282 | return result; |
0f2d19dd | 1283 | } |
1bbd0b84 | 1284 | #undef FUNC_NAME |
f25f761d | 1285 | #endif /* HAVE_UNAME */ |
0f2d19dd | 1286 | |
a1ec6916 | 1287 | SCM_DEFINE (scm_environ, "environ", 0, 1, 0, |
1bbd0b84 | 1288 | (SCM env), |
1e6808ea MG |
1289 | "If @var{env} is omitted, return the current environment (in the\n" |
1290 | "Unix sense) as a list of strings. Otherwise set the current\n" | |
1291 | "environment, which is also the default environment for child\n" | |
1292 | "processes, to the supplied list of strings. Each member of\n" | |
1293 | "@var{env} should be of the form @code{NAME=VALUE} and values of\n" | |
1294 | "@code{NAME} should not be duplicated. If @var{env} is supplied\n" | |
1295 | "then the return value is unspecified.") | |
1bbd0b84 | 1296 | #define FUNC_NAME s_scm_environ |
0f2d19dd JB |
1297 | { |
1298 | if (SCM_UNBNDP (env)) | |
1299 | return scm_makfromstrs (-1, environ); | |
1300 | else | |
1301 | { | |
2a776823 | 1302 | environ = scm_i_allocate_string_pointers (env); |
0f2d19dd JB |
1303 | return SCM_UNSPECIFIED; |
1304 | } | |
1305 | } | |
1bbd0b84 | 1306 | #undef FUNC_NAME |
0f2d19dd | 1307 | |
9ee5fce4 MD |
1308 | #ifdef L_tmpnam |
1309 | ||
a1ec6916 | 1310 | SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0, |
1bbd0b84 | 1311 | (), |
1e6808ea MG |
1312 | "Return a name in the file system that does not match any\n" |
1313 | "existing file. However there is no guarantee that another\n" | |
1314 | "process will not create the file after @code{tmpnam} is called.\n" | |
1315 | "Care should be taken if opening the file, e.g., use the\n" | |
1316 | "@code{O_EXCL} open flag or use @code{mkstemp!} instead.") | |
1bbd0b84 | 1317 | #define FUNC_NAME s_scm_tmpnam |
9ee5fce4 MD |
1318 | { |
1319 | char name[L_tmpnam]; | |
6d163216 GH |
1320 | char *rv; |
1321 | ||
1322 | SCM_SYSCALL (rv = tmpnam (name)); | |
1323 | if (rv == NULL) | |
1324 | /* not SCM_SYSERROR since errno probably not set. */ | |
1325 | SCM_MISC_ERROR ("tmpnam failed", SCM_EOL); | |
cc95e00a | 1326 | return scm_from_locale_string (name); |
9ee5fce4 | 1327 | } |
0f981281 | 1328 | #undef FUNC_NAME |
0f2d19dd | 1329 | |
1bbd0b84 | 1330 | #endif |
1cc91f1b | 1331 | |
cd53bd36 TTN |
1332 | SCM_DEFINE (scm_tmpfile, "tmpfile", 0, 0, 0, |
1333 | (void), | |
1334 | "Return an input/output port to a unique temporary file\n" | |
1335 | "named using the path prefix @code{P_tmpdir} defined in\n" | |
1336 | "@file{stdio.h}.\n" | |
1337 | "The file is automatically deleted when the port is closed\n" | |
1338 | "or the program terminates.") | |
1339 | #define FUNC_NAME s_scm_tmpfile | |
1340 | { | |
1341 | FILE *rv; | |
1342 | ||
1343 | if (! (rv = tmpfile ())) | |
1344 | SCM_SYSERROR; | |
1345 | return scm_fdes_to_port (fileno (rv), "w+", SCM_BOOL_F); | |
1346 | } | |
1347 | #undef FUNC_NAME | |
1348 | ||
06bfe276 AW |
1349 | SCM_DEFINE (scm_utime, "utime", 1, 5, 0, |
1350 | (SCM pathname, SCM actime, SCM modtime, SCM actimens, SCM modtimens, | |
1351 | SCM flags), | |
1e6808ea MG |
1352 | "@code{utime} sets the access and modification times for the\n" |
1353 | "file named by @var{path}. If @var{actime} or @var{modtime} is\n" | |
1354 | "not supplied, then the current time is used. @var{actime} and\n" | |
1355 | "@var{modtime} must be integer time values as returned by the\n" | |
06bfe276 AW |
1356 | "@code{current-time} procedure.\n\n" |
1357 | "The optional @var{actimens} and @var{modtimens} are nanoseconds\n" | |
1358 | "to add @var{actime} and @var{modtime}. Nanosecond precision is\n" | |
477e4219 | 1359 | "only supported on some combinations of file systems and operating\n" |
06bfe276 | 1360 | "systems.\n" |
1e6808ea | 1361 | "@lisp\n" |
b380b885 | 1362 | "(utime \"foo\" (- (current-time) 3600))\n" |
1e6808ea MG |
1363 | "@end lisp\n" |
1364 | "will set the access time to one hour in the past and the\n" | |
1365 | "modification time to the current time.") | |
1bbd0b84 | 1366 | #define FUNC_NAME s_scm_utime |
0f2d19dd JB |
1367 | { |
1368 | int rv; | |
06bfe276 AW |
1369 | time_t atim_sec, mtim_sec; |
1370 | long atim_nsec, mtim_nsec; | |
1371 | int f; | |
1372 | ||
0f2d19dd | 1373 | if (SCM_UNBNDP (actime)) |
06bfe276 | 1374 | { |
adfb4284 | 1375 | #ifdef HAVE_UTIMENSAT |
06bfe276 AW |
1376 | atim_sec = 0; |
1377 | atim_nsec = UTIME_NOW; | |
1378 | #else | |
1379 | SCM_SYSCALL (time (&atim_sec)); | |
1380 | atim_nsec = 0; | |
1381 | #endif | |
1382 | } | |
0f2d19dd | 1383 | else |
06bfe276 AW |
1384 | { |
1385 | atim_sec = SCM_NUM2ULONG (2, actime); | |
1386 | if (SCM_UNBNDP (actimens)) | |
1387 | atim_nsec = 0; | |
1388 | else | |
1389 | atim_nsec = SCM_NUM2LONG (4, actimens); | |
1390 | } | |
1391 | ||
0f2d19dd | 1392 | if (SCM_UNBNDP (modtime)) |
06bfe276 | 1393 | { |
adfb4284 | 1394 | #ifdef HAVE_UTIMENSAT |
06bfe276 AW |
1395 | mtim_sec = 0; |
1396 | mtim_nsec = UTIME_NOW; | |
1397 | #else | |
1398 | SCM_SYSCALL (time (&mtim_sec)); | |
1399 | mtim_nsec = 0; | |
1400 | #endif | |
1401 | } | |
0f2d19dd | 1402 | else |
06bfe276 AW |
1403 | { |
1404 | mtim_sec = SCM_NUM2ULONG (3, modtime); | |
1405 | if (SCM_UNBNDP (modtimens)) | |
1406 | mtim_nsec = 0; | |
1407 | else | |
1408 | mtim_nsec = SCM_NUM2LONG (5, modtimens); | |
1409 | } | |
1410 | ||
1411 | if (SCM_UNBNDP (flags)) | |
1412 | f = 0; | |
1413 | else | |
1414 | f = SCM_NUM2INT (6, flags); | |
1415 | ||
adfb4284 | 1416 | #ifdef HAVE_UTIMENSAT |
06bfe276 AW |
1417 | { |
1418 | struct timespec times[2]; | |
1419 | times[0].tv_sec = atim_sec; | |
1420 | times[0].tv_nsec = atim_nsec; | |
1421 | times[1].tv_sec = mtim_sec; | |
1422 | times[1].tv_nsec = mtim_nsec; | |
1423 | ||
1424 | STRING_SYSCALL (pathname, c_pathname, | |
adfb4284 | 1425 | rv = utimensat (AT_FDCWD, c_pathname, times, f)); |
06bfe276 AW |
1426 | } |
1427 | #else | |
1428 | { | |
1429 | struct utimbuf utm; | |
1430 | utm.actime = atim_sec; | |
1431 | utm.modtime = mtim_sec; | |
1432 | ||
1433 | STRING_SYSCALL (pathname, c_pathname, | |
1434 | rv = utime (c_pathname, &utm)); | |
1435 | } | |
1436 | #endif | |
0f2d19dd | 1437 | |
02b754d3 | 1438 | if (rv != 0) |
1bbd0b84 | 1439 | SCM_SYSERROR; |
02b754d3 | 1440 | return SCM_UNSPECIFIED; |
0f2d19dd | 1441 | } |
1bbd0b84 | 1442 | #undef FUNC_NAME |
0f2d19dd | 1443 | |
a1ec6916 | 1444 | SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0, |
1bbd0b84 | 1445 | (), |
1e6808ea | 1446 | "Return an integer representing the current process ID.") |
1bbd0b84 | 1447 | #define FUNC_NAME s_scm_getpid |
0f2d19dd | 1448 | { |
e11e83f3 | 1449 | return scm_from_ulong (getpid ()); |
0f2d19dd | 1450 | } |
1bbd0b84 | 1451 | #undef FUNC_NAME |
0f2d19dd | 1452 | |
a1ec6916 | 1453 | SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0, |
1bbd0b84 | 1454 | (SCM str), |
b380b885 MD |
1455 | "Modifies the environment of the current process, which is\n" |
1456 | "also the default environment inherited by child processes.\n\n" | |
1457 | "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n" | |
1458 | "directly into the environment, replacing any existing environment string\n" | |
1459 | "with\n" | |
1460 | "name matching @code{NAME}. If @var{string} does not contain an equal\n" | |
1461 | "sign, then any existing string with name matching @var{string} will\n" | |
1462 | "be removed.\n\n" | |
1463 | "The return value is unspecified.") | |
1bbd0b84 | 1464 | #define FUNC_NAME s_scm_putenv |
0f2d19dd | 1465 | { |
f158788f | 1466 | int rv; |
f015614a | 1467 | char *c_str = scm_to_locale_string (str); |
002409fe | 1468 | |
3731192f | 1469 | /* Leave C_STR in the environment. */ |
fcc5d734 | 1470 | |
3731192f LC |
1471 | /* Gnulib's `putenv' module honors the semantics described above. */ |
1472 | rv = putenv (c_str); | |
1473 | if (rv < 0) | |
1474 | SCM_SYSERROR; | |
f015614a | 1475 | |
f93ddd39 | 1476 | return SCM_UNSPECIFIED; |
0f2d19dd | 1477 | } |
1bbd0b84 | 1478 | #undef FUNC_NAME |
0f2d19dd | 1479 | |
b89c4943 | 1480 | /* This mutex is used to serialize invocations of `setlocale ()' on non-GNU |
a2f00b9b LC |
1481 | systems (i.e., systems where a reentrant locale API is not available). It |
1482 | is also acquired before calls to `nl_langinfo ()'. See `i18n.c' for | |
1483 | details. */ | |
1484 | scm_i_pthread_mutex_t scm_i_locale_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER; | |
b89c4943 | 1485 | |
f25f761d | 1486 | #ifdef HAVE_SETLOCALE |
b89c4943 | 1487 | |
a1ec6916 | 1488 | SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0, |
1bbd0b84 | 1489 | (SCM category, SCM locale), |
1e6808ea MG |
1490 | "If @var{locale} is omitted, return the current value of the\n" |
1491 | "specified locale category as a system-dependent string.\n" | |
1492 | "@var{category} should be specified using the values\n" | |
1493 | "@code{LC_COLLATE}, @code{LC_ALL} etc.\n" | |
1494 | "\n" | |
1495 | "Otherwise the specified locale category is set to the string\n" | |
1496 | "@var{locale} and the new value is returned as a\n" | |
1497 | "system-dependent string. If @var{locale} is an empty string,\n" | |
889975e5 MG |
1498 | "the locale will be set using environment variables.\n" |
1499 | "\n" | |
1500 | "When the locale is changed, the character encoding of the new\n" | |
1501 | "locale (UTF-8, ISO-8859-1, etc.) is used for the current\n" | |
1502 | "input, output, and error ports\n") | |
1bbd0b84 | 1503 | #define FUNC_NAME s_scm_setlocale |
0f2d19dd | 1504 | { |
a2f00b9b | 1505 | int c_category; |
0f2d19dd JB |
1506 | char *clocale; |
1507 | char *rv; | |
889975e5 | 1508 | const char *enc; |
0f2d19dd | 1509 | |
661ae7ab | 1510 | scm_dynwind_begin (0); |
f015614a | 1511 | |
0f2d19dd JB |
1512 | if (SCM_UNBNDP (locale)) |
1513 | { | |
1514 | clocale = NULL; | |
1515 | } | |
1516 | else | |
1517 | { | |
f015614a | 1518 | clocale = scm_to_locale_string (locale); |
661ae7ab | 1519 | scm_dynwind_free (clocale); |
0f2d19dd JB |
1520 | } |
1521 | ||
a2f00b9b LC |
1522 | c_category = scm_i_to_lc_category (category, 1); |
1523 | ||
b89c4943 | 1524 | scm_i_pthread_mutex_lock (&scm_i_locale_mutex); |
a2f00b9b | 1525 | rv = setlocale (c_category, clocale); |
b89c4943 | 1526 | scm_i_pthread_mutex_unlock (&scm_i_locale_mutex); |
b89c4943 | 1527 | |
02b754d3 | 1528 | if (rv == NULL) |
41b74b7d KR |
1529 | { |
1530 | /* POSIX and C99 don't say anything about setlocale setting errno, so | |
1531 | force a sensible value here. glibc leaves ENOENT, which would be | |
1532 | fine, but it's not a documented feature. */ | |
1533 | errno = EINVAL; | |
1534 | SCM_SYSERROR; | |
1535 | } | |
f015614a | 1536 | |
889975e5 | 1537 | enc = locale_charset (); |
9d9c66ba | 1538 | |
889975e5 | 1539 | /* Set the default encoding for new ports. */ |
9d9c66ba LC |
1540 | scm_i_set_default_port_encoding (enc); |
1541 | ||
889975e5 MG |
1542 | /* Set the encoding for the stdio ports. */ |
1543 | scm_i_set_port_encoding_x (scm_current_input_port (), enc); | |
1544 | scm_i_set_port_encoding_x (scm_current_output_port (), enc); | |
1545 | scm_i_set_port_encoding_x (scm_current_error_port (), enc); | |
a17d2654 | 1546 | |
661ae7ab | 1547 | scm_dynwind_end (); |
f015614a | 1548 | return scm_from_locale_string (rv); |
0f2d19dd | 1549 | } |
1bbd0b84 | 1550 | #undef FUNC_NAME |
f25f761d | 1551 | #endif /* HAVE_SETLOCALE */ |
0f2d19dd | 1552 | |
f25f761d | 1553 | #ifdef HAVE_MKNOD |
a1ec6916 | 1554 | SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0, |
1bbd0b84 | 1555 | (SCM path, SCM type, SCM perms, SCM dev), |
b380b885 MD |
1556 | "Creates a new special file, such as a file corresponding to a device.\n" |
1557 | "@var{path} specifies the name of the file. @var{type} should\n" | |
1558 | "be one of the following symbols:\n" | |
1559 | "regular, directory, symlink, block-special, char-special,\n" | |
1560 | "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n" | |
1561 | "@var{dev} (an integer) specifies which device the special file refers\n" | |
1562 | "to. Its exact interpretation depends on the kind of special file\n" | |
1563 | "being created.\n\n" | |
1564 | "E.g.,\n" | |
1e6808ea | 1565 | "@lisp\n" |
09831f94 | 1566 | "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n" |
1e6808ea | 1567 | "@end lisp\n\n" |
a3c8b9fc | 1568 | "The return value is unspecified.") |
1bbd0b84 | 1569 | #define FUNC_NAME s_scm_mknod |
0f2d19dd | 1570 | { |
0f2d19dd | 1571 | int val; |
cc95e00a | 1572 | const char *p; |
82a76bdf | 1573 | int ctype = 0; |
19468eff | 1574 | |
a6d9e5ab | 1575 | SCM_VALIDATE_STRING (1, path); |
34d19ef6 | 1576 | SCM_VALIDATE_SYMBOL (2, type); |
19468eff | 1577 | |
cc95e00a | 1578 | p = scm_i_symbol_chars (type); |
19468eff GH |
1579 | if (strcmp (p, "regular") == 0) |
1580 | ctype = S_IFREG; | |
1581 | else if (strcmp (p, "directory") == 0) | |
1582 | ctype = S_IFDIR; | |
23f2b9a3 KR |
1583 | #ifdef S_IFLNK |
1584 | /* systems without symlinks probably don't have S_IFLNK defined */ | |
19468eff GH |
1585 | else if (strcmp (p, "symlink") == 0) |
1586 | ctype = S_IFLNK; | |
23f2b9a3 | 1587 | #endif |
19468eff GH |
1588 | else if (strcmp (p, "block-special") == 0) |
1589 | ctype = S_IFBLK; | |
1590 | else if (strcmp (p, "char-special") == 0) | |
1591 | ctype = S_IFCHR; | |
1592 | else if (strcmp (p, "fifo") == 0) | |
1593 | ctype = S_IFIFO; | |
e655d034 | 1594 | #ifdef S_IFSOCK |
19468eff GH |
1595 | else if (strcmp (p, "socket") == 0) |
1596 | ctype = S_IFSOCK; | |
e655d034 | 1597 | #endif |
19468eff | 1598 | else |
34d19ef6 | 1599 | SCM_OUT_OF_RANGE (2, type); |
19468eff | 1600 | |
f015614a MV |
1601 | STRING_SYSCALL (path, c_path, |
1602 | val = mknod (c_path, | |
1603 | ctype | scm_to_int (perms), | |
1604 | scm_to_int (dev))); | |
02b754d3 | 1605 | if (val != 0) |
1bbd0b84 | 1606 | SCM_SYSERROR; |
02b754d3 | 1607 | return SCM_UNSPECIFIED; |
0f2d19dd | 1608 | } |
1bbd0b84 | 1609 | #undef FUNC_NAME |
f25f761d | 1610 | #endif /* HAVE_MKNOD */ |
0f2d19dd | 1611 | |
f25f761d | 1612 | #ifdef HAVE_NICE |
a1ec6916 | 1613 | SCM_DEFINE (scm_nice, "nice", 1, 0, 0, |
1bbd0b84 | 1614 | (SCM incr), |
b380b885 MD |
1615 | "Increment the priority of the current process by @var{incr}. A higher\n" |
1616 | "priority value means that the process runs less often.\n" | |
1617 | "The return value is unspecified.") | |
1bbd0b84 | 1618 | #define FUNC_NAME s_scm_nice |
0f2d19dd | 1619 | { |
f1c82f55 KR |
1620 | /* nice() returns "prio-NZERO" on success or -1 on error, but -1 can arise |
1621 | from "prio-NZERO", so an error must be detected from errno changed */ | |
1622 | errno = 0; | |
03976fee | 1623 | nice (scm_to_int (incr)); |
f1c82f55 | 1624 | if (errno != 0) |
1bbd0b84 | 1625 | SCM_SYSERROR; |
634aa8de | 1626 | |
02b754d3 | 1627 | return SCM_UNSPECIFIED; |
0f2d19dd | 1628 | } |
1bbd0b84 | 1629 | #undef FUNC_NAME |
f25f761d | 1630 | #endif /* HAVE_NICE */ |
0f2d19dd | 1631 | |
f25f761d | 1632 | #ifdef HAVE_SYNC |
a1ec6916 | 1633 | SCM_DEFINE (scm_sync, "sync", 0, 0, 0, |
1bbd0b84 | 1634 | (), |
b380b885 MD |
1635 | "Flush the operating system disk buffers.\n" |
1636 | "The return value is unspecified.") | |
1bbd0b84 | 1637 | #define FUNC_NAME s_scm_sync |
0f2d19dd | 1638 | { |
0f2d19dd | 1639 | sync(); |
127ec750 | 1640 | return SCM_UNSPECIFIED; |
0f2d19dd | 1641 | } |
1bbd0b84 | 1642 | #undef FUNC_NAME |
f25f761d | 1643 | #endif /* HAVE_SYNC */ |
0f2d19dd | 1644 | |
33bea692 KR |
1645 | |
1646 | /* crypt() returns a pointer to a static buffer, so we use scm_i_misc_mutex | |
1647 | to avoid another thread overwriting it. A test program running crypt | |
1648 | continuously in two threads can be quickly seen tripping this problem. | |
1649 | crypt() is pretty slow normally, so a mutex shouldn't add much overhead. | |
1650 | ||
1651 | glibc has a thread-safe crypt_r, but (in version 2.3.2) it runs a lot | |
1652 | slower (about 5x) than plain crypt if you pass an uninitialized data | |
1653 | block each time. Presumably there's some one-time setups. The best way | |
1654 | to use crypt_r for parallel execution in multiple threads would probably | |
1655 | be to maintain a little pool of initialized crypt_data structures, take | |
1656 | one and use it, then return it to the pool. That pool could be garbage | |
1657 | collected so it didn't add permanently to memory use if only a few crypt | |
1658 | calls are made. But we expect crypt will be used rarely, and even more | |
1659 | rarely will there be any desire for lots of parallel execution on | |
1660 | multiple cpus. So for now we don't bother with anything fancy, just | |
1661 | ensure it works. */ | |
1662 | ||
534bbcc1 | 1663 | #ifdef HAVE_CRYPT |
33bea692 | 1664 | SCM_DEFINE (scm_crypt, "crypt", 2, 0, 0, |
94e6d793 MG |
1665 | (SCM key, SCM salt), |
1666 | "Encrypt @var{key} using @var{salt} as the salt value to the\n" | |
9401323e | 1667 | "crypt(3) library call.") |
94e6d793 MG |
1668 | #define FUNC_NAME s_scm_crypt |
1669 | { | |
33bea692 | 1670 | SCM ret; |
23d72566 | 1671 | char *c_key, *c_salt, *c_ret; |
94e6d793 | 1672 | |
661ae7ab MV |
1673 | scm_dynwind_begin (0); |
1674 | scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex); | |
33bea692 | 1675 | |
f015614a | 1676 | c_key = scm_to_locale_string (key); |
661ae7ab | 1677 | scm_dynwind_free (c_key); |
f015614a | 1678 | c_salt = scm_to_locale_string (salt); |
661ae7ab | 1679 | scm_dynwind_free (c_salt); |
f015614a | 1680 | |
23d72566 KR |
1681 | /* The Linux crypt(3) man page says crypt will return NULL and set errno |
1682 | on error. (Eg. ENOSYS if legal restrictions mean it cannot be | |
1683 | implemented). */ | |
1684 | c_ret = crypt (c_key, c_salt); | |
1685 | if (c_ret == NULL) | |
1686 | SCM_SYSERROR; | |
33bea692 | 1687 | |
23d72566 | 1688 | ret = scm_from_locale_string (c_ret); |
661ae7ab | 1689 | scm_dynwind_end (); |
33bea692 | 1690 | return ret; |
94e6d793 MG |
1691 | } |
1692 | #undef FUNC_NAME | |
b9a7b725 | 1693 | #endif /* HAVE_CRYPT */ |
94e6d793 MG |
1694 | |
1695 | #if HAVE_CHROOT | |
1696 | SCM_DEFINE (scm_chroot, "chroot", 1, 0, 0, | |
1697 | (SCM path), | |
1698 | "Change the root directory to that specified in @var{path}.\n" | |
1699 | "This directory will be used for path names beginning with\n" | |
1700 | "@file{/}. The root directory is inherited by all children\n" | |
1701 | "of the current process. Only the superuser may change the\n" | |
1702 | "root directory.") | |
1703 | #define FUNC_NAME s_scm_chroot | |
1704 | { | |
f015614a | 1705 | int rv; |
94e6d793 | 1706 | |
f015614a MV |
1707 | WITH_STRING (path, c_path, |
1708 | rv = chroot (c_path)); | |
1709 | if (rv == -1) | |
94e6d793 MG |
1710 | SCM_SYSERROR; |
1711 | return SCM_UNSPECIFIED; | |
1712 | } | |
1713 | #undef FUNC_NAME | |
1714 | #endif /* HAVE_CHROOT */ | |
1715 | ||
7beabedb MG |
1716 | |
1717 | #ifdef __MINGW32__ | |
1718 | /* Wrapper function to supplying `getlogin()' under Windows. */ | |
1719 | static char * getlogin (void) | |
1720 | { | |
1721 | static char user[256]; | |
1722 | static unsigned long len = 256; | |
1723 | ||
1724 | if (!GetUserName (user, &len)) | |
1725 | return NULL; | |
1726 | return user; | |
1727 | } | |
1728 | #endif /* __MINGW32__ */ | |
1729 | ||
1730 | ||
4f68365d | 1731 | #if defined (HAVE_GETLOGIN) || defined (__MINGW32__) |
94e6d793 MG |
1732 | SCM_DEFINE (scm_getlogin, "getlogin", 0, 0, 0, |
1733 | (void), | |
1734 | "Return a string containing the name of the user logged in on\n" | |
1735 | "the controlling terminal of the process, or @code{#f} if this\n" | |
1736 | "information cannot be obtained.") | |
1737 | #define FUNC_NAME s_scm_getlogin | |
1738 | { | |
1739 | char * p; | |
1740 | ||
1741 | p = getlogin (); | |
1742 | if (!p || !*p) | |
1743 | return SCM_BOOL_F; | |
cc95e00a | 1744 | return scm_from_locale_string (p); |
94e6d793 MG |
1745 | } |
1746 | #undef FUNC_NAME | |
1747 | #endif /* HAVE_GETLOGIN */ | |
1748 | ||
94e6d793 MG |
1749 | #if HAVE_GETPRIORITY |
1750 | SCM_DEFINE (scm_getpriority, "getpriority", 2, 0, 0, | |
1751 | (SCM which, SCM who), | |
1752 | "Return the scheduling priority of the process, process group\n" | |
1753 | "or user, as indicated by @var{which} and @var{who}. @var{which}\n" | |
1754 | "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n" | |
1755 | "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n" | |
1756 | "@var{which} (a process identifier for @code{PRIO_PROCESS},\n" | |
1757 | "process group identifier for @code{PRIO_PGRP}, and a user\n" | |
1758 | "identifier for @code{PRIO_USER}. A zero value of @var{who}\n" | |
1759 | "denotes the current process, process group, or user. Return\n" | |
1760 | "the highest priority (lowest numerical value) of any of the\n" | |
1761 | "specified processes.") | |
1762 | #define FUNC_NAME s_scm_getpriority | |
1763 | { | |
1764 | int cwhich, cwho, ret; | |
1765 | ||
a55c2b68 MV |
1766 | cwhich = scm_to_int (which); |
1767 | cwho = scm_to_int (who); | |
94e6d793 MG |
1768 | |
1769 | /* We have to clear errno and examine it later, because -1 is a | |
1770 | legal return value for getpriority(). */ | |
1771 | errno = 0; | |
1772 | ret = getpriority (cwhich, cwho); | |
1773 | if (errno != 0) | |
1774 | SCM_SYSERROR; | |
a55c2b68 | 1775 | return scm_from_int (ret); |
94e6d793 MG |
1776 | } |
1777 | #undef FUNC_NAME | |
1778 | #endif /* HAVE_GETPRIORITY */ | |
1779 | ||
1780 | #if HAVE_SETPRIORITY | |
1781 | SCM_DEFINE (scm_setpriority, "setpriority", 3, 0, 0, | |
1782 | (SCM which, SCM who, SCM prio), | |
1783 | "Set the scheduling priority of the process, process group\n" | |
1784 | "or user, as indicated by @var{which} and @var{who}. @var{which}\n" | |
1785 | "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n" | |
1786 | "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n" | |
1787 | "@var{which} (a process identifier for @code{PRIO_PROCESS},\n" | |
1788 | "process group identifier for @code{PRIO_PGRP}, and a user\n" | |
1789 | "identifier for @code{PRIO_USER}. A zero value of @var{who}\n" | |
1790 | "denotes the current process, process group, or user.\n" | |
1791 | "@var{prio} is a value in the range -20 and 20, the default\n" | |
1792 | "priority is 0; lower priorities cause more favorable\n" | |
1793 | "scheduling. Sets the priority of all of the specified\n" | |
1794 | "processes. Only the super-user may lower priorities.\n" | |
1795 | "The return value is not specified.") | |
1796 | #define FUNC_NAME s_scm_setpriority | |
1797 | { | |
1798 | int cwhich, cwho, cprio; | |
1799 | ||
a55c2b68 MV |
1800 | cwhich = scm_to_int (which); |
1801 | cwho = scm_to_int (who); | |
1802 | cprio = scm_to_int (prio); | |
94e6d793 MG |
1803 | |
1804 | if (setpriority (cwhich, cwho, cprio) == -1) | |
1805 | SCM_SYSERROR; | |
1806 | return SCM_UNSPECIFIED; | |
1807 | } | |
1808 | #undef FUNC_NAME | |
1809 | #endif /* HAVE_SETPRIORITY */ | |
1810 | ||
fe613fe2 LC |
1811 | #ifdef HAVE_SCHED_GETAFFINITY |
1812 | ||
1813 | static SCM | |
1814 | cpu_set_to_bitvector (const cpu_set_t *cs) | |
1815 | { | |
1816 | SCM bv; | |
1817 | size_t cpu; | |
1818 | ||
1819 | bv = scm_c_make_bitvector (sizeof (*cs), SCM_BOOL_F); | |
1820 | ||
1821 | for (cpu = 0; cpu < sizeof (*cs); cpu++) | |
1822 | { | |
1823 | if (CPU_ISSET (cpu, cs)) | |
1824 | /* XXX: This is inefficient but avoids code duplication. */ | |
1825 | scm_c_bitvector_set_x (bv, cpu, SCM_BOOL_T); | |
1826 | } | |
1827 | ||
1828 | return bv; | |
1829 | } | |
1830 | ||
1831 | SCM_DEFINE (scm_getaffinity, "getaffinity", 1, 0, 0, | |
1832 | (SCM pid), | |
1833 | "Return a bitvector representing the CPU affinity mask for\n" | |
1834 | "process @var{pid}. Each CPU the process has affinity with\n" | |
1835 | "has its corresponding bit set in the returned bitvector.\n" | |
1836 | "The number of bits set is a good estimate of how many CPUs\n" | |
1837 | "Guile can use without stepping on other processes' toes.\n\n" | |
3ae78cac LC |
1838 | "Currently this procedure is only defined on GNU variants\n" |
1839 | "(@pxref{CPU Affinity, @code{sched_getaffinity},, libc, The\n" | |
1840 | "GNU C Library Reference Manual}).\n") | |
fe613fe2 LC |
1841 | #define FUNC_NAME s_scm_getaffinity |
1842 | { | |
1843 | int err; | |
1844 | cpu_set_t cs; | |
1845 | ||
1846 | CPU_ZERO (&cs); | |
1847 | err = sched_getaffinity (scm_to_int (pid), sizeof (cs), &cs); | |
1848 | if (err) | |
1849 | SCM_SYSERROR; | |
1850 | ||
1851 | return cpu_set_to_bitvector (&cs); | |
1852 | } | |
1853 | #undef FUNC_NAME | |
1854 | ||
1855 | #endif /* HAVE_SCHED_GETAFFINITY */ | |
1856 | ||
1857 | #ifdef HAVE_SCHED_SETAFFINITY | |
1858 | ||
1859 | SCM_DEFINE (scm_setaffinity, "setaffinity", 2, 0, 0, | |
1860 | (SCM pid, SCM mask), | |
1861 | "Install the CPU affinity mask @var{mask}, a bitvector, for\n" | |
1862 | "the process or thread with ID @var{pid}. The return value\n" | |
1863 | "is unspecified.\n\n" | |
3ae78cac LC |
1864 | "Currently this procedure is only defined on GNU variants\n" |
1865 | "(@pxref{CPU Affinity, @code{sched_setaffinity},, libc, The\n" | |
1866 | "GNU C Library Reference Manual}).\n") | |
fe613fe2 LC |
1867 | #define FUNC_NAME s_scm_setaffinity |
1868 | { | |
1869 | cpu_set_t cs; | |
1870 | scm_t_array_handle handle; | |
1871 | const scm_t_uint32 *c_mask; | |
1872 | size_t len, off, cpu; | |
1873 | ssize_t inc; | |
1874 | int err; | |
1875 | ||
1876 | c_mask = scm_bitvector_elements (mask, &handle, &off, &len, &inc); | |
1877 | ||
1878 | CPU_ZERO (&cs); | |
1879 | for (cpu = 0; cpu < len; cpu++) | |
1880 | { | |
1881 | size_t idx; | |
1882 | ||
1883 | idx = cpu * inc + off; | |
1884 | if (c_mask[idx / 32] & (1UL << (idx % 32))) | |
1885 | CPU_SET (cpu, &cs); | |
1886 | } | |
1887 | ||
1888 | err = sched_setaffinity (scm_to_int (pid), sizeof (cs), &cs); | |
1889 | if (err) | |
1890 | SCM_SYSERROR; | |
1891 | ||
1892 | return SCM_UNSPECIFIED; | |
1893 | } | |
1894 | #undef FUNC_NAME | |
1895 | ||
1896 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
1897 | ||
f0c0141f LC |
1898 | SCM_DEFINE (scm_total_processor_count, "total-processor-count", 0, 0, 0, |
1899 | (void), | |
1900 | "Return the total number of processors of the machine, which\n" | |
1901 | "is guaranteed to be at least 1. A ``processor'' here is a\n" | |
1902 | "thread execution unit, which can be either:\n\n" | |
1903 | "@itemize\n" | |
1904 | "@item an execution core in a (possibly multi-core) chip, in a\n" | |
1905 | " (possibly multi- chip) module, in a single computer, or\n" | |
1906 | "@item a thread execution unit inside a core in the case of\n" | |
1907 | " @dfn{hyper-threaded} CPUs.\n" | |
1908 | "@end itemize\n\n" | |
1909 | "Which of the two definitions is used, is unspecified.\n") | |
1910 | #define FUNC_NAME s_scm_total_processor_count | |
1911 | { | |
1912 | return scm_from_ulong (num_processors (NPROC_ALL)); | |
1913 | } | |
1914 | #undef FUNC_NAME | |
1915 | ||
1916 | SCM_DEFINE (scm_current_processor_count, "current-processor-count", 0, 0, 0, | |
1917 | (void), | |
1918 | "Like @code{total-processor-count}, but return the number of\n" | |
1919 | "processors available to the current process. See\n" | |
1920 | "@code{setaffinity} and @code{getaffinity} for more\n" | |
1921 | "information.\n") | |
b56c252b | 1922 | #define FUNC_NAME s_scm_current_processor_count |
f0c0141f LC |
1923 | { |
1924 | return scm_from_ulong (num_processors (NPROC_CURRENT)); | |
1925 | } | |
1926 | #undef FUNC_NAME | |
1927 | ||
1928 | \f | |
94e6d793 MG |
1929 | #if HAVE_GETPASS |
1930 | SCM_DEFINE (scm_getpass, "getpass", 1, 0, 0, | |
1931 | (SCM prompt), | |
1932 | "Display @var{prompt} to the standard error output and read\n" | |
1933 | "a password from @file{/dev/tty}. If this file is not\n" | |
1934 | "accessible, it reads from standard input. The password may be\n" | |
1935 | "up to 127 characters in length. Additional characters and the\n" | |
1936 | "terminating newline character are discarded. While reading\n" | |
1937 | "the password, echoing and the generation of signals by special\n" | |
1938 | "characters is disabled.") | |
1939 | #define FUNC_NAME s_scm_getpass | |
1940 | { | |
1941 | char * p; | |
1942 | SCM passwd; | |
1943 | ||
1944 | SCM_VALIDATE_STRING (1, prompt); | |
94e6d793 | 1945 | |
f015614a MV |
1946 | WITH_STRING (prompt, c_prompt, |
1947 | p = getpass(c_prompt)); | |
1948 | passwd = scm_from_locale_string (p); | |
94e6d793 MG |
1949 | |
1950 | /* Clear out the password in the static buffer. */ | |
1951 | memset (p, 0, strlen (p)); | |
1952 | ||
1953 | return passwd; | |
1954 | } | |
1955 | #undef FUNC_NAME | |
1956 | #endif /* HAVE_GETPASS */ | |
1957 | ||
94e6d793 MG |
1958 | SCM_DEFINE (scm_flock, "flock", 2, 0, 0, |
1959 | (SCM file, SCM operation), | |
1960 | "Apply or remove an advisory lock on an open file.\n" | |
1961 | "@var{operation} specifies the action to be done:\n" | |
0b91f1db KR |
1962 | "\n" |
1963 | "@defvar LOCK_SH\n" | |
94e6d793 MG |
1964 | "Shared lock. More than one process may hold a shared lock\n" |
1965 | "for a given file at a given time.\n" | |
0b91f1db KR |
1966 | "@end defvar\n" |
1967 | "@defvar LOCK_EX\n" | |
94e6d793 MG |
1968 | "Exclusive lock. Only one process may hold an exclusive lock\n" |
1969 | "for a given file at a given time.\n" | |
0b91f1db KR |
1970 | "@end defvar\n" |
1971 | "@defvar LOCK_UN\n" | |
94e6d793 | 1972 | "Unlock the file.\n" |
0b91f1db KR |
1973 | "@end defvar\n" |
1974 | "@defvar LOCK_NB\n" | |
1975 | "Don't block when locking. This is combined with one of the\n" | |
1976 | "other operations using @code{logior}. If @code{flock} would\n" | |
1977 | "block an @code{EWOULDBLOCK} error is thrown.\n" | |
1978 | "@end defvar\n" | |
1979 | "\n" | |
94e6d793 | 1980 | "The return value is not specified. @var{file} may be an open\n" |
0b91f1db KR |
1981 | "file descriptor or an open file descriptor port.\n" |
1982 | "\n" | |
1983 | "Note that @code{flock} does not lock files across NFS.") | |
94e6d793 MG |
1984 | #define FUNC_NAME s_scm_flock |
1985 | { | |
a55c2b68 | 1986 | int fdes; |
94e6d793 | 1987 | |
a55c2b68 MV |
1988 | if (scm_is_integer (file)) |
1989 | fdes = scm_to_int (file); | |
94e6d793 MG |
1990 | else |
1991 | { | |
1992 | SCM_VALIDATE_OPFPORT (2, file); | |
1993 | ||
1994 | fdes = SCM_FPORT_FDES (file); | |
1995 | } | |
a55c2b68 | 1996 | if (flock (fdes, scm_to_int (operation)) == -1) |
94e6d793 MG |
1997 | SCM_SYSERROR; |
1998 | return SCM_UNSPECIFIED; | |
1999 | } | |
2000 | #undef FUNC_NAME | |
94e6d793 MG |
2001 | |
2002 | #if HAVE_SETHOSTNAME | |
2003 | SCM_DEFINE (scm_sethostname, "sethostname", 1, 0, 0, | |
2004 | (SCM name), | |
2005 | "Set the host name of the current processor to @var{name}. May\n" | |
2006 | "only be used by the superuser. The return value is not\n" | |
2007 | "specified.") | |
2008 | #define FUNC_NAME s_scm_sethostname | |
2009 | { | |
f015614a | 2010 | int rv; |
94e6d793 | 2011 | |
f015614a MV |
2012 | WITH_STRING (name, c_name, |
2013 | rv = sethostname (c_name, strlen(c_name))); | |
2014 | if (rv == -1) | |
94e6d793 MG |
2015 | SCM_SYSERROR; |
2016 | return SCM_UNSPECIFIED; | |
2017 | } | |
2018 | #undef FUNC_NAME | |
2019 | #endif /* HAVE_SETHOSTNAME */ | |
2020 | ||
b28f5b3c | 2021 | |
94e6d793 MG |
2022 | #if HAVE_GETHOSTNAME |
2023 | SCM_DEFINE (scm_gethostname, "gethostname", 0, 0, 0, | |
2024 | (void), | |
2025 | "Return the host name of the current processor.") | |
2026 | #define FUNC_NAME s_scm_gethostname | |
2027 | { | |
b28f5b3c DH |
2028 | #ifdef MAXHOSTNAMELEN |
2029 | ||
2030 | /* Various systems define MAXHOSTNAMELEN (including Solaris in fact). | |
2031 | * On GNU/Linux this doesn't include the terminating '\0', hence "+ 1". */ | |
2032 | const int len = MAXHOSTNAMELEN + 1; | |
2033 | char *const p = scm_malloc (len); | |
2034 | const int res = gethostname (p, len); | |
2035 | ||
661ae7ab MV |
2036 | scm_dynwind_begin (0); |
2037 | scm_dynwind_unwind_handler (free, p, 0); | |
b28f5b3c DH |
2038 | |
2039 | #else | |
94e6d793 | 2040 | |
0eaf1055 | 2041 | /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not |
b28f5b3c DH |
2042 | * large enough. SUSv2 specifies 255 maximum too, apparently. */ |
2043 | int len = 256; | |
2044 | int res; | |
2045 | char *p; | |
0eaf1055 | 2046 | |
b28f5b3c | 2047 | # if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX) |
0eaf1055 KR |
2048 | |
2049 | /* POSIX specifies the HOST_NAME_MAX system parameter for the max size, | |
b28f5b3c DH |
2050 | * which may reflect a particular kernel configuration. |
2051 | * Must watch out for this existing but giving -1, as happens for instance | |
2052 | * in gnu/linux glibc 2.3.2. */ | |
0eaf1055 | 2053 | { |
b28f5b3c | 2054 | const long int n = sysconf (_SC_HOST_NAME_MAX); |
0eaf1055 KR |
2055 | if (n != -1L) |
2056 | len = n; | |
2057 | } | |
b28f5b3c DH |
2058 | |
2059 | # endif | |
2060 | ||
2061 | p = scm_malloc (len); | |
2062 | ||
661ae7ab MV |
2063 | scm_dynwind_begin (0); |
2064 | scm_dynwind_unwind_handler (free, p, 0); | |
0eaf1055 | 2065 | |
94e6d793 MG |
2066 | res = gethostname (p, len); |
2067 | while (res == -1 && errno == ENAMETOOLONG) | |
2068 | { | |
94e6d793 | 2069 | len *= 2; |
b28f5b3c DH |
2070 | |
2071 | /* scm_realloc may throw an exception. */ | |
2072 | p = scm_realloc (p, len); | |
94e6d793 MG |
2073 | res = gethostname (p, len); |
2074 | } | |
b28f5b3c DH |
2075 | |
2076 | #endif | |
2077 | ||
94e6d793 MG |
2078 | if (res == -1) |
2079 | { | |
b28f5b3c DH |
2080 | const int save_errno = errno; |
2081 | ||
2b829bbb | 2082 | /* No guile exceptions can occur before we have freed p's memory. */ |
661ae7ab | 2083 | scm_dynwind_end (); |
4c9419ac | 2084 | free (p); |
b28f5b3c | 2085 | |
22865124 | 2086 | errno = save_errno; |
94e6d793 MG |
2087 | SCM_SYSERROR; |
2088 | } | |
b28f5b3c DH |
2089 | else |
2090 | { | |
cc95e00a MV |
2091 | /* scm_from_locale_string may throw an exception. */ |
2092 | const SCM name = scm_from_locale_string (p); | |
b28f5b3c | 2093 | |
2b829bbb | 2094 | /* No guile exceptions can occur before we have freed p's memory. */ |
661ae7ab | 2095 | scm_dynwind_end (); |
b28f5b3c DH |
2096 | free (p); |
2097 | ||
2098 | return name; | |
2099 | } | |
94e6d793 MG |
2100 | } |
2101 | #undef FUNC_NAME | |
2102 | #endif /* HAVE_GETHOSTNAME */ | |
2103 | ||
fe613fe2 | 2104 | \f |
b89c4943 | 2105 | void |
0f2d19dd | 2106 | scm_init_posix () |
0f2d19dd JB |
2107 | { |
2108 | scm_add_feature ("posix"); | |
2109 | #ifdef HAVE_GETEUID | |
2110 | scm_add_feature ("EIDs"); | |
2111 | #endif | |
2112 | #ifdef WAIT_ANY | |
e11e83f3 | 2113 | scm_c_define ("WAIT_ANY", scm_from_int (WAIT_ANY)); |
0f2d19dd JB |
2114 | #endif |
2115 | #ifdef WAIT_MYPGRP | |
e11e83f3 | 2116 | scm_c_define ("WAIT_MYPGRP", scm_from_int (WAIT_MYPGRP)); |
0f2d19dd JB |
2117 | #endif |
2118 | #ifdef WNOHANG | |
e11e83f3 | 2119 | scm_c_define ("WNOHANG", scm_from_int (WNOHANG)); |
0f2d19dd JB |
2120 | #endif |
2121 | #ifdef WUNTRACED | |
e11e83f3 | 2122 | scm_c_define ("WUNTRACED", scm_from_int (WUNTRACED)); |
0f2d19dd JB |
2123 | #endif |
2124 | ||
0f2d19dd | 2125 | #ifdef LC_COLLATE |
e11e83f3 | 2126 | scm_c_define ("LC_COLLATE", scm_from_int (LC_COLLATE)); |
0f2d19dd JB |
2127 | #endif |
2128 | #ifdef LC_CTYPE | |
e11e83f3 | 2129 | scm_c_define ("LC_CTYPE", scm_from_int (LC_CTYPE)); |
0f2d19dd JB |
2130 | #endif |
2131 | #ifdef LC_MONETARY | |
e11e83f3 | 2132 | scm_c_define ("LC_MONETARY", scm_from_int (LC_MONETARY)); |
0f2d19dd JB |
2133 | #endif |
2134 | #ifdef LC_NUMERIC | |
e11e83f3 | 2135 | scm_c_define ("LC_NUMERIC", scm_from_int (LC_NUMERIC)); |
0f2d19dd JB |
2136 | #endif |
2137 | #ifdef LC_TIME | |
e11e83f3 | 2138 | scm_c_define ("LC_TIME", scm_from_int (LC_TIME)); |
0f2d19dd JB |
2139 | #endif |
2140 | #ifdef LC_MESSAGES | |
e11e83f3 | 2141 | scm_c_define ("LC_MESSAGES", scm_from_int (LC_MESSAGES)); |
0f2d19dd JB |
2142 | #endif |
2143 | #ifdef LC_ALL | |
e11e83f3 | 2144 | scm_c_define ("LC_ALL", scm_from_int (LC_ALL)); |
0f2d19dd | 2145 | #endif |
9361f762 MV |
2146 | #ifdef LC_PAPER |
2147 | scm_c_define ("LC_PAPER", scm_from_int (LC_PAPER)); | |
2148 | #endif | |
2149 | #ifdef LC_NAME | |
2150 | scm_c_define ("LC_NAME", scm_from_int (LC_NAME)); | |
2151 | #endif | |
2152 | #ifdef LC_ADDRESS | |
2153 | scm_c_define ("LC_ADDRESS", scm_from_int (LC_ADDRESS)); | |
2154 | #endif | |
2155 | #ifdef LC_TELEPHONE | |
2156 | scm_c_define ("LC_TELEPHONE", scm_from_int (LC_TELEPHONE)); | |
2157 | #endif | |
2158 | #ifdef LC_MEASUREMENT | |
2159 | scm_c_define ("LC_MEASUREMENT", scm_from_int (LC_MEASUREMENT)); | |
2160 | #endif | |
2161 | #ifdef LC_IDENTIFICATION | |
2162 | scm_c_define ("LC_IDENTIFICATION", scm_from_int (LC_IDENTIFICATION)); | |
2163 | #endif | |
bd9e24b3 | 2164 | #ifdef PIPE_BUF |
b9bd8526 | 2165 | scm_c_define ("PIPE_BUF", scm_from_long (PIPE_BUF)); |
bd9e24b3 GH |
2166 | #endif |
2167 | ||
94e6d793 | 2168 | #ifdef PRIO_PROCESS |
e11e83f3 | 2169 | scm_c_define ("PRIO_PROCESS", scm_from_int (PRIO_PROCESS)); |
94e6d793 MG |
2170 | #endif |
2171 | #ifdef PRIO_PGRP | |
e11e83f3 | 2172 | scm_c_define ("PRIO_PGRP", scm_from_int (PRIO_PGRP)); |
94e6d793 MG |
2173 | #endif |
2174 | #ifdef PRIO_USER | |
e11e83f3 | 2175 | scm_c_define ("PRIO_USER", scm_from_int (PRIO_USER)); |
94e6d793 MG |
2176 | #endif |
2177 | ||
2178 | #ifdef LOCK_SH | |
e11e83f3 | 2179 | scm_c_define ("LOCK_SH", scm_from_int (LOCK_SH)); |
94e6d793 MG |
2180 | #endif |
2181 | #ifdef LOCK_EX | |
e11e83f3 | 2182 | scm_c_define ("LOCK_EX", scm_from_int (LOCK_EX)); |
94e6d793 MG |
2183 | #endif |
2184 | #ifdef LOCK_UN | |
e11e83f3 | 2185 | scm_c_define ("LOCK_UN", scm_from_int (LOCK_UN)); |
94e6d793 MG |
2186 | #endif |
2187 | #ifdef LOCK_NB | |
e11e83f3 | 2188 | scm_c_define ("LOCK_NB", scm_from_int (LOCK_NB)); |
94e6d793 MG |
2189 | #endif |
2190 | ||
648da032 | 2191 | #include "libguile/cpp-SIG.c" |
a0599745 | 2192 | #include "libguile/posix.x" |
0f2d19dd | 2193 | } |
89e00824 ML |
2194 | |
2195 | /* | |
2196 | Local Variables: | |
2197 | c-file-style: "gnu" | |
2198 | End: | |
2199 | */ |