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