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