1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 #include "libguile/_scm.h"
35 #include "libguile/dynwind.h"
36 #include "libguile/fports.h"
37 #include "libguile/scmsigs.h"
38 #include "libguile/feature.h"
39 #include "libguile/strings.h"
40 #include "libguile/srfi-13.h"
41 #include "libguile/srfi-14.h"
42 #include "libguile/vectors.h"
43 #include "libguile/values.h"
45 #include "libguile/validate.h"
46 #include "libguile/posix.h"
47 #include "libguile/gettext.h"
48 #include "libguile/threads.h"
54 #ifdef TIME_WITH_SYS_TIME
55 # include <sys/time.h>
59 # include <sys/time.h>
69 extern char *ttyname();
73 #ifdef LIBC_H_WITH_UNISTD_H
77 #include <sys/types.h>
87 #ifdef HAVE_WINSOCK2_H
92 /* Some defines for Windows here. */
94 # define pipe(fd) _pipe (fd, 256, O_BINARY)
95 #endif /* __MINGW32__ */
98 # include <sys/wait.h>
101 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
104 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
112 #ifdef HAVE_SYS_UTSNAME_H
113 #include <sys/utsname.h>
116 #ifdef HAVE_SETLOCALE
120 #if (defined HAVE_NEWLOCALE) && (defined HAVE_STRCOLL_L)
121 # define USE_GNU_LOCALE_API
124 #if (defined USE_GNU_LOCALE_API) && (defined HAVE_XLOCALE_H)
125 # include <xlocale.h>
133 #include <netdb.h> /* for MAXHOSTNAMELEN on Solaris */
136 #ifdef HAVE_SYS_PARAM_H
137 #include <sys/param.h> /* for MAXHOSTNAMELEN */
140 #if HAVE_SYS_RESOURCE_H
141 # include <sys/resource.h>
144 #include <sys/file.h> /* from Gnulib */
146 /* Some Unix systems don't define these. CPP hair is dangerous, but
147 this seems safe enough... */
164 /* No prototype for this on Solaris 10. The man page says it's in
165 <unistd.h> ... but it lies. */
166 #if ! HAVE_DECL_SETHOSTNAME
167 int sethostname (char *name
, size_t namelen
);
170 /* On NextStep, <utime.h> doesn't define struct utime, unless we
171 #define _POSIX_SOURCE before #including it. I think this is less
172 of a kludge than defining struct utimbuf ourselves. */
173 #ifdef UTIMBUF_NEEDS_POSIX
174 #define _POSIX_SOURCE
177 #ifdef HAVE_SYS_UTIME_H
178 #include <sys/utime.h>
185 /* Please don't add any more #includes or #defines here. The hack
186 above means that _POSIX_SOURCE may be #defined, which will
187 encourage header files to do strange things.
189 FIXME: Maybe should undef _POSIX_SOURCE after it's done its job.
191 FIXME: Probably should do all the includes first, then all the fallback
192 declarations and defines, in case things are not in the header we
200 /* Two often used patterns
203 #define WITH_STRING(str,cstr,code) \
205 char *cstr = scm_to_locale_string (str); \
210 #define STRING_SYSCALL(str,cstr,code) \
213 char *cstr = scm_to_locale_string (str); \
214 SCM_SYSCALL (code); \
215 eno = errno; free (cstr); errno = eno; \
220 SCM_SYMBOL (sym_read_pipe
, "read pipe");
221 SCM_SYMBOL (sym_write_pipe
, "write pipe");
223 SCM_DEFINE (scm_pipe
, "pipe", 0, 0, 0,
225 "Return a newly created pipe: a pair of ports which are linked\n"
226 "together on the local machine. The @emph{car} is the input\n"
227 "port and the @emph{cdr} is the output port. Data written (and\n"
228 "flushed) to the output port can be read from the input port.\n"
229 "Pipes are commonly used for communication with a newly forked\n"
230 "child process. The need to flush the output port can be\n"
231 "avoided by making it unbuffered using @code{setvbuf}.\n"
233 "Writes occur atomically provided the size of the data in bytes\n"
234 "is not greater than the value of @code{PIPE_BUF}. Note that\n"
235 "the output port is likely to block if too much data (typically\n"
236 "equal to @code{PIPE_BUF}) has been written but not yet read\n"
237 "from the input port.")
238 #define FUNC_NAME s_scm_pipe
247 p_rd
= scm_fdes_to_port (fd
[0], "r", sym_read_pipe
);
248 p_wt
= scm_fdes_to_port (fd
[1], "w", sym_write_pipe
);
249 return scm_cons (p_rd
, p_wt
);
254 #ifdef HAVE_GETGROUPS
255 SCM_DEFINE (scm_getgroups
, "getgroups", 0, 0, 0,
257 "Return a vector of integers representing the current\n"
258 "supplementary group IDs.")
259 #define FUNC_NAME s_scm_getgroups
266 ngroups
= getgroups (0, NULL
);
270 size
= ngroups
* sizeof (GETGROUPS_T
);
271 groups
= scm_malloc (size
);
272 ngroups
= getgroups (ngroups
, groups
);
274 result
= scm_c_make_vector (ngroups
, SCM_BOOL_F
);
275 while (--ngroups
>= 0)
276 SCM_SIMPLE_VECTOR_SET (result
, ngroups
, scm_from_ulong (groups
[ngroups
]));
284 #ifdef HAVE_SETGROUPS
285 SCM_DEFINE (scm_setgroups
, "setgroups", 1, 0, 0,
287 "Set the current set of supplementary group IDs to the integers\n"
288 "in the given vector @var{vec}. The return value is\n"
291 "Generally only the superuser can set the process group IDs.")
292 #define FUNC_NAME s_scm_setgroups
301 SCM_VALIDATE_VECTOR (SCM_ARG1
, group_vec
);
303 ngroups
= SCM_SIMPLE_VECTOR_LENGTH (group_vec
);
305 /* validate before allocating, so we don't have to worry about leaks */
306 for (i
= 0; i
< ngroups
; i
++)
308 unsigned long ulong_gid
;
310 SCM_VALIDATE_ULONG_COPY (1, SCM_SIMPLE_VECTOR_REF (group_vec
, i
),
313 if (gid
!= ulong_gid
)
314 SCM_OUT_OF_RANGE (1, SCM_SIMPLE_VECTOR_REF (group_vec
, i
));
317 size
= ngroups
* sizeof (GETGROUPS_T
);
318 if (size
/ sizeof (GETGROUPS_T
) != ngroups
)
319 SCM_OUT_OF_RANGE (SCM_ARG1
, scm_from_int (ngroups
));
320 groups
= scm_malloc (size
);
321 for(i
= 0; i
< ngroups
; i
++)
322 groups
[i
] = SCM_NUM2ULONG (1, SCM_SIMPLE_VECTOR_REF (group_vec
, i
));
324 result
= setgroups (ngroups
, groups
);
325 save_errno
= errno
; /* don't let free() touch errno */
330 return SCM_UNSPECIFIED
;
336 SCM_DEFINE (scm_getpwuid
, "getpw", 0, 1, 0,
338 "Look up an entry in the user database. @var{obj} can be an integer,\n"
339 "a string, or omitted, giving the behaviour of getpwuid, getpwnam\n"
340 "or getpwent respectively.")
341 #define FUNC_NAME s_scm_getpwuid
343 struct passwd
*entry
;
345 SCM result
= scm_c_make_vector (7, SCM_UNSPECIFIED
);
346 if (SCM_UNBNDP (user
) || scm_is_false (user
))
348 SCM_SYSCALL (entry
= getpwent ());
354 else if (scm_is_integer (user
))
356 entry
= getpwuid (scm_to_int (user
));
360 WITH_STRING (user
, c_user
,
361 entry
= getpwnam (c_user
));
364 SCM_MISC_ERROR ("entry not found", SCM_EOL
);
366 SCM_SIMPLE_VECTOR_SET(result
, 0, scm_from_locale_string (entry
->pw_name
));
367 SCM_SIMPLE_VECTOR_SET(result
, 1, scm_from_locale_string (entry
->pw_passwd
));
368 SCM_SIMPLE_VECTOR_SET(result
, 2, scm_from_ulong (entry
->pw_uid
));
369 SCM_SIMPLE_VECTOR_SET(result
, 3, scm_from_ulong (entry
->pw_gid
));
370 SCM_SIMPLE_VECTOR_SET(result
, 4, scm_from_locale_string (entry
->pw_gecos
));
372 SCM_SIMPLE_VECTOR_SET(result
, 5, scm_from_locale_string (""));
374 SCM_SIMPLE_VECTOR_SET(result
, 5, scm_from_locale_string (entry
->pw_dir
));
375 if (!entry
->pw_shell
)
376 SCM_SIMPLE_VECTOR_SET(result
, 6, scm_from_locale_string (""));
378 SCM_SIMPLE_VECTOR_SET(result
, 6, scm_from_locale_string (entry
->pw_shell
));
382 #endif /* HAVE_GETPWENT */
386 SCM_DEFINE (scm_setpwent
, "setpw", 0, 1, 0,
388 "If called with a true argument, initialize or reset the password data\n"
389 "stream. Otherwise, close the stream. The @code{setpwent} and\n"
390 "@code{endpwent} procedures are implemented on top of this.")
391 #define FUNC_NAME s_scm_setpwent
393 if (SCM_UNBNDP (arg
) || scm_is_false (arg
))
397 return SCM_UNSPECIFIED
;
404 /* Combines getgrgid and getgrnam. */
405 SCM_DEFINE (scm_getgrgid
, "getgr", 0, 1, 0,
407 "Look up an entry in the group database. @var{obj} can be an integer,\n"
408 "a string, or omitted, giving the behaviour of getgrgid, getgrnam\n"
409 "or getgrent respectively.")
410 #define FUNC_NAME s_scm_getgrgid
413 SCM result
= scm_c_make_vector (4, SCM_UNSPECIFIED
);
415 if (SCM_UNBNDP (name
) || scm_is_false (name
))
417 SCM_SYSCALL (entry
= getgrent ());
423 else if (scm_is_integer (name
))
424 SCM_SYSCALL (entry
= getgrgid (scm_to_int (name
)));
426 STRING_SYSCALL (name
, c_name
,
427 entry
= getgrnam (c_name
));
431 SCM_SIMPLE_VECTOR_SET(result
, 0, scm_from_locale_string (entry
->gr_name
));
432 SCM_SIMPLE_VECTOR_SET(result
, 1, scm_from_locale_string (entry
->gr_passwd
));
433 SCM_SIMPLE_VECTOR_SET(result
, 2, scm_from_ulong (entry
->gr_gid
));
434 SCM_SIMPLE_VECTOR_SET(result
, 3, scm_makfromstrs (-1, entry
->gr_mem
));
441 SCM_DEFINE (scm_setgrent
, "setgr", 0, 1, 0,
443 "If called with a true argument, initialize or reset the group data\n"
444 "stream. Otherwise, close the stream. The @code{setgrent} and\n"
445 "@code{endgrent} procedures are implemented on top of this.")
446 #define FUNC_NAME s_scm_setgrent
448 if (SCM_UNBNDP (arg
) || scm_is_false (arg
))
452 return SCM_UNSPECIFIED
;
455 #endif /* HAVE_GETGRENT */
458 #ifdef HAVE_GETRLIMIT
460 SCM_SYMBOL (sym_as
, "as");
463 SCM_SYMBOL (sym_core
, "core");
466 SCM_SYMBOL (sym_cpu
, "cpu");
469 SCM_SYMBOL (sym_data
, "data");
472 SCM_SYMBOL (sym_fsize
, "fsize");
474 #ifdef RLIMIT_MEMLOCK
475 SCM_SYMBOL (sym_memlock
, "memlock");
477 #ifdef RLIMIT_MSGQUEUE
478 SCM_SYMBOL (sym_msgqueue
, "msgqueue");
481 SCM_SYMBOL (sym_nice
, "nice");
484 SCM_SYMBOL (sym_nofile
, "nofile");
487 SCM_SYMBOL (sym_nproc
, "nproc");
490 SCM_SYMBOL (sym_rss
, "rss");
493 SCM_SYMBOL (sym_rtprio
, "rtprio");
496 SCM_SYMBOL (sym_rttime
, "rttime");
498 #ifdef RLIMIT_SIGPENDING
499 SCM_SYMBOL (sym_sigpending
, "sigpending");
502 SCM_SYMBOL (sym_stack
, "stack");
506 scm_to_resource (SCM s
, const char *func
, int pos
)
508 if (scm_is_number (s
))
509 return scm_to_int (s
);
511 SCM_ASSERT_TYPE (scm_is_symbol (s
), s
, pos
, func
, "symbol");
533 #ifdef RLIMIT_MEMLOCK
534 if (s
== sym_memlock
)
535 return RLIMIT_MEMLOCK
;
537 #ifdef RLIMIT_MSGQUEUE
538 if (s
== sym_msgqueue
)
539 return RLIMIT_MSGQUEUE
;
547 return RLIMIT_NOFILE
;
559 return RLIMIT_RTPRIO
;
563 return RLIMIT_RTPRIO
;
565 #ifdef RLIMIT_SIGPENDING
566 if (s
== sym_sigpending
)
567 return RLIMIT_SIGPENDING
;
574 scm_misc_error (func
, "invalid rlimit resource ~A", scm_list_1 (s
));
578 SCM_DEFINE (scm_getrlimit
, "getrlimit", 1, 0, 0,
580 "Get a resource limit for this process. @var{resource} identifies the resource,\n"
581 "either as an integer or as a symbol. For example, @code{(getrlimit 'stack)}\n"
582 "gets the limits associated with @code{RLIMIT_STACK}.\n\n"
583 "@code{getrlimit} returns two values, the soft and the hard limit. If no\n"
584 "limit is set for the resource in question, the returned limit will be @code{#f}.")
585 #define FUNC_NAME s_scm_getrlimit
588 struct rlimit lim
= { 0, 0 };
590 iresource
= scm_to_resource (resource
, FUNC_NAME
, 1);
592 if (getrlimit (iresource
, &lim
) != 0)
593 scm_syserror (FUNC_NAME
);
595 return scm_values (scm_list_2 ((lim
.rlim_cur
== RLIM_INFINITY
) ? SCM_BOOL_F
596 : scm_from_long (lim
.rlim_cur
),
597 (lim
.rlim_max
== RLIM_INFINITY
) ? SCM_BOOL_F
598 : scm_from_long (lim
.rlim_max
)));
603 #ifdef HAVE_SETRLIMIT
604 SCM_DEFINE (scm_setrlimit
, "setrlimit", 3, 0, 0,
605 (SCM resource
, SCM soft
, SCM hard
),
606 "Set a resource limit for this process. @var{resource} identifies the resource,\n"
607 "either as an integer or as a symbol. @var{soft} and @var{hard} should be integers,\n"
608 "or @code{#f} to indicate no limit (i.e., @code{RLIM_INFINITY}).\n\n"
609 "For example, @code{(setrlimit 'stack 150000 300000)} sets the @code{RLIMIT_STACK}\n"
610 "limit to 150 kilobytes, with a hard limit of 300 kB.")
611 #define FUNC_NAME s_scm_setrlimit
614 struct rlimit lim
= { 0, 0 };
616 iresource
= scm_to_resource (resource
, FUNC_NAME
, 1);
618 lim
.rlim_cur
= (soft
== SCM_BOOL_F
) ? RLIM_INFINITY
: scm_to_long (soft
);
619 lim
.rlim_max
= (hard
== SCM_BOOL_F
) ? RLIM_INFINITY
: scm_to_long (hard
);
621 if (setrlimit (iresource
, &lim
) != 0)
622 scm_syserror (FUNC_NAME
);
624 return SCM_UNSPECIFIED
;
627 #endif /* HAVE_SETRLIMIT */
628 #endif /* HAVE_GETRLIMIT */
631 SCM_DEFINE (scm_kill
, "kill", 2, 0, 0,
633 "Sends a signal to the specified process or group of processes.\n\n"
634 "@var{pid} specifies the processes to which the signal is sent:\n\n"
636 "@item @var{pid} greater than 0\n"
637 "The process whose identifier is @var{pid}.\n"
638 "@item @var{pid} equal to 0\n"
639 "All processes in the current process group.\n"
640 "@item @var{pid} less than -1\n"
641 "The process group whose identifier is -@var{pid}\n"
642 "@item @var{pid} equal to -1\n"
643 "If the process is privileged, all processes except for some special\n"
644 "system processes. Otherwise, all processes with the current effective\n"
647 "@var{sig} should be specified using a variable corresponding to\n"
648 "the Unix symbolic name, e.g.,\n\n"
653 "Interrupt signal.\n"
655 #define FUNC_NAME s_scm_kill
657 /* Signal values are interned in scm_init_posix(). */
659 if (kill (scm_to_int (pid
), scm_to_int (sig
)) != 0)
662 /* Mingw has raise(), but not kill(). (Other raw DOS environments might
663 be similar.) Use raise() when the requested pid is our own process,
665 if (scm_to_int (pid
) == getpid ())
667 if (raise (scm_to_int (sig
)) != 0)
679 return SCM_UNSPECIFIED
;
684 SCM_DEFINE (scm_waitpid
, "waitpid", 1, 1, 0,
685 (SCM pid
, SCM options
),
686 "This procedure collects status information from a child process which\n"
687 "has terminated or (optionally) stopped. Normally it will\n"
688 "suspend the calling process until this can be done. If more than one\n"
689 "child process is eligible then one will be chosen by the operating system.\n\n"
690 "The value of @var{pid} determines the behaviour:\n\n"
692 "@item @var{pid} greater than 0\n"
693 "Request status information from the specified child process.\n"
694 "@item @var{pid} equal to -1 or WAIT_ANY\n"
695 "Request status information for any child process.\n"
696 "@item @var{pid} equal to 0 or WAIT_MYPGRP\n"
697 "Request status information for any child process in the current process\n"
699 "@item @var{pid} less than -1\n"
700 "Request status information for any child process whose process group ID\n"
703 "The @var{options} argument, if supplied, should be the bitwise OR of the\n"
704 "values of zero or more of the following variables:\n\n"
706 "Return immediately even if there are no child processes to be collected.\n"
708 "@defvar WUNTRACED\n"
709 "Report status information for stopped processes as well as terminated\n"
712 "The return value is a pair containing:\n\n"
715 "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
716 "specified and no process was collected.\n"
718 "The integer status value.\n"
720 #define FUNC_NAME s_scm_waitpid
725 if (SCM_UNBNDP (options
))
729 /* Flags are interned in scm_init_posix. */
730 ioptions
= scm_to_int (options
);
732 SCM_SYSCALL (i
= waitpid (scm_to_int (pid
), &status
, ioptions
));
735 return scm_cons (scm_from_int (i
), scm_from_int (status
));
738 #endif /* HAVE_WAITPID */
741 SCM_DEFINE (scm_status_exit_val
, "status:exit-val", 1, 0, 0,
743 "Return the exit status value, as would be set if a process\n"
744 "ended normally through a call to @code{exit} or @code{_exit},\n"
745 "if any, otherwise @code{#f}.")
746 #define FUNC_NAME s_scm_status_exit_val
750 /* On Ultrix, the WIF... macros assume their argument is an lvalue;
752 lstatus
= scm_to_int (status
);
753 if (WIFEXITED (lstatus
))
754 return (scm_from_int (WEXITSTATUS (lstatus
)));
760 SCM_DEFINE (scm_status_term_sig
, "status:term-sig", 1, 0, 0,
762 "Return the signal number which terminated the process, if any,\n"
763 "otherwise @code{#f}.")
764 #define FUNC_NAME s_scm_status_term_sig
768 lstatus
= scm_to_int (status
);
769 if (WIFSIGNALED (lstatus
))
770 return scm_from_int (WTERMSIG (lstatus
));
776 SCM_DEFINE (scm_status_stop_sig
, "status:stop-sig", 1, 0, 0,
778 "Return the signal number which stopped the process, if any,\n"
779 "otherwise @code{#f}.")
780 #define FUNC_NAME s_scm_status_stop_sig
784 lstatus
= scm_to_int (status
);
785 if (WIFSTOPPED (lstatus
))
786 return scm_from_int (WSTOPSIG (lstatus
));
791 #endif /* __MINGW32__ */
794 SCM_DEFINE (scm_getppid
, "getppid", 0, 0, 0,
796 "Return an integer representing the process ID of the parent\n"
798 #define FUNC_NAME s_scm_getppid
800 return scm_from_int (getppid ());
803 #endif /* HAVE_GETPPID */
807 SCM_DEFINE (scm_getuid
, "getuid", 0, 0, 0,
809 "Return an integer representing the current real user ID.")
810 #define FUNC_NAME s_scm_getuid
812 return scm_from_int (getuid ());
818 SCM_DEFINE (scm_getgid
, "getgid", 0, 0, 0,
820 "Return an integer representing the current real group ID.")
821 #define FUNC_NAME s_scm_getgid
823 return scm_from_int (getgid ());
829 SCM_DEFINE (scm_geteuid
, "geteuid", 0, 0, 0,
831 "Return an integer representing the current effective user ID.\n"
832 "If the system does not support effective IDs, then the real ID\n"
833 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
834 "system supports effective IDs.")
835 #define FUNC_NAME s_scm_geteuid
838 return scm_from_int (geteuid ());
840 return scm_from_int (getuid ());
846 SCM_DEFINE (scm_getegid
, "getegid", 0, 0, 0,
848 "Return an integer representing the current effective group ID.\n"
849 "If the system does not support effective IDs, then the real ID\n"
850 "is returned. @code{(provided? 'EIDs)} reports whether the\n"
851 "system supports effective IDs.")
852 #define FUNC_NAME s_scm_getegid
855 return scm_from_int (getegid ());
857 return scm_from_int (getgid ());
863 SCM_DEFINE (scm_setuid
, "setuid", 1, 0, 0,
865 "Sets both the real and effective user IDs to the integer @var{id}, provided\n"
866 "the process has appropriate privileges.\n"
867 "The return value is unspecified.")
868 #define FUNC_NAME s_scm_setuid
870 if (setuid (scm_to_int (id
)) != 0)
872 return SCM_UNSPECIFIED
;
876 SCM_DEFINE (scm_setgid
, "setgid", 1, 0, 0,
878 "Sets both the real and effective group IDs to the integer @var{id}, provided\n"
879 "the process has appropriate privileges.\n"
880 "The return value is unspecified.")
881 #define FUNC_NAME s_scm_setgid
883 if (setgid (scm_to_int (id
)) != 0)
885 return SCM_UNSPECIFIED
;
889 SCM_DEFINE (scm_seteuid
, "seteuid", 1, 0, 0,
891 "Sets the effective user ID to the integer @var{id}, provided the process\n"
892 "has appropriate privileges. If effective IDs are not supported, the\n"
893 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
894 "system supports effective IDs.\n"
895 "The return value is unspecified.")
896 #define FUNC_NAME s_scm_seteuid
901 rv
= seteuid (scm_to_int (id
));
903 rv
= setuid (scm_to_int (id
));
907 return SCM_UNSPECIFIED
;
910 #endif /* __MINGW32__ */
914 SCM_DEFINE (scm_setegid
, "setegid", 1, 0, 0,
916 "Sets the effective group ID to the integer @var{id}, provided the process\n"
917 "has appropriate privileges. If effective IDs are not supported, the\n"
918 "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
919 "system supports effective IDs.\n"
920 "The return value is unspecified.")
921 #define FUNC_NAME s_scm_setegid
926 rv
= setegid (scm_to_int (id
));
928 rv
= setgid (scm_to_int (id
));
932 return SCM_UNSPECIFIED
;
940 SCM_DEFINE (scm_getpgrp
, "getpgrp", 0, 0, 0,
942 "Return an integer representing the current process group ID.\n"
943 "This is the POSIX definition, not BSD.")
944 #define FUNC_NAME s_scm_getpgrp
947 fn
= (int (*) ()) getpgrp
;
948 return scm_from_int (fn (0));
951 #endif /* HAVE_GETPGRP */
955 SCM_DEFINE (scm_setpgid
, "setpgid", 2, 0, 0,
957 "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n"
958 "@var{pgid} must be integers: they can be zero to indicate the ID of the\n"
960 "Fails on systems that do not support job control.\n"
961 "The return value is unspecified.")
962 #define FUNC_NAME s_scm_setpgid
964 /* FIXME(?): may be known as setpgrp. */
965 if (setpgid (scm_to_int (pid
), scm_to_int (pgid
)) != 0)
967 return SCM_UNSPECIFIED
;
970 #endif /* HAVE_SETPGID */
973 SCM_DEFINE (scm_setsid
, "setsid", 0, 0, 0,
975 "Creates a new session. The current process becomes the session leader\n"
976 "and is put in a new process group. The process will be detached\n"
977 "from its controlling terminal if it has one.\n"
978 "The return value is an integer representing the new process group ID.")
979 #define FUNC_NAME s_scm_setsid
981 pid_t sid
= setsid ();
984 return SCM_UNSPECIFIED
;
987 #endif /* HAVE_SETSID */
990 SCM_DEFINE (scm_getsid
, "getsid", 1, 0, 0,
992 "Returns the session ID of process @var{pid}. (The session\n"
993 "ID of a process is the process group ID of its session leader.)")
994 #define FUNC_NAME s_scm_getsid
996 return scm_from_int (getsid (scm_to_int (pid
)));
999 #endif /* HAVE_GETSID */
1002 /* ttyname returns its result in a single static buffer, hence
1003 scm_i_misc_mutex for thread safety. In glibc 2.3.2 two threads
1004 continuously calling ttyname will otherwise get an overwrite quite
1007 ttyname_r (when available) could be used instead of scm_i_misc_mutex, but
1008 there's probably little to be gained in either speed or parallelism. */
1011 SCM_DEFINE (scm_ttyname
, "ttyname", 1, 0, 0,
1013 "Return a string with the name of the serial terminal device\n"
1014 "underlying @var{port}.")
1015 #define FUNC_NAME s_scm_ttyname
1019 SCM ret
= SCM_BOOL_F
;
1021 port
= SCM_COERCE_OUTPORT (port
);
1022 SCM_VALIDATE_OPPORT (1, port
);
1023 if (!SCM_FPORTP (port
))
1025 fd
= SCM_FPORT_FDES (port
);
1027 scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex
);
1029 SCM_SYSCALL (result
= ttyname (fd
));
1032 result
= strdup (result
);
1034 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex
);
1042 ret
= scm_take_locale_string (result
);
1047 #endif /* HAVE_TTYNAME */
1050 /* For thread safety "buf" is used instead of NULL for the ctermid static
1051 buffer. Actually it's unlikely the controlling terminal will change
1052 during program execution, and indeed on glibc (2.3.2) it's always just
1053 "/dev/tty", but L_ctermid on the stack is easy and fast and guarantees
1054 safety everywhere. */
1056 SCM_DEFINE (scm_ctermid
, "ctermid", 0, 0, 0,
1058 "Return a string containing the file name of the controlling\n"
1059 "terminal for the current process.")
1060 #define FUNC_NAME s_scm_ctermid
1062 char buf
[L_ctermid
];
1063 char *result
= ctermid (buf
);
1064 if (*result
== '\0')
1066 return scm_from_locale_string (result
);
1069 #endif /* HAVE_CTERMID */
1071 #ifdef HAVE_TCGETPGRP
1072 SCM_DEFINE (scm_tcgetpgrp
, "tcgetpgrp", 1, 0, 0,
1074 "Return the process group ID of the foreground process group\n"
1075 "associated with the terminal open on the file descriptor\n"
1076 "underlying @var{port}.\n"
1078 "If there is no foreground process group, the return value is a\n"
1079 "number greater than 1 that does not match the process group ID\n"
1080 "of any existing process group. This can happen if all of the\n"
1081 "processes in the job that was formerly the foreground job have\n"
1082 "terminated, and no other job has yet been moved into the\n"
1084 #define FUNC_NAME s_scm_tcgetpgrp
1089 port
= SCM_COERCE_OUTPORT (port
);
1091 SCM_VALIDATE_OPFPORT (1, port
);
1092 fd
= SCM_FPORT_FDES (port
);
1093 if ((pgid
= tcgetpgrp (fd
)) == -1)
1095 return scm_from_int (pgid
);
1098 #endif /* HAVE_TCGETPGRP */
1100 #ifdef HAVE_TCSETPGRP
1101 SCM_DEFINE (scm_tcsetpgrp
, "tcsetpgrp", 2, 0, 0,
1102 (SCM port
, SCM pgid
),
1103 "Set the foreground process group ID for the terminal used by the file\n"
1104 "descriptor underlying @var{port} to the integer @var{pgid}.\n"
1105 "The calling process\n"
1106 "must be a member of the same session as @var{pgid} and must have the same\n"
1107 "controlling terminal. The return value is unspecified.")
1108 #define FUNC_NAME s_scm_tcsetpgrp
1112 port
= SCM_COERCE_OUTPORT (port
);
1114 SCM_VALIDATE_OPFPORT (1, port
);
1115 fd
= SCM_FPORT_FDES (port
);
1116 if (tcsetpgrp (fd
, scm_to_int (pgid
)) == -1)
1118 return SCM_UNSPECIFIED
;
1121 #endif /* HAVE_TCSETPGRP */
1123 SCM_DEFINE (scm_execl
, "execl", 1, 0, 1,
1124 (SCM filename
, SCM args
),
1125 "Executes the file named by @var{path} as a new process image.\n"
1126 "The remaining arguments are supplied to the process; from a C program\n"
1127 "they are accessible as the @code{argv} argument to @code{main}.\n"
1128 "Conventionally the first @var{arg} is the same as @var{path}.\n"
1129 "All arguments must be strings.\n\n"
1130 "If @var{arg} is missing, @var{path} is executed with a null\n"
1131 "argument list, which may have system-dependent side-effects.\n\n"
1132 "This procedure is currently implemented using the @code{execv} system\n"
1133 "call, but we call it @code{execl} because of its Scheme calling interface.")
1134 #define FUNC_NAME s_scm_execl
1139 scm_dynwind_begin (0);
1141 exec_file
= scm_to_locale_string (filename
);
1142 scm_dynwind_free (exec_file
);
1144 exec_argv
= scm_i_allocate_string_pointers (args
);
1148 /* extra "const" in mingw formals, provokes warning from gcc */
1149 (const char * const *)
1160 SCM_DEFINE (scm_execlp
, "execlp", 1, 0, 1,
1161 (SCM filename
, SCM args
),
1162 "Similar to @code{execl}, however if\n"
1163 "@var{filename} does not contain a slash\n"
1164 "then the file to execute will be located by searching the\n"
1165 "directories listed in the @code{PATH} environment variable.\n\n"
1166 "This procedure is currently implemented using the @code{execvp} system\n"
1167 "call, but we call it @code{execlp} because of its Scheme calling interface.")
1168 #define FUNC_NAME s_scm_execlp
1173 scm_dynwind_begin (0);
1175 exec_file
= scm_to_locale_string (filename
);
1176 scm_dynwind_free (exec_file
);
1178 exec_argv
= scm_i_allocate_string_pointers (args
);
1182 /* extra "const" in mingw formals, provokes warning from gcc */
1183 (const char * const *)
1195 /* OPTIMIZE-ME: scm_execle doesn't need malloced copies of the environment
1196 list strings the way environ_list_to_c gives. */
1198 SCM_DEFINE (scm_execle
, "execle", 2, 0, 1,
1199 (SCM filename
, SCM env
, SCM args
),
1200 "Similar to @code{execl}, but the environment of the new process is\n"
1201 "specified by @var{env}, which must be a list of strings as returned by the\n"
1202 "@code{environ} procedure.\n\n"
1203 "This procedure is currently implemented using the @code{execve} system\n"
1204 "call, but we call it @code{execle} because of its Scheme calling interface.")
1205 #define FUNC_NAME s_scm_execle
1211 scm_dynwind_begin (0);
1213 exec_file
= scm_to_locale_string (filename
);
1214 scm_dynwind_free (exec_file
);
1216 exec_argv
= scm_i_allocate_string_pointers (args
);
1217 exec_env
= scm_i_allocate_string_pointers (env
);
1221 /* extra "const" in mingw formals, provokes warning from gcc */
1222 (const char * const *)
1226 /* extra "const" in mingw formals, provokes warning from gcc */
1227 (const char * const *)
1239 SCM_DEFINE (scm_fork
, "primitive-fork", 0, 0, 0,
1241 "Creates a new \"child\" process by duplicating the current \"parent\" process.\n"
1242 "In the child the return value is 0. In the parent the return value is\n"
1243 "the integer process ID of the child.\n\n"
1244 "This procedure has been renamed from @code{fork} to avoid a naming conflict\n"
1245 "with the scsh fork.")
1246 #define FUNC_NAME s_scm_fork
1252 return scm_from_int (pid
);
1255 #endif /* HAVE_FORK */
1258 # include "win32-uname.h"
1261 #if defined (HAVE_UNAME) || defined (__MINGW32__)
1262 SCM_DEFINE (scm_uname
, "uname", 0, 0, 0,
1264 "Return an object with some information about the computer\n"
1265 "system the program is running on.")
1266 #define FUNC_NAME s_scm_uname
1269 SCM result
= scm_c_make_vector (5, SCM_UNSPECIFIED
);
1270 if (uname (&buf
) < 0)
1272 SCM_SIMPLE_VECTOR_SET(result
, 0, scm_from_locale_string (buf
.sysname
));
1273 SCM_SIMPLE_VECTOR_SET(result
, 1, scm_from_locale_string (buf
.nodename
));
1274 SCM_SIMPLE_VECTOR_SET(result
, 2, scm_from_locale_string (buf
.release
));
1275 SCM_SIMPLE_VECTOR_SET(result
, 3, scm_from_locale_string (buf
.version
));
1276 SCM_SIMPLE_VECTOR_SET(result
, 4, scm_from_locale_string (buf
.machine
));
1279 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (buf.domainname));
1284 #endif /* HAVE_UNAME */
1286 SCM_DEFINE (scm_environ
, "environ", 0, 1, 0,
1288 "If @var{env} is omitted, return the current environment (in the\n"
1289 "Unix sense) as a list of strings. Otherwise set the current\n"
1290 "environment, which is also the default environment for child\n"
1291 "processes, to the supplied list of strings. Each member of\n"
1292 "@var{env} should be of the form @code{NAME=VALUE} and values of\n"
1293 "@code{NAME} should not be duplicated. If @var{env} is supplied\n"
1294 "then the return value is unspecified.")
1295 #define FUNC_NAME s_scm_environ
1297 if (SCM_UNBNDP (env
))
1298 return scm_makfromstrs (-1, environ
);
1301 environ
= scm_i_allocate_string_pointers (env
);
1302 return SCM_UNSPECIFIED
;
1309 SCM_DEFINE (scm_tmpnam
, "tmpnam", 0, 0, 0,
1311 "Return a name in the file system that does not match any\n"
1312 "existing file. However there is no guarantee that another\n"
1313 "process will not create the file after @code{tmpnam} is called.\n"
1314 "Care should be taken if opening the file, e.g., use the\n"
1315 "@code{O_EXCL} open flag or use @code{mkstemp!} instead.")
1316 #define FUNC_NAME s_scm_tmpnam
1318 char name
[L_tmpnam
];
1321 SCM_SYSCALL (rv
= tmpnam (name
));
1323 /* not SCM_SYSERROR since errno probably not set. */
1324 SCM_MISC_ERROR ("tmpnam failed", SCM_EOL
);
1325 return scm_from_locale_string (name
);
1331 SCM_DEFINE (scm_tmpfile
, "tmpfile", 0, 0, 0,
1333 "Return an input/output port to a unique temporary file\n"
1334 "named using the path prefix @code{P_tmpdir} defined in\n"
1336 "The file is automatically deleted when the port is closed\n"
1337 "or the program terminates.")
1338 #define FUNC_NAME s_scm_tmpfile
1342 if (! (rv
= tmpfile ()))
1344 return scm_fdes_to_port (fileno (rv
), "w+", SCM_BOOL_F
);
1348 SCM_DEFINE (scm_utime
, "utime", 1, 5, 0,
1349 (SCM pathname
, SCM actime
, SCM modtime
, SCM actimens
, SCM modtimens
,
1351 "@code{utime} sets the access and modification times for the\n"
1352 "file named by @var{path}. If @var{actime} or @var{modtime} is\n"
1353 "not supplied, then the current time is used. @var{actime} and\n"
1354 "@var{modtime} must be integer time values as returned by the\n"
1355 "@code{current-time} procedure.\n\n"
1356 "The optional @var{actimens} and @var{modtimens} are nanoseconds\n"
1357 "to add @var{actime} and @var{modtime}. Nanosecond precision is\n"
1358 "only supported on some combinations of file systems and operating\n"
1361 "(utime \"foo\" (- (current-time) 3600))\n"
1363 "will set the access time to one hour in the past and the\n"
1364 "modification time to the current time.")
1365 #define FUNC_NAME s_scm_utime
1368 time_t atim_sec
, mtim_sec
;
1369 long atim_nsec
, mtim_nsec
;
1372 if (SCM_UNBNDP (actime
))
1374 #ifdef HAVE_UTIMENSAT
1376 atim_nsec
= UTIME_NOW
;
1378 SCM_SYSCALL (time (&atim_sec
));
1384 atim_sec
= SCM_NUM2ULONG (2, actime
);
1385 if (SCM_UNBNDP (actimens
))
1388 atim_nsec
= SCM_NUM2LONG (4, actimens
);
1391 if (SCM_UNBNDP (modtime
))
1393 #ifdef HAVE_UTIMENSAT
1395 mtim_nsec
= UTIME_NOW
;
1397 SCM_SYSCALL (time (&mtim_sec
));
1403 mtim_sec
= SCM_NUM2ULONG (3, modtime
);
1404 if (SCM_UNBNDP (modtimens
))
1407 mtim_nsec
= SCM_NUM2LONG (5, modtimens
);
1410 if (SCM_UNBNDP (flags
))
1413 f
= SCM_NUM2INT (6, flags
);
1415 #ifdef HAVE_UTIMENSAT
1417 struct timespec times
[2];
1418 times
[0].tv_sec
= atim_sec
;
1419 times
[0].tv_nsec
= atim_nsec
;
1420 times
[1].tv_sec
= mtim_sec
;
1421 times
[1].tv_nsec
= mtim_nsec
;
1423 STRING_SYSCALL (pathname
, c_pathname
,
1424 rv
= utimensat (AT_FDCWD
, c_pathname
, times
, f
));
1429 utm
.actime
= atim_sec
;
1430 utm
.modtime
= mtim_sec
;
1432 STRING_SYSCALL (pathname
, c_pathname
,
1433 rv
= utime (c_pathname
, &utm
));
1439 return SCM_UNSPECIFIED
;
1443 SCM_DEFINE (scm_getpid
, "getpid", 0, 0, 0,
1445 "Return an integer representing the current process ID.")
1446 #define FUNC_NAME s_scm_getpid
1448 return scm_from_ulong (getpid ());
1452 SCM_DEFINE (scm_putenv
, "putenv", 1, 0, 0,
1454 "Modifies the environment of the current process, which is\n"
1455 "also the default environment inherited by child processes.\n\n"
1456 "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n"
1457 "directly into the environment, replacing any existing environment string\n"
1459 "name matching @code{NAME}. If @var{string} does not contain an equal\n"
1460 "sign, then any existing string with name matching @var{string} will\n"
1462 "The return value is unspecified.")
1463 #define FUNC_NAME s_scm_putenv
1466 char *c_str
= scm_to_locale_string (str
);
1468 /* Leave C_STR in the environment. */
1470 /* Gnulib's `putenv' module honors the semantics described above. */
1471 rv
= putenv (c_str
);
1475 return SCM_UNSPECIFIED
;
1479 /* This mutex is used to serialize invocations of `setlocale ()' on non-GNU
1480 systems (i.e., systems where a reentrant locale API is not available). It
1481 is also acquired before calls to `nl_langinfo ()'. See `i18n.c' for
1483 scm_i_pthread_mutex_t scm_i_locale_mutex
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
1485 #ifdef HAVE_SETLOCALE
1487 SCM_DEFINE (scm_setlocale
, "setlocale", 1, 1, 0,
1488 (SCM category
, SCM locale
),
1489 "If @var{locale} is omitted, return the current value of the\n"
1490 "specified locale category as a system-dependent string.\n"
1491 "@var{category} should be specified using the values\n"
1492 "@code{LC_COLLATE}, @code{LC_ALL} etc.\n"
1494 "Otherwise the specified locale category is set to the string\n"
1495 "@var{locale} and the new value is returned as a\n"
1496 "system-dependent string. If @var{locale} is an empty string,\n"
1497 "the locale will be set using environment variables.\n"
1499 "When the locale is changed, the character encoding of the new\n"
1500 "locale (UTF-8, ISO-8859-1, etc.) is used for the current\n"
1501 "input, output, and error ports\n")
1502 #define FUNC_NAME s_scm_setlocale
1509 scm_dynwind_begin (0);
1511 if (SCM_UNBNDP (locale
))
1517 clocale
= scm_to_locale_string (locale
);
1518 scm_dynwind_free (clocale
);
1521 c_category
= scm_i_to_lc_category (category
, 1);
1523 scm_i_pthread_mutex_lock (&scm_i_locale_mutex
);
1524 rv
= setlocale (c_category
, clocale
);
1525 scm_i_pthread_mutex_unlock (&scm_i_locale_mutex
);
1529 /* POSIX and C99 don't say anything about setlocale setting errno, so
1530 force a sensible value here. glibc leaves ENOENT, which would be
1531 fine, but it's not a documented feature. */
1536 enc
= locale_charset ();
1538 /* Set the default encoding for new ports. */
1539 scm_i_set_default_port_encoding (enc
);
1541 /* Set the encoding for the stdio ports. */
1542 scm_i_set_port_encoding_x (scm_current_input_port (), enc
);
1543 scm_i_set_port_encoding_x (scm_current_output_port (), enc
);
1544 scm_i_set_port_encoding_x (scm_current_error_port (), enc
);
1547 return scm_from_locale_string (rv
);
1550 #endif /* HAVE_SETLOCALE */
1553 SCM_DEFINE (scm_mknod
, "mknod", 4, 0, 0,
1554 (SCM path
, SCM type
, SCM perms
, SCM dev
),
1555 "Creates a new special file, such as a file corresponding to a device.\n"
1556 "@var{path} specifies the name of the file. @var{type} should\n"
1557 "be one of the following symbols:\n"
1558 "regular, directory, symlink, block-special, char-special,\n"
1559 "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
1560 "@var{dev} (an integer) specifies which device the special file refers\n"
1561 "to. Its exact interpretation depends on the kind of special file\n"
1562 "being created.\n\n"
1565 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
1567 "The return value is unspecified.")
1568 #define FUNC_NAME s_scm_mknod
1574 SCM_VALIDATE_STRING (1, path
);
1575 SCM_VALIDATE_SYMBOL (2, type
);
1577 p
= scm_i_symbol_chars (type
);
1578 if (strcmp (p
, "regular") == 0)
1580 else if (strcmp (p
, "directory") == 0)
1583 /* systems without symlinks probably don't have S_IFLNK defined */
1584 else if (strcmp (p
, "symlink") == 0)
1587 else if (strcmp (p
, "block-special") == 0)
1589 else if (strcmp (p
, "char-special") == 0)
1591 else if (strcmp (p
, "fifo") == 0)
1594 else if (strcmp (p
, "socket") == 0)
1598 SCM_OUT_OF_RANGE (2, type
);
1600 STRING_SYSCALL (path
, c_path
,
1601 val
= mknod (c_path
,
1602 ctype
| scm_to_int (perms
),
1606 return SCM_UNSPECIFIED
;
1609 #endif /* HAVE_MKNOD */
1612 SCM_DEFINE (scm_nice
, "nice", 1, 0, 0,
1614 "Increment the priority of the current process by @var{incr}. A higher\n"
1615 "priority value means that the process runs less often.\n"
1616 "The return value is unspecified.")
1617 #define FUNC_NAME s_scm_nice
1619 /* nice() returns "prio-NZERO" on success or -1 on error, but -1 can arise
1620 from "prio-NZERO", so an error must be detected from errno changed */
1622 nice (scm_to_int (incr
));
1626 return SCM_UNSPECIFIED
;
1629 #endif /* HAVE_NICE */
1632 SCM_DEFINE (scm_sync
, "sync", 0, 0, 0,
1634 "Flush the operating system disk buffers.\n"
1635 "The return value is unspecified.")
1636 #define FUNC_NAME s_scm_sync
1639 return SCM_UNSPECIFIED
;
1642 #endif /* HAVE_SYNC */
1645 /* crypt() returns a pointer to a static buffer, so we use scm_i_misc_mutex
1646 to avoid another thread overwriting it. A test program running crypt
1647 continuously in two threads can be quickly seen tripping this problem.
1648 crypt() is pretty slow normally, so a mutex shouldn't add much overhead.
1650 glibc has a thread-safe crypt_r, but (in version 2.3.2) it runs a lot
1651 slower (about 5x) than plain crypt if you pass an uninitialized data
1652 block each time. Presumably there's some one-time setups. The best way
1653 to use crypt_r for parallel execution in multiple threads would probably
1654 be to maintain a little pool of initialized crypt_data structures, take
1655 one and use it, then return it to the pool. That pool could be garbage
1656 collected so it didn't add permanently to memory use if only a few crypt
1657 calls are made. But we expect crypt will be used rarely, and even more
1658 rarely will there be any desire for lots of parallel execution on
1659 multiple cpus. So for now we don't bother with anything fancy, just
1663 SCM_DEFINE (scm_crypt
, "crypt", 2, 0, 0,
1664 (SCM key
, SCM salt
),
1665 "Encrypt @var{key} using @var{salt} as the salt value to the\n"
1666 "crypt(3) library call.")
1667 #define FUNC_NAME s_scm_crypt
1670 char *c_key
, *c_salt
, *c_ret
;
1672 scm_dynwind_begin (0);
1673 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex
);
1675 c_key
= scm_to_locale_string (key
);
1676 scm_dynwind_free (c_key
);
1677 c_salt
= scm_to_locale_string (salt
);
1678 scm_dynwind_free (c_salt
);
1680 /* The Linux crypt(3) man page says crypt will return NULL and set errno
1681 on error. (Eg. ENOSYS if legal restrictions mean it cannot be
1683 c_ret
= crypt (c_key
, c_salt
);
1687 ret
= scm_from_locale_string (c_ret
);
1692 #endif /* HAVE_CRYPT */
1695 SCM_DEFINE (scm_chroot
, "chroot", 1, 0, 0,
1697 "Change the root directory to that specified in @var{path}.\n"
1698 "This directory will be used for path names beginning with\n"
1699 "@file{/}. The root directory is inherited by all children\n"
1700 "of the current process. Only the superuser may change the\n"
1702 #define FUNC_NAME s_scm_chroot
1706 WITH_STRING (path
, c_path
,
1707 rv
= chroot (c_path
));
1710 return SCM_UNSPECIFIED
;
1713 #endif /* HAVE_CHROOT */
1717 /* Wrapper function to supplying `getlogin()' under Windows. */
1718 static char * getlogin (void)
1720 static char user
[256];
1721 static unsigned long len
= 256;
1723 if (!GetUserName (user
, &len
))
1727 #endif /* __MINGW32__ */
1730 #if defined (HAVE_GETLOGIN) || defined (__MINGW32__)
1731 SCM_DEFINE (scm_getlogin
, "getlogin", 0, 0, 0,
1733 "Return a string containing the name of the user logged in on\n"
1734 "the controlling terminal of the process, or @code{#f} if this\n"
1735 "information cannot be obtained.")
1736 #define FUNC_NAME s_scm_getlogin
1743 return scm_from_locale_string (p
);
1746 #endif /* HAVE_GETLOGIN */
1748 #if HAVE_GETPRIORITY
1749 SCM_DEFINE (scm_getpriority
, "getpriority", 2, 0, 0,
1750 (SCM which
, SCM who
),
1751 "Return the scheduling priority of the process, process group\n"
1752 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1753 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1754 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1755 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1756 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1757 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1758 "denotes the current process, process group, or user. Return\n"
1759 "the highest priority (lowest numerical value) of any of the\n"
1760 "specified processes.")
1761 #define FUNC_NAME s_scm_getpriority
1763 int cwhich
, cwho
, ret
;
1765 cwhich
= scm_to_int (which
);
1766 cwho
= scm_to_int (who
);
1768 /* We have to clear errno and examine it later, because -1 is a
1769 legal return value for getpriority(). */
1771 ret
= getpriority (cwhich
, cwho
);
1774 return scm_from_int (ret
);
1777 #endif /* HAVE_GETPRIORITY */
1779 #if HAVE_SETPRIORITY
1780 SCM_DEFINE (scm_setpriority
, "setpriority", 3, 0, 0,
1781 (SCM which
, SCM who
, SCM prio
),
1782 "Set the scheduling priority of the process, process group\n"
1783 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1784 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1785 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1786 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1787 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1788 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1789 "denotes the current process, process group, or user.\n"
1790 "@var{prio} is a value in the range -20 and 20, the default\n"
1791 "priority is 0; lower priorities cause more favorable\n"
1792 "scheduling. Sets the priority of all of the specified\n"
1793 "processes. Only the super-user may lower priorities.\n"
1794 "The return value is not specified.")
1795 #define FUNC_NAME s_scm_setpriority
1797 int cwhich
, cwho
, cprio
;
1799 cwhich
= scm_to_int (which
);
1800 cwho
= scm_to_int (who
);
1801 cprio
= scm_to_int (prio
);
1803 if (setpriority (cwhich
, cwho
, cprio
) == -1)
1805 return SCM_UNSPECIFIED
;
1808 #endif /* HAVE_SETPRIORITY */
1810 #ifdef HAVE_SCHED_GETAFFINITY
1813 cpu_set_to_bitvector (const cpu_set_t
*cs
)
1818 bv
= scm_c_make_bitvector (sizeof (*cs
), SCM_BOOL_F
);
1820 for (cpu
= 0; cpu
< sizeof (*cs
); cpu
++)
1822 if (CPU_ISSET (cpu
, cs
))
1823 /* XXX: This is inefficient but avoids code duplication. */
1824 scm_c_bitvector_set_x (bv
, cpu
, SCM_BOOL_T
);
1830 SCM_DEFINE (scm_getaffinity
, "getaffinity", 1, 0, 0,
1832 "Return a bitvector representing the CPU affinity mask for\n"
1833 "process @var{pid}. Each CPU the process has affinity with\n"
1834 "has its corresponding bit set in the returned bitvector.\n"
1835 "The number of bits set is a good estimate of how many CPUs\n"
1836 "Guile can use without stepping on other processes' toes.\n\n"
1837 "Currently this procedure is only defined on GNU variants\n"
1838 "(@pxref{CPU Affinity, @code{sched_getaffinity},, libc, The\n"
1839 "GNU C Library Reference Manual}).\n")
1840 #define FUNC_NAME s_scm_getaffinity
1846 err
= sched_getaffinity (scm_to_int (pid
), sizeof (cs
), &cs
);
1850 return cpu_set_to_bitvector (&cs
);
1854 #endif /* HAVE_SCHED_GETAFFINITY */
1856 #ifdef HAVE_SCHED_SETAFFINITY
1858 SCM_DEFINE (scm_setaffinity
, "setaffinity", 2, 0, 0,
1859 (SCM pid
, SCM mask
),
1860 "Install the CPU affinity mask @var{mask}, a bitvector, for\n"
1861 "the process or thread with ID @var{pid}. The return value\n"
1862 "is unspecified.\n\n"
1863 "Currently this procedure is only defined on GNU variants\n"
1864 "(@pxref{CPU Affinity, @code{sched_setaffinity},, libc, The\n"
1865 "GNU C Library Reference Manual}).\n")
1866 #define FUNC_NAME s_scm_setaffinity
1869 scm_t_array_handle handle
;
1870 const scm_t_uint32
*c_mask
;
1871 size_t len
, off
, cpu
;
1875 c_mask
= scm_bitvector_elements (mask
, &handle
, &off
, &len
, &inc
);
1878 for (cpu
= 0; cpu
< len
; cpu
++)
1882 idx
= cpu
* inc
+ off
;
1883 if (c_mask
[idx
/ 32] & (1UL << (idx
% 32)))
1887 err
= sched_setaffinity (scm_to_int (pid
), sizeof (cs
), &cs
);
1891 return SCM_UNSPECIFIED
;
1895 #endif /* HAVE_SCHED_SETAFFINITY */
1899 SCM_DEFINE (scm_getpass
, "getpass", 1, 0, 0,
1901 "Display @var{prompt} to the standard error output and read\n"
1902 "a password from @file{/dev/tty}. If this file is not\n"
1903 "accessible, it reads from standard input. The password may be\n"
1904 "up to 127 characters in length. Additional characters and the\n"
1905 "terminating newline character are discarded. While reading\n"
1906 "the password, echoing and the generation of signals by special\n"
1907 "characters is disabled.")
1908 #define FUNC_NAME s_scm_getpass
1913 SCM_VALIDATE_STRING (1, prompt
);
1915 WITH_STRING (prompt
, c_prompt
,
1916 p
= getpass(c_prompt
));
1917 passwd
= scm_from_locale_string (p
);
1919 /* Clear out the password in the static buffer. */
1920 memset (p
, 0, strlen (p
));
1925 #endif /* HAVE_GETPASS */
1927 SCM_DEFINE (scm_flock
, "flock", 2, 0, 0,
1928 (SCM file
, SCM operation
),
1929 "Apply or remove an advisory lock on an open file.\n"
1930 "@var{operation} specifies the action to be done:\n"
1933 "Shared lock. More than one process may hold a shared lock\n"
1934 "for a given file at a given time.\n"
1937 "Exclusive lock. Only one process may hold an exclusive lock\n"
1938 "for a given file at a given time.\n"
1941 "Unlock the file.\n"
1944 "Don't block when locking. This is combined with one of the\n"
1945 "other operations using @code{logior}. If @code{flock} would\n"
1946 "block an @code{EWOULDBLOCK} error is thrown.\n"
1949 "The return value is not specified. @var{file} may be an open\n"
1950 "file descriptor or an open file descriptor port.\n"
1952 "Note that @code{flock} does not lock files across NFS.")
1953 #define FUNC_NAME s_scm_flock
1957 if (scm_is_integer (file
))
1958 fdes
= scm_to_int (file
);
1961 SCM_VALIDATE_OPFPORT (2, file
);
1963 fdes
= SCM_FPORT_FDES (file
);
1965 if (flock (fdes
, scm_to_int (operation
)) == -1)
1967 return SCM_UNSPECIFIED
;
1971 #if HAVE_SETHOSTNAME
1972 SCM_DEFINE (scm_sethostname
, "sethostname", 1, 0, 0,
1974 "Set the host name of the current processor to @var{name}. May\n"
1975 "only be used by the superuser. The return value is not\n"
1977 #define FUNC_NAME s_scm_sethostname
1981 WITH_STRING (name
, c_name
,
1982 rv
= sethostname (c_name
, strlen(c_name
)));
1985 return SCM_UNSPECIFIED
;
1988 #endif /* HAVE_SETHOSTNAME */
1991 #if HAVE_GETHOSTNAME
1992 SCM_DEFINE (scm_gethostname
, "gethostname", 0, 0, 0,
1994 "Return the host name of the current processor.")
1995 #define FUNC_NAME s_scm_gethostname
1997 #ifdef MAXHOSTNAMELEN
1999 /* Various systems define MAXHOSTNAMELEN (including Solaris in fact).
2000 * On GNU/Linux this doesn't include the terminating '\0', hence "+ 1". */
2001 const int len
= MAXHOSTNAMELEN
+ 1;
2002 char *const p
= scm_malloc (len
);
2003 const int res
= gethostname (p
, len
);
2005 scm_dynwind_begin (0);
2006 scm_dynwind_unwind_handler (free
, p
, 0);
2010 /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
2011 * large enough. SUSv2 specifies 255 maximum too, apparently. */
2016 # if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX)
2018 /* POSIX specifies the HOST_NAME_MAX system parameter for the max size,
2019 * which may reflect a particular kernel configuration.
2020 * Must watch out for this existing but giving -1, as happens for instance
2021 * in gnu/linux glibc 2.3.2. */
2023 const long int n
= sysconf (_SC_HOST_NAME_MAX
);
2030 p
= scm_malloc (len
);
2032 scm_dynwind_begin (0);
2033 scm_dynwind_unwind_handler (free
, p
, 0);
2035 res
= gethostname (p
, len
);
2036 while (res
== -1 && errno
== ENAMETOOLONG
)
2040 /* scm_realloc may throw an exception. */
2041 p
= scm_realloc (p
, len
);
2042 res
= gethostname (p
, len
);
2049 const int save_errno
= errno
;
2051 /* No guile exceptions can occur before we have freed p's memory. */
2060 /* scm_from_locale_string may throw an exception. */
2061 const SCM name
= scm_from_locale_string (p
);
2063 /* No guile exceptions can occur before we have freed p's memory. */
2071 #endif /* HAVE_GETHOSTNAME */
2077 scm_add_feature ("posix");
2079 scm_add_feature ("EIDs");
2082 scm_c_define ("WAIT_ANY", scm_from_int (WAIT_ANY
));
2085 scm_c_define ("WAIT_MYPGRP", scm_from_int (WAIT_MYPGRP
));
2088 scm_c_define ("WNOHANG", scm_from_int (WNOHANG
));
2091 scm_c_define ("WUNTRACED", scm_from_int (WUNTRACED
));
2095 scm_c_define ("LC_COLLATE", scm_from_int (LC_COLLATE
));
2098 scm_c_define ("LC_CTYPE", scm_from_int (LC_CTYPE
));
2101 scm_c_define ("LC_MONETARY", scm_from_int (LC_MONETARY
));
2104 scm_c_define ("LC_NUMERIC", scm_from_int (LC_NUMERIC
));
2107 scm_c_define ("LC_TIME", scm_from_int (LC_TIME
));
2110 scm_c_define ("LC_MESSAGES", scm_from_int (LC_MESSAGES
));
2113 scm_c_define ("LC_ALL", scm_from_int (LC_ALL
));
2116 scm_c_define ("LC_PAPER", scm_from_int (LC_PAPER
));
2119 scm_c_define ("LC_NAME", scm_from_int (LC_NAME
));
2122 scm_c_define ("LC_ADDRESS", scm_from_int (LC_ADDRESS
));
2125 scm_c_define ("LC_TELEPHONE", scm_from_int (LC_TELEPHONE
));
2127 #ifdef LC_MEASUREMENT
2128 scm_c_define ("LC_MEASUREMENT", scm_from_int (LC_MEASUREMENT
));
2130 #ifdef LC_IDENTIFICATION
2131 scm_c_define ("LC_IDENTIFICATION", scm_from_int (LC_IDENTIFICATION
));
2134 scm_c_define ("PIPE_BUF", scm_from_long (PIPE_BUF
));
2138 scm_c_define ("PRIO_PROCESS", scm_from_int (PRIO_PROCESS
));
2141 scm_c_define ("PRIO_PGRP", scm_from_int (PRIO_PGRP
));
2144 scm_c_define ("PRIO_USER", scm_from_int (PRIO_USER
));
2148 scm_c_define ("LOCK_SH", scm_from_int (LOCK_SH
));
2151 scm_c_define ("LOCK_EX", scm_from_int (LOCK_EX
));
2154 scm_c_define ("LOCK_UN", scm_from_int (LOCK_UN
));
2157 scm_c_define ("LOCK_NB", scm_from_int (LOCK_NB
));
2160 #include "libguile/cpp-SIG.c"
2161 #include "libguile/posix.x"