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