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