1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 * 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
3 * 2014 Free Software Foundation, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 3 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
39 #ifdef TIME_WITH_SYS_TIME
40 # include <sys/time.h>
44 # include <sys/time.h>
52 #ifdef LIBC_H_WITH_UNISTD_H
56 #include <sys/types.h>
67 #include "libguile/_scm.h"
68 #include "libguile/dynwind.h"
69 #include "libguile/fports.h"
70 #include "libguile/scmsigs.h"
71 #include "libguile/feature.h"
72 #include "libguile/strings.h"
73 #include "libguile/srfi-13.h"
74 #include "libguile/srfi-14.h"
75 #include "libguile/vectors.h"
76 #include "libguile/values.h"
78 #include "libguile/validate.h"
79 #include "libguile/posix.h"
80 #include "libguile/gettext.h"
81 #include "libguile/threads.h"
85 # include <sys/wait.h>
88 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
91 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
99 #ifdef HAVE_SYS_UTSNAME_H
100 #include <sys/utsname.h>
103 #ifdef HAVE_SETLOCALE
107 #if (defined HAVE_NEWLOCALE) && (defined HAVE_STRCOLL_L)
108 # define USE_GNU_LOCALE_API
111 #if (defined USE_GNU_LOCALE_API) && (defined HAVE_XLOCALE_H)
112 # include <xlocale.h>
120 #include <netdb.h> /* for MAXHOSTNAMELEN on Solaris */
123 #ifdef HAVE_SYS_PARAM_H
124 #include <sys/param.h> /* for MAXHOSTNAMELEN */
127 #if HAVE_SYS_RESOURCE_H
128 # include <sys/resource.h>
131 #include <sys/file.h> /* from Gnulib */
133 /* Some Unix systems don't define these. CPP hair is dangerous, but
134 this seems safe enough... */
151 /* No prototype for this on Solaris 10. The man page says it's in
152 <unistd.h> ... but it lies. */
153 #if ! HAVE_DECL_SETHOSTNAME
154 int sethostname (char *name
, size_t namelen
);
157 #if defined HAVE_GETLOGIN && !HAVE_DECL_GETLOGIN
158 /* MinGW doesn't supply this decl; see
159 http://lists.gnu.org/archive/html/bug-gnulib/2013-03/msg00030.html for more
161 char *getlogin (void);
164 /* On NextStep, <utime.h> doesn't define struct utime, unless we
165 #define _POSIX_SOURCE before #including it. I think this is less
166 of a kludge than defining struct utimbuf ourselves. */
167 #ifdef UTIMBUF_NEEDS_POSIX
168 #define _POSIX_SOURCE
171 #ifdef HAVE_SYS_UTIME_H
172 #include <sys/utime.h>
179 /* Please don't add any more #includes or #defines here. The hack
180 above means that _POSIX_SOURCE may be #defined, which will
181 encourage header files to do strange things.
183 FIXME: Maybe should undef _POSIX_SOURCE after it's done its job.
185 FIXME: Probably should do all the includes first, then all the fallback
186 declarations and defines, in case things are not in the header we
194 /* Two often used patterns
197 #define WITH_STRING(str,cstr,code) \
199 char *cstr = scm_to_locale_string (str); \
204 #define STRING_SYSCALL(str,cstr,code) \
207 char *cstr = scm_to_locale_string (str); \
208 SCM_SYSCALL (code); \
209 eno = errno; free (cstr); errno = eno; \
214 SCM_SYMBOL (sym_read_pipe
, "read pipe");
215 SCM_SYMBOL (sym_write_pipe
, "write pipe");
217 SCM_DEFINE (scm_pipe
, "pipe", 0, 0, 0,
219 "Return a newly created pipe: a pair of ports which are linked\n"
220 "together on the local machine. The @emph{car} is the input\n"
221 "port and the @emph{cdr} is the output port. Data written (and\n"
222 "flushed) to the output port can be read from the input port.\n"
223 "Pipes are commonly used for communication with a newly forked\n"
224 "child process. The need to flush the output port can be\n"
225 "avoided by making it unbuffered using @code{setvbuf}.\n"
227 "Writes occur atomically provided the size of the data in bytes\n"
228 "is not greater than the value of @code{PIPE_BUF}. Note that\n"
229 "the output port is likely to block if too much data (typically\n"
230 "equal to @code{PIPE_BUF}) has been written but not yet read\n"
231 "from the input port.")
232 #define FUNC_NAME s_scm_pipe
241 p_rd
= scm_fdes_to_port (fd
[0], "r", sym_read_pipe
);
242 p_wt
= scm_fdes_to_port (fd
[1], "w", sym_write_pipe
);
243 return scm_cons (p_rd
, p_wt
);
248 #ifdef HAVE_GETGROUPS
249 SCM_DEFINE (scm_getgroups
, "getgroups", 0, 0, 0,
251 "Return a vector of integers representing the current\n"
252 "supplementary group IDs.")
253 #define FUNC_NAME s_scm_getgroups
260 ngroups
= getgroups (0, NULL
);
263 else if (ngroups
== 0)
264 return scm_c_make_vector (0, SCM_BOOL_F
);
266 size
= ngroups
* sizeof (GETGROUPS_T
);
267 groups
= scm_malloc (size
);
268 ngroups
= getgroups (ngroups
, groups
);
270 result
= scm_c_make_vector (ngroups
, SCM_BOOL_F
);
271 while (--ngroups
>= 0)
272 SCM_SIMPLE_VECTOR_SET (result
, ngroups
, scm_from_ulong (groups
[ngroups
]));
280 #ifdef HAVE_SETGROUPS
281 SCM_DEFINE (scm_setgroups
, "setgroups", 1, 0, 0,
283 "Set the current set of supplementary group IDs to the integers\n"
284 "in the given vector @var{group_vec}. The return value is\n"
287 "Generally only the superuser can set the process group IDs.")
288 #define FUNC_NAME s_scm_setgroups
297 SCM_VALIDATE_VECTOR (SCM_ARG1
, group_vec
);
299 ngroups
= SCM_SIMPLE_VECTOR_LENGTH (group_vec
);
301 /* validate before allocating, so we don't have to worry about leaks */
302 for (i
= 0; i
< ngroups
; i
++)
304 unsigned long ulong_gid
;
306 SCM_VALIDATE_ULONG_COPY (1, SCM_SIMPLE_VECTOR_REF (group_vec
, i
),
309 if (gid
!= ulong_gid
)
310 SCM_OUT_OF_RANGE (1, SCM_SIMPLE_VECTOR_REF (group_vec
, i
));
313 size
= ngroups
* sizeof (GETGROUPS_T
);
314 if (size
/ sizeof (GETGROUPS_T
) != ngroups
)
315 SCM_OUT_OF_RANGE (SCM_ARG1
, scm_from_int (ngroups
));
316 groups
= scm_malloc (size
);
317 for(i
= 0; i
< ngroups
; i
++)
318 groups
[i
] = SCM_NUM2ULONG (1, SCM_SIMPLE_VECTOR_REF (group_vec
, i
));
320 result
= setgroups (ngroups
, groups
);
321 save_errno
= errno
; /* don't let free() touch errno */
326 return SCM_UNSPECIFIED
;
332 SCM_DEFINE (scm_getpwuid
, "getpw", 0, 1, 0,
334 "Look up an entry in the user database. @var{user} can be an\n"
335 "integer, a string, or omitted, giving the behaviour of\n"
336 "@code{getpwuid}, @code{getpwnam} or @code{getpwent}\n"
338 #define FUNC_NAME s_scm_getpwuid
340 struct passwd
*entry
;
342 SCM result
= scm_c_make_vector (7, SCM_UNSPECIFIED
);
343 if (SCM_UNBNDP (user
) || scm_is_false (user
))
345 SCM_SYSCALL (entry
= getpwent ());
351 else if (scm_is_integer (user
))
353 entry
= getpwuid (scm_to_int (user
));
357 WITH_STRING (user
, c_user
,
358 entry
= getpwnam (c_user
));
361 SCM_MISC_ERROR ("entry not found", SCM_EOL
);
363 SCM_SIMPLE_VECTOR_SET(result
, 0, scm_from_locale_string (entry
->pw_name
));
364 SCM_SIMPLE_VECTOR_SET(result
, 1, scm_from_locale_string (entry
->pw_passwd
));
365 SCM_SIMPLE_VECTOR_SET(result
, 2, scm_from_ulong (entry
->pw_uid
));
366 SCM_SIMPLE_VECTOR_SET(result
, 3, scm_from_ulong (entry
->pw_gid
));
367 SCM_SIMPLE_VECTOR_SET(result
, 4, scm_from_locale_string (entry
->pw_gecos
));
369 SCM_SIMPLE_VECTOR_SET(result
, 5, scm_from_locale_string (""));
371 SCM_SIMPLE_VECTOR_SET(result
, 5, scm_from_locale_string (entry
->pw_dir
));
372 if (!entry
->pw_shell
)
373 SCM_SIMPLE_VECTOR_SET(result
, 6, scm_from_locale_string (""));
375 SCM_SIMPLE_VECTOR_SET(result
, 6, scm_from_locale_string (entry
->pw_shell
));
379 #endif /* HAVE_GETPWENT */
383 SCM_DEFINE (scm_setpwent
, "setpw", 0, 1, 0,
385 "If called with a true argument, initialize or reset the password data\n"
386 "stream. Otherwise, close the stream. The @code{setpwent} and\n"
387 "@code{endpwent} procedures are implemented on top of this.")
388 #define FUNC_NAME s_scm_setpwent
390 if (SCM_UNBNDP (arg
) || scm_is_false (arg
))
394 return SCM_UNSPECIFIED
;
401 /* Combines getgrgid and getgrnam. */
402 SCM_DEFINE (scm_getgrgid
, "getgr", 0, 1, 0,
404 "Look up an entry in the group database. @var{name} can be an\n"
405 "integer, a string, or omitted, giving the behaviour of\n"
406 "@code{getgrgid}, @code{getgrnam} or @code{getgrent}\n"
408 #define FUNC_NAME s_scm_getgrgid
411 SCM result
= scm_c_make_vector (4, SCM_UNSPECIFIED
);
413 if (SCM_UNBNDP (name
) || scm_is_false (name
))
415 SCM_SYSCALL (entry
= getgrent ());
421 else if (scm_is_integer (name
))
422 SCM_SYSCALL (entry
= getgrgid (scm_to_int (name
)));
424 STRING_SYSCALL (name
, c_name
,
425 entry
= getgrnam (c_name
));
429 SCM_SIMPLE_VECTOR_SET(result
, 0, scm_from_locale_string (entry
->gr_name
));
430 SCM_SIMPLE_VECTOR_SET(result
, 1, scm_from_locale_string (entry
->gr_passwd
));
431 SCM_SIMPLE_VECTOR_SET(result
, 2, scm_from_ulong (entry
->gr_gid
));
432 SCM_SIMPLE_VECTOR_SET(result
, 3, scm_makfromstrs (-1, entry
->gr_mem
));
439 SCM_DEFINE (scm_setgrent
, "setgr", 0, 1, 0,
441 "If called with a true argument, initialize or reset the group data\n"
442 "stream. Otherwise, close the stream. The @code{setgrent} and\n"
443 "@code{endgrent} procedures are implemented on top of this.")
444 #define FUNC_NAME s_scm_setgrent
446 if (SCM_UNBNDP (arg
) || scm_is_false (arg
))
450 return SCM_UNSPECIFIED
;
453 #endif /* HAVE_GETGRENT */
456 #ifdef HAVE_GETRLIMIT
458 SCM_SYMBOL (sym_as
, "as");
461 SCM_SYMBOL (sym_core
, "core");
464 SCM_SYMBOL (sym_cpu
, "cpu");
467 SCM_SYMBOL (sym_data
, "data");
470 SCM_SYMBOL (sym_fsize
, "fsize");
472 #ifdef RLIMIT_MEMLOCK
473 SCM_SYMBOL (sym_memlock
, "memlock");
475 #ifdef RLIMIT_MSGQUEUE
476 SCM_SYMBOL (sym_msgqueue
, "msgqueue");
479 SCM_SYMBOL (sym_nice
, "nice");
482 SCM_SYMBOL (sym_nofile
, "nofile");
485 SCM_SYMBOL (sym_nproc
, "nproc");
488 SCM_SYMBOL (sym_rss
, "rss");
491 SCM_SYMBOL (sym_rtprio
, "rtprio");
494 SCM_SYMBOL (sym_rttime
, "rttime");
496 #ifdef RLIMIT_SIGPENDING
497 SCM_SYMBOL (sym_sigpending
, "sigpending");
500 SCM_SYMBOL (sym_stack
, "stack");
504 scm_to_resource (SCM s
, const char *func
, int pos
)
506 if (scm_is_number (s
))
507 return scm_to_int (s
);
509 SCM_ASSERT_TYPE (scm_is_symbol (s
), s
, pos
, func
, "symbol");
512 if (scm_is_eq (s
, sym_as
))
516 if (scm_is_eq (s
, sym_core
))
520 if (scm_is_eq (s
, sym_cpu
))
524 if (scm_is_eq (s
, sym_data
))
528 if (scm_is_eq (s
, sym_fsize
))
531 #ifdef RLIMIT_MEMLOCK
532 if (scm_is_eq (s
, sym_memlock
))
533 return RLIMIT_MEMLOCK
;
535 #ifdef RLIMIT_MSGQUEUE
536 if (scm_is_eq (s
, sym_msgqueue
))
537 return RLIMIT_MSGQUEUE
;
540 if (scm_is_eq (s
, sym_nice
))
544 if (scm_is_eq (s
, sym_nofile
))
545 return RLIMIT_NOFILE
;
548 if (scm_is_eq (s
, sym_nproc
))
552 if (scm_is_eq (s
, sym_rss
))
556 if (scm_is_eq (s
, sym_rtprio
))
557 return RLIMIT_RTPRIO
;
560 if (scm_is_eq (s
, sym_rttime
))
561 return RLIMIT_RTPRIO
;
563 #ifdef RLIMIT_SIGPENDING
564 if (scm_is_eq (s
, sym_sigpending
))
565 return RLIMIT_SIGPENDING
;
568 if (scm_is_eq (s
, sym_stack
))
572 scm_misc_error (func
, "invalid rlimit resource ~A", scm_list_1 (s
));
576 SCM_DEFINE (scm_getrlimit
, "getrlimit", 1, 0, 0,
578 "Get a resource limit for this process. @var{resource} identifies the resource,\n"
579 "either as an integer or as a symbol. For example, @code{(getrlimit 'stack)}\n"
580 "gets the limits associated with @code{RLIMIT_STACK}.\n\n"
581 "@code{getrlimit} returns two values, the soft and the hard limit. If no\n"
582 "limit is set for the resource in question, the returned limit will be @code{#f}.")
583 #define FUNC_NAME s_scm_getrlimit
586 struct rlimit lim
= { 0, 0 };
588 iresource
= scm_to_resource (resource
, FUNC_NAME
, 1);
590 if (getrlimit (iresource
, &lim
) != 0)
591 scm_syserror (FUNC_NAME
);
593 return scm_values (scm_list_2 ((lim
.rlim_cur
== RLIM_INFINITY
) ? SCM_BOOL_F
594 : scm_from_long (lim
.rlim_cur
),
595 (lim
.rlim_max
== RLIM_INFINITY
) ? SCM_BOOL_F
596 : scm_from_long (lim
.rlim_max
)));
601 #ifdef HAVE_SETRLIMIT
602 SCM_DEFINE (scm_setrlimit
, "setrlimit", 3, 0, 0,
603 (SCM resource
, SCM soft
, SCM hard
),
604 "Set a resource limit for this process. @var{resource} identifies the resource,\n"
605 "either as an integer or as a symbol. @var{soft} and @var{hard} should be integers,\n"
606 "or @code{#f} to indicate no limit (i.e., @code{RLIM_INFINITY}).\n\n"
607 "For example, @code{(setrlimit 'stack 150000 300000)} sets the @code{RLIMIT_STACK}\n"
608 "limit to 150 kilobytes, with a hard limit of 300 kB.")
609 #define FUNC_NAME s_scm_setrlimit
612 struct rlimit lim
= { 0, 0 };
614 iresource
= scm_to_resource (resource
, FUNC_NAME
, 1);
616 lim
.rlim_cur
= scm_is_false (soft
) ? RLIM_INFINITY
: scm_to_long (soft
);
617 lim
.rlim_max
= scm_is_false (hard
) ? RLIM_INFINITY
: scm_to_long (hard
);
619 if (setrlimit (iresource
, &lim
) != 0)
620 scm_syserror (FUNC_NAME
);
622 return SCM_UNSPECIFIED
;
625 #endif /* HAVE_SETRLIMIT */
626 #endif /* HAVE_GETRLIMIT */
629 SCM_DEFINE (scm_kill
, "kill", 2, 0, 0,
631 "Sends a signal to the specified process or group of processes.\n\n"
632 "@var{pid} specifies the processes to which the signal is sent:\n\n"
634 "@item @var{pid} greater than 0\n"
635 "The process whose identifier is @var{pid}.\n"
636 "@item @var{pid} equal to 0\n"
637 "All processes in the current process group.\n"
638 "@item @var{pid} less than -1\n"
639 "The process group whose identifier is -@var{pid}\n"
640 "@item @var{pid} equal to -1\n"
641 "If the process is privileged, all processes except for some special\n"
642 "system processes. Otherwise, all processes with the current effective\n"
645 "@var{sig} should be specified using a variable corresponding to\n"
646 "the Unix symbolic name, e.g.,\n\n"
651 "Interrupt signal.\n"
653 #define FUNC_NAME s_scm_kill
655 /* Signal values are interned in scm_init_posix(). */
657 if (kill (scm_to_int (pid
), scm_to_int (sig
)) != 0)
660 /* Mingw has raise(), but not kill(). (Other raw DOS environments might
661 be similar.) Use raise() when the requested pid is our own process,
663 if (scm_to_int (pid
) == getpid ())
665 if (raise (scm_to_int (sig
)) != 0)
677 return SCM_UNSPECIFIED
;
682 SCM_DEFINE (scm_waitpid
, "waitpid", 1, 1, 0,
683 (SCM pid
, SCM options
),
684 "This procedure collects status information from a child process which\n"
685 "has terminated or (optionally) stopped. Normally it will\n"
686 "suspend the calling process until this can be done. If more than one\n"
687 "child process is eligible then one will be chosen by the operating system.\n\n"
688 "The value of @var{pid} determines the behaviour:\n\n"
690 "@item @var{pid} greater than 0\n"
691 "Request status information from the specified child process.\n"
692 "@item @var{pid} equal to -1 or WAIT_ANY\n"
693 "Request status information for any child process.\n"
694 "@item @var{pid} equal to 0 or WAIT_MYPGRP\n"
695 "Request status information for any child process in the current process\n"
697 "@item @var{pid} less than -1\n"
698 "Request status information for any child process whose process group ID\n"
701 "The @var{options} argument, if supplied, should be the bitwise OR of the\n"
702 "values of zero or more of the following variables:\n\n"
704 "Return immediately even if there are no child processes to be collected.\n"
706 "@defvar WUNTRACED\n"
707 "Report status information for stopped processes as well as terminated\n"
710 "The return value is a pair containing:\n\n"
713 "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
714 "specified and no process was collected.\n"
716 "The integer status value.\n"
718 #define FUNC_NAME s_scm_waitpid
723 if (SCM_UNBNDP (options
))
727 /* Flags are interned in scm_init_posix. */
728 ioptions
= scm_to_int (options
);
730 SCM_SYSCALL (i
= waitpid (scm_to_int (pid
), &status
, ioptions
));
733 return scm_cons (scm_from_int (i
), scm_from_int (status
));
736 #endif /* HAVE_WAITPID */
739 SCM_DEFINE (scm_status_exit_val
, "status:exit-val", 1, 0, 0,
741 "Return the exit status value, as would be set if a process\n"
742 "ended normally through a call to @code{exit} or @code{_exit},\n"
743 "if any, otherwise @code{#f}.")
744 #define FUNC_NAME s_scm_status_exit_val
748 /* On Ultrix, the WIF... macros assume their argument is an lvalue;
750 lstatus
= scm_to_int (status
);
751 if (WIFEXITED (lstatus
))
752 return (scm_from_int (WEXITSTATUS (lstatus
)));
758 SCM_DEFINE (scm_status_term_sig
, "status:term-sig", 1, 0, 0,
760 "Return the signal number which terminated the process, if any,\n"
761 "otherwise @code{#f}.")
762 #define FUNC_NAME s_scm_status_term_sig
766 lstatus
= scm_to_int (status
);
767 if (WIFSIGNALED (lstatus
))
768 return scm_from_int (WTERMSIG (lstatus
));
774 SCM_DEFINE (scm_status_stop_sig
, "status:stop-sig", 1, 0, 0,
776 "Return the signal number which stopped the process, if any,\n"
777 "otherwise @code{#f}.")
778 #define FUNC_NAME s_scm_status_stop_sig
782 lstatus
= scm_to_int (status
);
783 if (WIFSTOPPED (lstatus
))
784 return scm_from_int (WSTOPSIG (lstatus
));
789 #endif /* __MINGW32__ */
792 SCM_DEFINE (scm_getppid
, "getppid", 0, 0, 0,
794 "Return an integer representing the process ID of the parent\n"
796 #define FUNC_NAME s_scm_getppid
798 return scm_from_int (getppid ());
801 #endif /* HAVE_GETPPID */
805 SCM_DEFINE (scm_getuid
, "getuid", 0, 0, 0,
807 "Return an integer representing the current real user ID.")
808 #define FUNC_NAME s_scm_getuid
810 return scm_from_int (getuid ());
816 SCM_DEFINE (scm_getgid
, "getgid", 0, 0, 0,
818 "Return an integer representing the current real group ID.")
819 #define FUNC_NAME s_scm_getgid
821 return scm_from_int (getgid ());
827 SCM_DEFINE (scm_geteuid
, "geteuid", 0, 0, 0,
829 "Return an integer representing the current effective user ID.\n"
830 "If the system does not support effective IDs, then the real ID\n"
831 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
832 "system supports effective IDs.")
833 #define FUNC_NAME s_scm_geteuid
836 return scm_from_int (geteuid ());
838 return scm_from_int (getuid ());
844 SCM_DEFINE (scm_getegid
, "getegid", 0, 0, 0,
846 "Return an integer representing the current effective group ID.\n"
847 "If the system does not support effective IDs, then the real ID\n"
848 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
849 "system supports effective IDs.")
850 #define FUNC_NAME s_scm_getegid
853 return scm_from_int (getegid ());
855 return scm_from_int (getgid ());
861 SCM_DEFINE (scm_setuid
, "setuid", 1, 0, 0,
863 "Sets both the real and effective user IDs to the integer @var{id}, provided\n"
864 "the process has appropriate privileges.\n"
865 "The return value is unspecified.")
866 #define FUNC_NAME s_scm_setuid
868 if (setuid (scm_to_int (id
)) != 0)
870 return SCM_UNSPECIFIED
;
874 SCM_DEFINE (scm_setgid
, "setgid", 1, 0, 0,
876 "Sets both the real and effective group IDs to the integer @var{id}, provided\n"
877 "the process has appropriate privileges.\n"
878 "The return value is unspecified.")
879 #define FUNC_NAME s_scm_setgid
881 if (setgid (scm_to_int (id
)) != 0)
883 return SCM_UNSPECIFIED
;
887 SCM_DEFINE (scm_seteuid
, "seteuid", 1, 0, 0,
889 "Sets the effective user ID to the integer @var{id}, provided the process\n"
890 "has appropriate privileges. If effective IDs are not supported, the\n"
891 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
892 "system supports effective IDs.\n"
893 "The return value is unspecified.")
894 #define FUNC_NAME s_scm_seteuid
899 rv
= seteuid (scm_to_int (id
));
901 rv
= setuid (scm_to_int (id
));
905 return SCM_UNSPECIFIED
;
908 #endif /* __MINGW32__ */
912 SCM_DEFINE (scm_setegid
, "setegid", 1, 0, 0,
914 "Sets the effective group ID to the integer @var{id}, provided the process\n"
915 "has appropriate privileges. If effective IDs are not supported, the\n"
916 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
917 "system supports effective IDs.\n"
918 "The return value is unspecified.")
919 #define FUNC_NAME s_scm_setegid
924 rv
= setegid (scm_to_int (id
));
926 rv
= setgid (scm_to_int (id
));
930 return SCM_UNSPECIFIED
;
938 SCM_DEFINE (scm_getpgrp
, "getpgrp", 0, 0, 0,
940 "Return an integer representing the current process group ID.\n"
941 "This is the POSIX definition, not BSD.")
942 #define FUNC_NAME s_scm_getpgrp
945 fn
= (int (*) ()) getpgrp
;
946 return scm_from_int (fn (0));
949 #endif /* HAVE_GETPGRP */
953 SCM_DEFINE (scm_setpgid
, "setpgid", 2, 0, 0,
955 "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n"
956 "@var{pgid} must be integers: they can be zero to indicate the ID of the\n"
958 "Fails on systems that do not support job control.\n"
959 "The return value is unspecified.")
960 #define FUNC_NAME s_scm_setpgid
962 /* FIXME(?): may be known as setpgrp. */
963 if (setpgid (scm_to_int (pid
), scm_to_int (pgid
)) != 0)
965 return SCM_UNSPECIFIED
;
968 #endif /* HAVE_SETPGID */
971 SCM_DEFINE (scm_setsid
, "setsid", 0, 0, 0,
973 "Creates a new session. The current process becomes the session leader\n"
974 "and is put in a new process group. The process will be detached\n"
975 "from its controlling terminal if it has one.\n"
976 "The return value is an integer representing the new process group ID.")
977 #define FUNC_NAME s_scm_setsid
979 pid_t sid
= setsid ();
982 return SCM_UNSPECIFIED
;
985 #endif /* HAVE_SETSID */
988 SCM_DEFINE (scm_getsid
, "getsid", 1, 0, 0,
990 "Returns the session ID of process @var{pid}. (The session\n"
991 "ID of a process is the process group ID of its session leader.)")
992 #define FUNC_NAME s_scm_getsid
994 return scm_from_int (getsid (scm_to_int (pid
)));
997 #endif /* HAVE_GETSID */
1000 /* ttyname returns its result in a single static buffer, hence
1001 scm_i_misc_mutex for thread safety. In glibc 2.3.2 two threads
1002 continuously calling ttyname will otherwise get an overwrite quite
1005 ttyname_r (when available) could be used instead of scm_i_misc_mutex, but
1006 there's probably little to be gained in either speed or parallelism. */
1009 SCM_DEFINE (scm_ttyname
, "ttyname", 1, 0, 0,
1011 "Return a string with the name of the serial terminal device\n"
1012 "underlying @var{port}.")
1013 #define FUNC_NAME s_scm_ttyname
1017 SCM ret
= SCM_BOOL_F
;
1019 port
= SCM_COERCE_OUTPORT (port
);
1020 SCM_VALIDATE_OPPORT (1, port
);
1021 if (!SCM_FPORTP (port
))
1023 fd
= SCM_FPORT_FDES (port
);
1025 scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex
);
1027 SCM_SYSCALL (result
= ttyname (fd
));
1030 result
= strdup (result
);
1032 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex
);
1040 ret
= scm_take_locale_string (result
);
1045 #endif /* HAVE_TTYNAME */
1048 /* For thread safety "buf" is used instead of NULL for the ctermid static
1049 buffer. Actually it's unlikely the controlling terminal will change
1050 during program execution, and indeed on glibc (2.3.2) it's always just
1051 "/dev/tty", but L_ctermid on the stack is easy and fast and guarantees
1052 safety everywhere. */
1054 SCM_DEFINE (scm_ctermid
, "ctermid", 0, 0, 0,
1056 "Return a string containing the file name of the controlling\n"
1057 "terminal for the current process.")
1058 #define FUNC_NAME s_scm_ctermid
1060 char buf
[L_ctermid
];
1061 char *result
= ctermid (buf
);
1062 if (*result
== '\0')
1064 return scm_from_locale_string (result
);
1067 #endif /* HAVE_CTERMID */
1069 #ifdef HAVE_TCGETPGRP
1070 SCM_DEFINE (scm_tcgetpgrp
, "tcgetpgrp", 1, 0, 0,
1072 "Return the process group ID of the foreground process group\n"
1073 "associated with the terminal open on the file descriptor\n"
1074 "underlying @var{port}.\n"
1076 "If there is no foreground process group, the return value is a\n"
1077 "number greater than 1 that does not match the process group ID\n"
1078 "of any existing process group. This can happen if all of the\n"
1079 "processes in the job that was formerly the foreground job have\n"
1080 "terminated, and no other job has yet been moved into the\n"
1082 #define FUNC_NAME s_scm_tcgetpgrp
1087 port
= SCM_COERCE_OUTPORT (port
);
1089 SCM_VALIDATE_OPFPORT (1, port
);
1090 fd
= SCM_FPORT_FDES (port
);
1091 if ((pgid
= tcgetpgrp (fd
)) == -1)
1093 return scm_from_int (pgid
);
1096 #endif /* HAVE_TCGETPGRP */
1098 #ifdef HAVE_TCSETPGRP
1099 SCM_DEFINE (scm_tcsetpgrp
, "tcsetpgrp", 2, 0, 0,
1100 (SCM port
, SCM pgid
),
1101 "Set the foreground process group ID for the terminal used by the file\n"
1102 "descriptor underlying @var{port} to the integer @var{pgid}.\n"
1103 "The calling process\n"
1104 "must be a member of the same session as @var{pgid} and must have the same\n"
1105 "controlling terminal. The return value is unspecified.")
1106 #define FUNC_NAME s_scm_tcsetpgrp
1110 port
= SCM_COERCE_OUTPORT (port
);
1112 SCM_VALIDATE_OPFPORT (1, port
);
1113 fd
= SCM_FPORT_FDES (port
);
1114 if (tcsetpgrp (fd
, scm_to_int (pgid
)) == -1)
1116 return SCM_UNSPECIFIED
;
1119 #endif /* HAVE_TCSETPGRP */
1121 SCM_DEFINE (scm_execl
, "execl", 1, 0, 1,
1122 (SCM filename
, SCM args
),
1123 "Executes the file named by @var{filename} as a new process image.\n"
1124 "The remaining arguments are supplied to the process; from a C program\n"
1125 "they are accessible as the @code{argv} argument to @code{main}.\n"
1126 "Conventionally the first @var{arg} is the same as @var{filename}.\n"
1127 "All arguments must be strings.\n\n"
1128 "If @var{arg} is missing, @var{path} is executed with a null\n"
1129 "argument list, which may have system-dependent side-effects.\n\n"
1130 "This procedure is currently implemented using the @code{execv} system\n"
1131 "call, but we call it @code{execl} because of its Scheme calling interface.")
1132 #define FUNC_NAME s_scm_execl
1137 scm_dynwind_begin (0);
1139 exec_file
= scm_to_locale_string (filename
);
1140 scm_dynwind_free (exec_file
);
1142 exec_argv
= scm_i_allocate_string_pointers (args
);
1144 execv (exec_file
, exec_argv
);
1153 SCM_DEFINE (scm_execlp
, "execlp", 1, 0, 1,
1154 (SCM filename
, SCM args
),
1155 "Similar to @code{execl}, however if\n"
1156 "@var{filename} does not contain a slash\n"
1157 "then the file to execute will be located by searching the\n"
1158 "directories listed in the @code{PATH} environment variable.\n\n"
1159 "This procedure is currently implemented using the @code{execvp} system\n"
1160 "call, but we call it @code{execlp} because of its Scheme calling interface.")
1161 #define FUNC_NAME s_scm_execlp
1166 scm_dynwind_begin (0);
1168 exec_file
= scm_to_locale_string (filename
);
1169 scm_dynwind_free (exec_file
);
1171 exec_argv
= scm_i_allocate_string_pointers (args
);
1173 execvp (exec_file
, exec_argv
);
1183 /* OPTIMIZE-ME: scm_execle doesn't need malloced copies of the environment
1184 list strings the way environ_list_to_c gives. */
1186 SCM_DEFINE (scm_execle
, "execle", 2, 0, 1,
1187 (SCM filename
, SCM env
, SCM args
),
1188 "Similar to @code{execl}, but the environment of the new process is\n"
1189 "specified by @var{env}, which must be a list of strings as returned by the\n"
1190 "@code{environ} procedure.\n\n"
1191 "This procedure is currently implemented using the @code{execve} system\n"
1192 "call, but we call it @code{execle} because of its Scheme calling interface.")
1193 #define FUNC_NAME s_scm_execle
1199 scm_dynwind_begin (0);
1201 exec_file
= scm_to_locale_string (filename
);
1202 scm_dynwind_free (exec_file
);
1204 exec_argv
= scm_i_allocate_string_pointers (args
);
1205 exec_env
= scm_i_allocate_string_pointers (env
);
1207 execve (exec_file
, exec_argv
, exec_env
);
1217 SCM_DEFINE (scm_fork
, "primitive-fork", 0, 0, 0,
1219 "Creates a new \"child\" process by duplicating the current \"parent\" process.\n"
1220 "In the child the return value is 0. In the parent the return value is\n"
1221 "the integer process ID of the child.\n\n"
1222 "This procedure has been renamed from @code{fork} to avoid a naming conflict\n"
1223 "with the scsh fork.")
1224 #define FUNC_NAME s_scm_fork
1227 if (scm_ilength (scm_all_threads ()) != 1)
1228 /* Other threads may be holding on to resources that Guile needs --
1229 it is not safe to permit one thread to fork while others are
1232 In addition, POSIX clearly specifies that if a multi-threaded
1233 program forks, the child must only call functions that are
1234 async-signal-safe. We can't guarantee that in general. The best
1235 we can do is to allow forking only very early, before any call to
1236 sigaction spawns the signal-handling thread. */
1238 (scm_from_latin1_string
1239 ("warning: call to primitive-fork while multiple threads are running;\n"
1240 " further behavior unspecified. See \"Processes\" in the\n"
1241 " manual, for more information.\n"),
1242 scm_current_warning_port ());
1246 return scm_from_int (pid
);
1250 /* Since Guile uses threads, we have to be very careful to avoid calling
1251 functions that are not async-signal-safe in the child. That's why
1252 this function is implemented in C. */
1254 scm_open_process (SCM mode
, SCM prog
, SCM args
)
1255 #define FUNC_NAME "open-process"
1258 int reading
, writing
;
1259 int c2p
[2]; /* Child to parent. */
1260 int p2c
[2]; /* Parent to child. */
1261 int in
= -1, out
= -1, err
= -1;
1267 exec_file
= scm_to_locale_string (prog
);
1268 exec_argv
= scm_i_allocate_string_pointers (scm_cons (prog
, args
));
1270 mode_bits
= scm_i_mode_bits (mode
);
1271 reading
= mode_bits
& SCM_RDNG
;
1272 writing
= mode_bits
& SCM_WRTNG
;
1278 int errno_save
= errno
;
1290 int errno_save
= errno
;
1306 if (SCM_OPOUTFPORTP ((port
= scm_current_error_port ())))
1307 err
= SCM_FPORT_FDES (port
);
1308 if (out
== -1 && SCM_OPOUTFPORTP ((port
= scm_current_output_port ())))
1309 out
= SCM_FPORT_FDES (port
);
1310 if (in
== -1 && SCM_OPINFPORTP ((port
= scm_current_input_port ())))
1311 in
= SCM_FPORT_FDES (port
);
1314 #if defined (HAVE_GETRLIMIT) && defined (RLIMIT_NOFILE)
1316 struct rlimit lim
= { 0, 0 };
1317 if (getrlimit (RLIMIT_NOFILE
, &lim
) == 0)
1318 max_fd
= lim
.rlim_cur
;
1326 int errno_save
= errno
;
1345 SCM read_port
= SCM_BOOL_F
, write_port
= SCM_BOOL_F
;
1347 /* There is no sense in catching errors on close(). */
1351 read_port
= scm_fdes_to_port (c2p
[0], "r", sym_read_pipe
);
1352 scm_setvbuf (read_port
, scm_from_int (_IONBF
), SCM_UNDEFINED
);
1357 write_port
= scm_fdes_to_port (p2c
[1], "w", sym_write_pipe
);
1358 scm_setvbuf (write_port
, scm_from_int (_IONBF
), SCM_UNDEFINED
);
1362 (scm_list_3 (read_port
, write_port
, scm_from_int (pid
)));
1371 /* Close all file descriptors in ports inherited from the parent
1372 except for in, out, and err. Heavy-handed, but robust. */
1374 if (max_fd
!= in
&& max_fd
!= out
&& max_fd
!= err
)
1377 /* Ignore errors on these open() calls. */
1379 in
= open ("/dev/null", O_RDONLY
);
1381 out
= open ("/dev/null", O_WRONLY
);
1383 err
= open ("/dev/null", O_WRONLY
);
1388 do out
= dup (out
); while (errno
== EINTR
);
1390 do err
= dup (err
); while (errno
== EINTR
);
1391 do dup2 (in
, 0); while (errno
== EINTR
);
1397 do err
= dup (err
); while (errno
== EINTR
);
1398 do dup2 (out
, 1); while (errno
== EINTR
);
1403 do dup2 (err
, 2); while (errno
== EINTR
);
1407 execvp (exec_file
, exec_argv
);
1409 /* The exec failed! There is nothing sensible to do. */
1412 char *msg
= strerror (errno
);
1413 fprintf (fdopen (err
, "a"), "In execlp of %s: %s\n",
1417 _exit (EXIT_FAILURE
);
1422 #endif /* HAVE_FORK */
1425 # include "win32-uname.h"
1428 #if defined (HAVE_UNAME) || defined (__MINGW32__)
1429 SCM_DEFINE (scm_uname
, "uname", 0, 0, 0,
1431 "Return an object with some information about the computer\n"
1432 "system the program is running on.")
1433 #define FUNC_NAME s_scm_uname
1436 SCM result
= scm_c_make_vector (5, SCM_UNSPECIFIED
);
1437 if (uname (&buf
) < 0)
1439 SCM_SIMPLE_VECTOR_SET(result
, 0, scm_from_locale_string (buf
.sysname
));
1440 SCM_SIMPLE_VECTOR_SET(result
, 1, scm_from_locale_string (buf
.nodename
));
1441 SCM_SIMPLE_VECTOR_SET(result
, 2, scm_from_locale_string (buf
.release
));
1442 SCM_SIMPLE_VECTOR_SET(result
, 3, scm_from_locale_string (buf
.version
));
1443 SCM_SIMPLE_VECTOR_SET(result
, 4, scm_from_locale_string (buf
.machine
));
1446 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (buf.domainname));
1451 #endif /* HAVE_UNAME */
1453 SCM_DEFINE (scm_environ
, "environ", 0, 1, 0,
1455 "If @var{env} is omitted, return the current environment (in the\n"
1456 "Unix sense) as a list of strings. Otherwise set the current\n"
1457 "environment, which is also the default environment for child\n"
1458 "processes, to the supplied list of strings. Each member of\n"
1459 "@var{env} should be of the form @code{NAME=VALUE} and values of\n"
1460 "@code{NAME} should not be duplicated. If @var{env} is supplied\n"
1461 "then the return value is unspecified.")
1462 #define FUNC_NAME s_scm_environ
1464 if (SCM_UNBNDP (env
))
1465 return scm_makfromstrs (-1, environ
);
1468 environ
= scm_i_allocate_string_pointers (env
);
1469 return SCM_UNSPECIFIED
;
1476 SCM_DEFINE (scm_tmpnam
, "tmpnam", 0, 0, 0,
1478 "Return a name in the file system that does not match any\n"
1479 "existing file. However there is no guarantee that another\n"
1480 "process will not create the file after @code{tmpnam} is called.\n"
1481 "Care should be taken if opening the file, e.g., use the\n"
1482 "@code{O_EXCL} open flag or use @code{mkstemp!} instead.")
1483 #define FUNC_NAME s_scm_tmpnam
1485 char name
[L_tmpnam
];
1488 SCM_SYSCALL (rv
= tmpnam (name
));
1490 /* not SCM_SYSERROR since errno probably not set. */
1491 SCM_MISC_ERROR ("tmpnam failed", SCM_EOL
);
1492 return scm_from_locale_string (name
);
1498 SCM_DEFINE (scm_tmpfile
, "tmpfile", 0, 0, 0,
1500 "Return an input/output port to a unique temporary file\n"
1501 "named using the path prefix @code{P_tmpdir} defined in\n"
1503 "The file is automatically deleted when the port is closed\n"
1504 "or the program terminates.")
1505 #define FUNC_NAME s_scm_tmpfile
1510 if (! (rv
= tmpfile ()))
1514 fd
= dup (fileno (rv
));
1518 /* FIXME: leaking the file, it will never be closed! */
1521 return scm_fdes_to_port (fd
, "w+", SCM_BOOL_F
);
1525 SCM_DEFINE (scm_utime
, "utime", 1, 5, 0,
1526 (SCM pathname
, SCM actime
, SCM modtime
, SCM actimens
, SCM modtimens
,
1528 "@code{utime} sets the access and modification times for the\n"
1529 "file named by @var{pathname}. If @var{actime} or @var{modtime} is\n"
1530 "not supplied, then the current time is used. @var{actime} and\n"
1531 "@var{modtime} must be integer time values as returned by the\n"
1532 "@code{current-time} procedure.\n\n"
1533 "The optional @var{actimens} and @var{modtimens} are nanoseconds\n"
1534 "to add @var{actime} and @var{modtime}. Nanosecond precision is\n"
1535 "only supported on some combinations of file systems and operating\n"
1538 "(utime \"foo\" (- (current-time) 3600))\n"
1540 "will set the access time to one hour in the past and the\n"
1541 "modification time to the current time.")
1542 #define FUNC_NAME s_scm_utime
1545 time_t atim_sec
, mtim_sec
;
1546 long atim_nsec
, mtim_nsec
;
1549 if (SCM_UNBNDP (actime
))
1551 #ifdef HAVE_UTIMENSAT
1553 atim_nsec
= UTIME_NOW
;
1555 SCM_SYSCALL (time (&atim_sec
));
1561 atim_sec
= SCM_NUM2ULONG (2, actime
);
1562 if (SCM_UNBNDP (actimens
))
1565 atim_nsec
= SCM_NUM2LONG (4, actimens
);
1568 if (SCM_UNBNDP (modtime
))
1570 #ifdef HAVE_UTIMENSAT
1572 mtim_nsec
= UTIME_NOW
;
1574 SCM_SYSCALL (time (&mtim_sec
));
1580 mtim_sec
= SCM_NUM2ULONG (3, modtime
);
1581 if (SCM_UNBNDP (modtimens
))
1584 mtim_nsec
= SCM_NUM2LONG (5, modtimens
);
1587 if (SCM_UNBNDP (flags
))
1590 f
= SCM_NUM2INT (6, flags
);
1592 #ifdef HAVE_UTIMENSAT
1594 struct timespec times
[2];
1595 times
[0].tv_sec
= atim_sec
;
1596 times
[0].tv_nsec
= atim_nsec
;
1597 times
[1].tv_sec
= mtim_sec
;
1598 times
[1].tv_nsec
= mtim_nsec
;
1600 STRING_SYSCALL (pathname
, c_pathname
,
1601 rv
= utimensat (AT_FDCWD
, c_pathname
, times
, f
));
1606 utm
.actime
= atim_sec
;
1607 utm
.modtime
= mtim_sec
;
1608 /* Silence warnings. */
1613 scm_out_of_range(FUNC_NAME
, flags
);
1615 STRING_SYSCALL (pathname
, c_pathname
,
1616 rv
= utime (c_pathname
, &utm
));
1622 return SCM_UNSPECIFIED
;
1626 SCM_DEFINE (scm_getpid
, "getpid", 0, 0, 0,
1628 "Return an integer representing the current process ID.")
1629 #define FUNC_NAME s_scm_getpid
1631 return scm_from_ulong (getpid ());
1635 SCM_DEFINE (scm_putenv
, "putenv", 1, 0, 0,
1637 "Modifies the environment of the current process, which is also\n"
1638 "the default environment inherited by child processes. If\n"
1639 "@var{str} is of the form @code{NAME=VALUE} then it will be\n"
1640 "written directly into the environment, replacing any existing\n"
1641 "environment string with name matching @code{NAME}. If\n"
1642 "@var{str} does not contain an equal sign, then any existing\n"
1643 "string with name matching @var{str} will be removed.\n"
1645 "The return value is unspecified.")
1646 #define FUNC_NAME s_scm_putenv
1649 char *c_str
= scm_to_locale_string (str
);
1651 /* Leave C_STR in the environment. */
1653 /* Gnulib's `putenv' module honors the semantics described above. */
1654 rv
= putenv (c_str
);
1658 return SCM_UNSPECIFIED
;
1662 /* This mutex is used to serialize invocations of `setlocale ()' on non-GNU
1663 systems (i.e., systems where a reentrant locale API is not available). It
1664 is also acquired before calls to `nl_langinfo ()'. See `i18n.c' for
1666 scm_i_pthread_mutex_t scm_i_locale_mutex
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
1668 #ifdef HAVE_SETLOCALE
1670 SCM_DEFINE (scm_setlocale
, "setlocale", 1, 1, 0,
1671 (SCM category
, SCM locale
),
1672 "If @var{locale} is omitted, return the current value of the\n"
1673 "specified locale category as a system-dependent string.\n"
1674 "@var{category} should be specified using the values\n"
1675 "@code{LC_COLLATE}, @code{LC_ALL} etc.\n"
1677 "Otherwise the specified locale category is set to the string\n"
1678 "@var{locale} and the new value is returned as a\n"
1679 "system-dependent string. If @var{locale} is an empty string,\n"
1680 "the locale will be set using environment variables.\n"
1682 "When the locale is changed, the character encoding of the new\n"
1683 "locale (UTF-8, ISO-8859-1, etc.) is used for the current\n"
1684 "input, output, and error ports\n")
1685 #define FUNC_NAME s_scm_setlocale
1692 scm_dynwind_begin (0);
1694 if (SCM_UNBNDP (locale
))
1700 clocale
= scm_to_locale_string (locale
);
1701 scm_dynwind_free (clocale
);
1704 c_category
= scm_i_to_lc_category (category
, 1);
1706 scm_i_pthread_mutex_lock (&scm_i_locale_mutex
);
1707 rv
= setlocale (c_category
, clocale
);
1708 scm_i_pthread_mutex_unlock (&scm_i_locale_mutex
);
1712 /* POSIX and C99 don't say anything about setlocale setting errno, so
1713 force a sensible value here. glibc leaves ENOENT, which would be
1714 fine, but it's not a documented feature. */
1719 enc
= locale_charset ();
1721 /* Set the default encoding for new ports. */
1722 scm_i_set_default_port_encoding (enc
);
1724 /* Set the encoding for the stdio ports. */
1725 scm_i_set_port_encoding_x (scm_current_input_port (), enc
);
1726 scm_i_set_port_encoding_x (scm_current_output_port (), enc
);
1727 scm_i_set_port_encoding_x (scm_current_error_port (), enc
);
1730 return scm_from_locale_string (rv
);
1733 #endif /* HAVE_SETLOCALE */
1736 SCM_DEFINE (scm_mknod
, "mknod", 4, 0, 0,
1737 (SCM path
, SCM type
, SCM perms
, SCM dev
),
1738 "Creates a new special file, such as a file corresponding to a device.\n"
1739 "@var{path} specifies the name of the file. @var{type} should\n"
1740 "be one of the following symbols:\n"
1741 "regular, directory, symlink, block-special, char-special,\n"
1742 "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
1743 "@var{dev} (an integer) specifies which device the special file refers\n"
1744 "to. Its exact interpretation depends on the kind of special file\n"
1745 "being created.\n\n"
1748 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
1750 "The return value is unspecified.")
1751 #define FUNC_NAME s_scm_mknod
1757 SCM_VALIDATE_STRING (1, path
);
1758 SCM_VALIDATE_SYMBOL (2, type
);
1760 p
= scm_i_symbol_chars (type
);
1761 if (strcmp (p
, "regular") == 0)
1763 else if (strcmp (p
, "directory") == 0)
1766 /* systems without symlinks probably don't have S_IFLNK defined */
1767 else if (strcmp (p
, "symlink") == 0)
1770 else if (strcmp (p
, "block-special") == 0)
1772 else if (strcmp (p
, "char-special") == 0)
1774 else if (strcmp (p
, "fifo") == 0)
1777 else if (strcmp (p
, "socket") == 0)
1781 SCM_OUT_OF_RANGE (2, type
);
1783 STRING_SYSCALL (path
, c_path
,
1784 val
= mknod (c_path
,
1785 ctype
| scm_to_int (perms
),
1789 return SCM_UNSPECIFIED
;
1792 #endif /* HAVE_MKNOD */
1795 SCM_DEFINE (scm_nice
, "nice", 1, 0, 0,
1797 "Increment the priority of the current process by @var{incr}. A higher\n"
1798 "priority value means that the process runs less often.\n"
1799 "The return value is unspecified.")
1800 #define FUNC_NAME s_scm_nice
1802 /* nice() returns "prio-NZERO" on success or -1 on error, but -1 can arise
1803 from "prio-NZERO", so an error must be detected from errno changed */
1805 nice (scm_to_int (incr
));
1809 return SCM_UNSPECIFIED
;
1812 #endif /* HAVE_NICE */
1815 SCM_DEFINE (scm_sync
, "sync", 0, 0, 0,
1817 "Flush the operating system disk buffers.\n"
1818 "The return value is unspecified.")
1819 #define FUNC_NAME s_scm_sync
1822 return SCM_UNSPECIFIED
;
1825 #endif /* HAVE_SYNC */
1828 /* crypt() returns a pointer to a static buffer, so we use scm_i_misc_mutex
1829 to avoid another thread overwriting it. A test program running crypt
1830 continuously in two threads can be quickly seen tripping this problem.
1831 crypt() is pretty slow normally, so a mutex shouldn't add much overhead.
1833 glibc has a thread-safe crypt_r, but (in version 2.3.2) it runs a lot
1834 slower (about 5x) than plain crypt if you pass an uninitialized data
1835 block each time. Presumably there's some one-time setups. The best way
1836 to use crypt_r for parallel execution in multiple threads would probably
1837 be to maintain a little pool of initialized crypt_data structures, take
1838 one and use it, then return it to the pool. That pool could be garbage
1839 collected so it didn't add permanently to memory use if only a few crypt
1840 calls are made. But we expect crypt will be used rarely, and even more
1841 rarely will there be any desire for lots of parallel execution on
1842 multiple cpus. So for now we don't bother with anything fancy, just
1846 SCM_DEFINE (scm_crypt
, "crypt", 2, 0, 0,
1847 (SCM key
, SCM salt
),
1848 "Encrypt @var{key} using @var{salt} as the salt value to the\n"
1849 "crypt(3) library call.")
1850 #define FUNC_NAME s_scm_crypt
1853 char *c_key
, *c_salt
, *c_ret
;
1855 scm_dynwind_begin (0);
1856 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex
);
1858 c_key
= scm_to_locale_string (key
);
1859 scm_dynwind_free (c_key
);
1860 c_salt
= scm_to_locale_string (salt
);
1861 scm_dynwind_free (c_salt
);
1863 /* The Linux crypt(3) man page says crypt will return NULL and set errno
1864 on error. (Eg. ENOSYS if legal restrictions mean it cannot be
1866 c_ret
= crypt (c_key
, c_salt
);
1870 ret
= scm_from_locale_string (c_ret
);
1875 #endif /* HAVE_CRYPT */
1878 SCM_DEFINE (scm_chroot
, "chroot", 1, 0, 0,
1880 "Change the root directory to that specified in @var{path}.\n"
1881 "This directory will be used for path names beginning with\n"
1882 "@file{/}. The root directory is inherited by all children\n"
1883 "of the current process. Only the superuser may change the\n"
1885 #define FUNC_NAME s_scm_chroot
1889 WITH_STRING (path
, c_path
,
1890 rv
= chroot (c_path
));
1893 return SCM_UNSPECIFIED
;
1896 #endif /* HAVE_CHROOT */
1898 SCM_DEFINE (scm_getlogin
, "getlogin", 0, 0, 0,
1900 "Return a string containing the name of the user logged in on\n"
1901 "the controlling terminal of the process, or @code{#f} if this\n"
1902 "information cannot be obtained.")
1903 #define FUNC_NAME s_scm_getlogin
1910 return scm_from_locale_string (p
);
1914 #if HAVE_GETPRIORITY
1915 SCM_DEFINE (scm_getpriority
, "getpriority", 2, 0, 0,
1916 (SCM which
, SCM who
),
1917 "Return the scheduling priority of the process, process group\n"
1918 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1919 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1920 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1921 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1922 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1923 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1924 "denotes the current process, process group, or user. Return\n"
1925 "the highest priority (lowest numerical value) of any of the\n"
1926 "specified processes.")
1927 #define FUNC_NAME s_scm_getpriority
1929 int cwhich
, cwho
, ret
;
1931 cwhich
= scm_to_int (which
);
1932 cwho
= scm_to_int (who
);
1934 /* We have to clear errno and examine it later, because -1 is a
1935 legal return value for getpriority(). */
1937 ret
= getpriority (cwhich
, cwho
);
1940 return scm_from_int (ret
);
1943 #endif /* HAVE_GETPRIORITY */
1945 #if HAVE_SETPRIORITY
1946 SCM_DEFINE (scm_setpriority
, "setpriority", 3, 0, 0,
1947 (SCM which
, SCM who
, SCM prio
),
1948 "Set the scheduling priority of the process, process group\n"
1949 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1950 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1951 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1952 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1953 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1954 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1955 "denotes the current process, process group, or user.\n"
1956 "@var{prio} is a value in the range -20 and 20, the default\n"
1957 "priority is 0; lower priorities cause more favorable\n"
1958 "scheduling. Sets the priority of all of the specified\n"
1959 "processes. Only the super-user may lower priorities.\n"
1960 "The return value is not specified.")
1961 #define FUNC_NAME s_scm_setpriority
1963 int cwhich
, cwho
, cprio
;
1965 cwhich
= scm_to_int (which
);
1966 cwho
= scm_to_int (who
);
1967 cprio
= scm_to_int (prio
);
1969 if (setpriority (cwhich
, cwho
, cprio
) == -1)
1971 return SCM_UNSPECIFIED
;
1974 #endif /* HAVE_SETPRIORITY */
1976 #ifdef HAVE_SCHED_GETAFFINITY
1979 cpu_set_to_bitvector (const cpu_set_t
*cs
)
1984 bv
= scm_c_make_bitvector (sizeof (*cs
), SCM_BOOL_F
);
1986 for (cpu
= 0; cpu
< sizeof (*cs
); cpu
++)
1988 if (CPU_ISSET (cpu
, cs
))
1989 /* XXX: This is inefficient but avoids code duplication. */
1990 scm_c_bitvector_set_x (bv
, cpu
, SCM_BOOL_T
);
1996 SCM_DEFINE (scm_getaffinity
, "getaffinity", 1, 0, 0,
1998 "Return a bitvector representing the CPU affinity mask for\n"
1999 "process @var{pid}. Each CPU the process has affinity with\n"
2000 "has its corresponding bit set in the returned bitvector.\n"
2001 "The number of bits set is a good estimate of how many CPUs\n"
2002 "Guile can use without stepping on other processes' toes.\n\n"
2003 "Currently this procedure is only defined on GNU variants\n"
2004 "(@pxref{CPU Affinity, @code{sched_getaffinity},, libc, The\n"
2005 "GNU C Library Reference Manual}).\n")
2006 #define FUNC_NAME s_scm_getaffinity
2012 err
= sched_getaffinity (scm_to_int (pid
), sizeof (cs
), &cs
);
2016 return cpu_set_to_bitvector (&cs
);
2020 #endif /* HAVE_SCHED_GETAFFINITY */
2022 #ifdef HAVE_SCHED_SETAFFINITY
2024 SCM_DEFINE (scm_setaffinity
, "setaffinity", 2, 0, 0,
2025 (SCM pid
, SCM mask
),
2026 "Install the CPU affinity mask @var{mask}, a bitvector, for\n"
2027 "the process or thread with ID @var{pid}. The return value\n"
2028 "is unspecified.\n\n"
2029 "Currently this procedure is only defined on GNU variants\n"
2030 "(@pxref{CPU Affinity, @code{sched_setaffinity},, libc, The\n"
2031 "GNU C Library Reference Manual}).\n")
2032 #define FUNC_NAME s_scm_setaffinity
2035 scm_t_array_handle handle
;
2036 const scm_t_uint32
*c_mask
;
2037 size_t len
, off
, cpu
;
2041 c_mask
= scm_bitvector_elements (mask
, &handle
, &off
, &len
, &inc
);
2044 for (cpu
= 0; cpu
< len
; cpu
++)
2048 idx
= cpu
* inc
+ off
;
2049 if (c_mask
[idx
/ 32] & (1UL << (idx
% 32)))
2053 err
= sched_setaffinity (scm_to_int (pid
), sizeof (cs
), &cs
);
2057 return SCM_UNSPECIFIED
;
2061 #endif /* HAVE_SCHED_SETAFFINITY */
2065 SCM_DEFINE (scm_getpass
, "getpass", 1, 0, 0,
2067 "Display @var{prompt} to the standard error output and read\n"
2068 "a password from @file{/dev/tty}. If this file is not\n"
2069 "accessible, it reads from standard input. The password may be\n"
2070 "up to 127 characters in length. Additional characters and the\n"
2071 "terminating newline character are discarded. While reading\n"
2072 "the password, echoing and the generation of signals by special\n"
2073 "characters is disabled.")
2074 #define FUNC_NAME s_scm_getpass
2079 SCM_VALIDATE_STRING (1, prompt
);
2081 WITH_STRING (prompt
, c_prompt
,
2082 p
= getpass(c_prompt
));
2083 passwd
= scm_from_locale_string (p
);
2085 /* Clear out the password in the static buffer. */
2086 memset (p
, 0, strlen (p
));
2091 #endif /* HAVE_GETPASS */
2093 SCM_DEFINE (scm_flock
, "flock", 2, 0, 0,
2094 (SCM file
, SCM operation
),
2095 "Apply or remove an advisory lock on an open file.\n"
2096 "@var{operation} specifies the action to be done:\n"
2099 "Shared lock. More than one process may hold a shared lock\n"
2100 "for a given file at a given time.\n"
2103 "Exclusive lock. Only one process may hold an exclusive lock\n"
2104 "for a given file at a given time.\n"
2107 "Unlock the file.\n"
2110 "Don't block when locking. This is combined with one of the\n"
2111 "other operations using @code{logior}. If @code{flock} would\n"
2112 "block an @code{EWOULDBLOCK} error is thrown.\n"
2115 "The return value is not specified. @var{file} may be an open\n"
2116 "file descriptor or an open file descriptor port.\n"
2118 "Note that @code{flock} does not lock files across NFS.")
2119 #define FUNC_NAME s_scm_flock
2123 if (scm_is_integer (file
))
2124 fdes
= scm_to_int (file
);
2127 SCM_VALIDATE_OPFPORT (2, file
);
2129 fdes
= SCM_FPORT_FDES (file
);
2131 if (flock (fdes
, scm_to_int (operation
)) == -1)
2133 return SCM_UNSPECIFIED
;
2137 #if HAVE_SETHOSTNAME
2138 SCM_DEFINE (scm_sethostname
, "sethostname", 1, 0, 0,
2140 "Set the host name of the current processor to @var{name}. May\n"
2141 "only be used by the superuser. The return value is not\n"
2143 #define FUNC_NAME s_scm_sethostname
2147 WITH_STRING (name
, c_name
,
2148 rv
= sethostname (c_name
, strlen(c_name
)));
2151 return SCM_UNSPECIFIED
;
2154 #endif /* HAVE_SETHOSTNAME */
2157 #if HAVE_GETHOSTNAME
2158 SCM_DEFINE (scm_gethostname
, "gethostname", 0, 0, 0,
2160 "Return the host name of the current processor.")
2161 #define FUNC_NAME s_scm_gethostname
2163 #ifdef MAXHOSTNAMELEN
2165 /* Various systems define MAXHOSTNAMELEN (including Solaris in fact).
2166 * On GNU/Linux this doesn't include the terminating '\0', hence "+ 1". */
2167 const int len
= MAXHOSTNAMELEN
+ 1;
2168 char *const p
= scm_malloc (len
);
2169 const int res
= gethostname (p
, len
);
2171 scm_dynwind_begin (0);
2172 scm_dynwind_unwind_handler (free
, p
, 0);
2176 /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
2177 * large enough. SUSv2 specifies 255 maximum too, apparently. */
2182 # if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX)
2184 /* POSIX specifies the HOST_NAME_MAX system parameter for the max size,
2185 * which may reflect a particular kernel configuration.
2186 * Must watch out for this existing but giving -1, as happens for instance
2187 * in gnu/linux glibc 2.3.2. */
2189 const long int n
= sysconf (_SC_HOST_NAME_MAX
);
2196 p
= scm_malloc (len
);
2198 scm_dynwind_begin (0);
2199 scm_dynwind_unwind_handler (free
, p
, 0);
2201 res
= gethostname (p
, len
);
2202 while (res
== -1 && errno
== ENAMETOOLONG
)
2206 /* scm_realloc may throw an exception. */
2207 p
= scm_realloc (p
, len
);
2208 res
= gethostname (p
, len
);
2215 const int save_errno
= errno
;
2217 /* No guile exceptions can occur before we have freed p's memory. */
2226 /* scm_from_locale_string may throw an exception. */
2227 const SCM name
= scm_from_locale_string (p
);
2229 /* No guile exceptions can occur before we have freed p's memory. */
2237 #endif /* HAVE_GETHOSTNAME */
2242 scm_init_popen (void)
2244 scm_c_define_gsubr ("open-process", 2, 0, 1, scm_open_process
);
2251 scm_add_feature ("posix");
2253 scm_add_feature ("EIDs");
2256 scm_c_define ("WAIT_ANY", scm_from_int (WAIT_ANY
));
2259 scm_c_define ("WAIT_MYPGRP", scm_from_int (WAIT_MYPGRP
));
2262 scm_c_define ("WNOHANG", scm_from_int (WNOHANG
));
2265 scm_c_define ("WUNTRACED", scm_from_int (WUNTRACED
));
2269 scm_c_define ("LC_COLLATE", scm_from_int (LC_COLLATE
));
2272 scm_c_define ("LC_CTYPE", scm_from_int (LC_CTYPE
));
2275 scm_c_define ("LC_MONETARY", scm_from_int (LC_MONETARY
));
2278 scm_c_define ("LC_NUMERIC", scm_from_int (LC_NUMERIC
));
2281 scm_c_define ("LC_TIME", scm_from_int (LC_TIME
));
2284 scm_c_define ("LC_MESSAGES", scm_from_int (LC_MESSAGES
));
2287 scm_c_define ("LC_ALL", scm_from_int (LC_ALL
));
2290 scm_c_define ("LC_PAPER", scm_from_int (LC_PAPER
));
2293 scm_c_define ("LC_NAME", scm_from_int (LC_NAME
));
2296 scm_c_define ("LC_ADDRESS", scm_from_int (LC_ADDRESS
));
2299 scm_c_define ("LC_TELEPHONE", scm_from_int (LC_TELEPHONE
));
2301 #ifdef LC_MEASUREMENT
2302 scm_c_define ("LC_MEASUREMENT", scm_from_int (LC_MEASUREMENT
));
2304 #ifdef LC_IDENTIFICATION
2305 scm_c_define ("LC_IDENTIFICATION", scm_from_int (LC_IDENTIFICATION
));
2308 scm_c_define ("PIPE_BUF", scm_from_long (PIPE_BUF
));
2312 scm_c_define ("PRIO_PROCESS", scm_from_int (PRIO_PROCESS
));
2315 scm_c_define ("PRIO_PGRP", scm_from_int (PRIO_PGRP
));
2318 scm_c_define ("PRIO_USER", scm_from_int (PRIO_USER
));
2322 scm_c_define ("LOCK_SH", scm_from_int (LOCK_SH
));
2325 scm_c_define ("LOCK_EX", scm_from_int (LOCK_EX
));
2328 scm_c_define ("LOCK_UN", scm_from_int (LOCK_UN
));
2331 scm_c_define ("LOCK_NB", scm_from_int (LOCK_NB
));
2334 #include "libguile/cpp-SIG.c"
2335 #include "libguile/posix.x"
2338 scm_add_feature ("fork");
2339 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
2341 (scm_t_extension_init_func
) scm_init_popen
,
2343 #endif /* HAVE_FORK */