Update Gnulib to v0.0-6827-g39c3009; use the `dirfd' module.
[bpt/guile.git] / libguile / posix.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 * 02110-1301 USA
17 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <uniconv.h>
29
30 #ifdef HAVE_SCHED_H
31 # include <sched.h>
32 #endif
33
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"
44
45 #include "libguile/validate.h"
46 #include "libguile/posix.h"
47 #include "libguile/gettext.h"
48 #include "libguile/threads.h"
49 \f
50
51 #ifdef HAVE_STRING_H
52 #include <string.h>
53 #endif
54 #ifdef TIME_WITH_SYS_TIME
55 # include <sys/time.h>
56 # include <time.h>
57 #else
58 # if HAVE_SYS_TIME_H
59 # include <sys/time.h>
60 # else
61 # include <time.h>
62 # endif
63 #endif
64
65 #ifdef HAVE_UNISTD_H
66 #include <unistd.h>
67 #else
68 #ifndef ttyname
69 extern char *ttyname();
70 #endif
71 #endif
72
73 #ifdef LIBC_H_WITH_UNISTD_H
74 #include <libc.h>
75 #endif
76
77 #include <sys/types.h>
78 #include <sys/stat.h>
79 #include <fcntl.h>
80
81 #ifdef HAVE_PWD_H
82 #include <pwd.h>
83 #endif
84 #ifdef HAVE_IO_H
85 #include <io.h>
86 #endif
87 #ifdef HAVE_WINSOCK2_H
88 #include <winsock2.h>
89 #endif
90
91 #ifdef __MINGW32__
92 /* Some defines for Windows here. */
93 # include <process.h>
94 # define pipe(fd) _pipe (fd, 256, O_BINARY)
95 #endif /* __MINGW32__ */
96
97 #if HAVE_SYS_WAIT_H
98 # include <sys/wait.h>
99 #endif
100 #ifndef WEXITSTATUS
101 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
102 #endif
103 #ifndef WIFEXITED
104 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
105 #endif
106
107 #include <signal.h>
108
109 #ifdef HAVE_GRP_H
110 #include <grp.h>
111 #endif
112 #ifdef HAVE_SYS_UTSNAME_H
113 #include <sys/utsname.h>
114 #endif
115
116 #ifdef HAVE_SETLOCALE
117 #include <locale.h>
118 #endif
119
120 #if (defined HAVE_NEWLOCALE) && (defined HAVE_STRCOLL_L)
121 # define USE_GNU_LOCALE_API
122 #endif
123
124 #if (defined USE_GNU_LOCALE_API) && (defined HAVE_XLOCALE_H)
125 # include <xlocale.h>
126 #endif
127
128 #ifdef HAVE_CRYPT_H
129 # include <crypt.h>
130 #endif
131
132 #ifdef HAVE_NETDB_H
133 #include <netdb.h> /* for MAXHOSTNAMELEN on Solaris */
134 #endif
135
136 #ifdef HAVE_SYS_PARAM_H
137 #include <sys/param.h> /* for MAXHOSTNAMELEN */
138 #endif
139
140 #if HAVE_SYS_RESOURCE_H
141 # include <sys/resource.h>
142 #endif
143
144 #include <sys/file.h> /* from Gnulib */
145
146 /* Some Unix systems don't define these. CPP hair is dangerous, but
147 this seems safe enough... */
148 #ifndef R_OK
149 #define R_OK 4
150 #endif
151
152 #ifndef W_OK
153 #define W_OK 2
154 #endif
155
156 #ifndef X_OK
157 #define X_OK 1
158 #endif
159
160 #ifndef F_OK
161 #define F_OK 0
162 #endif
163
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);
168 #endif
169
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
175 #endif
176
177 #ifdef HAVE_SYS_UTIME_H
178 #include <sys/utime.h>
179 #endif
180
181 #ifdef HAVE_UTIME_H
182 #include <utime.h>
183 #endif
184
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.
188
189 FIXME: Maybe should undef _POSIX_SOURCE after it's done its job.
190
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
193 imagine. */
194
195
196
197
198 \f
199
200 /* Two often used patterns
201 */
202
203 #define WITH_STRING(str,cstr,code) \
204 do { \
205 char *cstr = scm_to_locale_string (str); \
206 code; \
207 free (cstr); \
208 } while (0)
209
210 #define STRING_SYSCALL(str,cstr,code) \
211 do { \
212 int eno; \
213 char *cstr = scm_to_locale_string (str); \
214 SCM_SYSCALL (code); \
215 eno = errno; free (cstr); errno = eno; \
216 } while (0)
217
218
219 \f
220 SCM_SYMBOL (sym_read_pipe, "read pipe");
221 SCM_SYMBOL (sym_write_pipe, "write pipe");
222
223 SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0,
224 (),
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"
232 "\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
239 {
240 int fd[2], rv;
241 SCM p_rd, p_wt;
242
243 rv = pipe (fd);
244 if (rv)
245 SCM_SYSERROR;
246
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);
250 }
251 #undef FUNC_NAME
252
253
254 #ifdef HAVE_GETGROUPS
255 SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0,
256 (),
257 "Return a vector of integers representing the current\n"
258 "supplementary group IDs.")
259 #define FUNC_NAME s_scm_getgroups
260 {
261 SCM result;
262 int ngroups;
263 size_t size;
264 GETGROUPS_T *groups;
265
266 ngroups = getgroups (0, NULL);
267 if (ngroups <= 0)
268 SCM_SYSERROR;
269
270 size = ngroups * sizeof (GETGROUPS_T);
271 groups = scm_malloc (size);
272 ngroups = getgroups (ngroups, groups);
273
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]));
277
278 free (groups);
279 return result;
280 }
281 #undef FUNC_NAME
282 #endif
283
284 #ifdef HAVE_SETGROUPS
285 SCM_DEFINE (scm_setgroups, "setgroups", 1, 0, 0,
286 (SCM group_vec),
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"
289 "unspecified.\n"
290 "\n"
291 "Generally only the superuser can set the process group IDs.")
292 #define FUNC_NAME s_scm_setgroups
293 {
294 size_t ngroups;
295 size_t size;
296 size_t i;
297 int result;
298 int save_errno;
299 GETGROUPS_T *groups;
300
301 SCM_VALIDATE_VECTOR (SCM_ARG1, group_vec);
302
303 ngroups = SCM_SIMPLE_VECTOR_LENGTH (group_vec);
304
305 /* validate before allocating, so we don't have to worry about leaks */
306 for (i = 0; i < ngroups; i++)
307 {
308 unsigned long ulong_gid;
309 GETGROUPS_T gid;
310 SCM_VALIDATE_ULONG_COPY (1, SCM_SIMPLE_VECTOR_REF (group_vec, i),
311 ulong_gid);
312 gid = ulong_gid;
313 if (gid != ulong_gid)
314 SCM_OUT_OF_RANGE (1, SCM_SIMPLE_VECTOR_REF (group_vec, i));
315 }
316
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));
323
324 result = setgroups (ngroups, groups);
325 save_errno = errno; /* don't let free() touch errno */
326 free (groups);
327 errno = save_errno;
328 if (result < 0)
329 SCM_SYSERROR;
330 return SCM_UNSPECIFIED;
331 }
332 #undef FUNC_NAME
333 #endif
334
335 #ifdef HAVE_GETPWENT
336 SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0,
337 (SCM user),
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
342 {
343 struct passwd *entry;
344
345 SCM result = scm_c_make_vector (7, SCM_UNSPECIFIED);
346 if (SCM_UNBNDP (user) || scm_is_false (user))
347 {
348 SCM_SYSCALL (entry = getpwent ());
349 if (! entry)
350 {
351 return SCM_BOOL_F;
352 }
353 }
354 else if (scm_is_integer (user))
355 {
356 entry = getpwuid (scm_to_int (user));
357 }
358 else
359 {
360 WITH_STRING (user, c_user,
361 entry = getpwnam (c_user));
362 }
363 if (!entry)
364 SCM_MISC_ERROR ("entry not found", SCM_EOL);
365
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));
371 if (!entry->pw_dir)
372 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (""));
373 else
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 (""));
377 else
378 SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (entry->pw_shell));
379 return result;
380 }
381 #undef FUNC_NAME
382 #endif /* HAVE_GETPWENT */
383
384
385 #ifdef HAVE_SETPWENT
386 SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0,
387 (SCM arg),
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
392 {
393 if (SCM_UNBNDP (arg) || scm_is_false (arg))
394 endpwent ();
395 else
396 setpwent ();
397 return SCM_UNSPECIFIED;
398 }
399 #undef FUNC_NAME
400 #endif
401
402
403 #ifdef HAVE_GETGRENT
404 /* Combines getgrgid and getgrnam. */
405 SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0,
406 (SCM name),
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
411 {
412 struct group *entry;
413 SCM result = scm_c_make_vector (4, SCM_UNSPECIFIED);
414
415 if (SCM_UNBNDP (name) || scm_is_false (name))
416 {
417 SCM_SYSCALL (entry = getgrent ());
418 if (! entry)
419 {
420 return SCM_BOOL_F;
421 }
422 }
423 else if (scm_is_integer (name))
424 SCM_SYSCALL (entry = getgrgid (scm_to_int (name)));
425 else
426 STRING_SYSCALL (name, c_name,
427 entry = getgrnam (c_name));
428 if (!entry)
429 SCM_SYSERROR;
430
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));
435 return result;
436 }
437 #undef FUNC_NAME
438
439
440
441 SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0,
442 (SCM arg),
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
447 {
448 if (SCM_UNBNDP (arg) || scm_is_false (arg))
449 endgrent ();
450 else
451 setgrent ();
452 return SCM_UNSPECIFIED;
453 }
454 #undef FUNC_NAME
455 #endif /* HAVE_GETGRENT */
456
457
458 #ifdef HAVE_GETRLIMIT
459 #ifdef RLIMIT_AS
460 SCM_SYMBOL (sym_as, "as");
461 #endif
462 #ifdef RLIMIT_CORE
463 SCM_SYMBOL (sym_core, "core");
464 #endif
465 #ifdef RLIMIT_CPU
466 SCM_SYMBOL (sym_cpu, "cpu");
467 #endif
468 #ifdef RLIMIT_DATA
469 SCM_SYMBOL (sym_data, "data");
470 #endif
471 #ifdef RLIMIT_FSIZE
472 SCM_SYMBOL (sym_fsize, "fsize");
473 #endif
474 #ifdef RLIMIT_MEMLOCK
475 SCM_SYMBOL (sym_memlock, "memlock");
476 #endif
477 #ifdef RLIMIT_MSGQUEUE
478 SCM_SYMBOL (sym_msgqueue, "msgqueue");
479 #endif
480 #ifdef RLIMIT_NICE
481 SCM_SYMBOL (sym_nice, "nice");
482 #endif
483 #ifdef RLIMIT_NOFILE
484 SCM_SYMBOL (sym_nofile, "nofile");
485 #endif
486 #ifdef RLIMIT_NPROC
487 SCM_SYMBOL (sym_nproc, "nproc");
488 #endif
489 #ifdef RLIMIT_RSS
490 SCM_SYMBOL (sym_rss, "rss");
491 #endif
492 #ifdef RLIMIT_RTPRIO
493 SCM_SYMBOL (sym_rtprio, "rtprio");
494 #endif
495 #ifdef RLIMIT_RTPRIO
496 SCM_SYMBOL (sym_rttime, "rttime");
497 #endif
498 #ifdef RLIMIT_SIGPENDING
499 SCM_SYMBOL (sym_sigpending, "sigpending");
500 #endif
501 #ifdef RLIMIT_STACK
502 SCM_SYMBOL (sym_stack, "stack");
503 #endif
504
505 static int
506 scm_to_resource (SCM s, const char *func, int pos)
507 {
508 if (scm_is_number (s))
509 return scm_to_int (s);
510
511 SCM_ASSERT_TYPE (scm_is_symbol (s), s, pos, func, "symbol");
512
513 #ifdef RLIMIT_AS
514 if (scm_is_eq (s, sym_as))
515 return RLIMIT_AS;
516 #endif
517 #ifdef RLIMIT_CORE
518 if (scm_is_eq (s, sym_core))
519 return RLIMIT_CORE;
520 #endif
521 #ifdef RLIMIT_CPU
522 if (scm_is_eq (s, sym_cpu))
523 return RLIMIT_CPU;
524 #endif
525 #ifdef RLIMIT_DATA
526 if (scm_is_eq (s, sym_data))
527 return RLIMIT_DATA;
528 #endif
529 #ifdef RLIMIT_FSIZE
530 if (scm_is_eq (s, sym_fsize))
531 return RLIMIT_FSIZE;
532 #endif
533 #ifdef RLIMIT_MEMLOCK
534 if (scm_is_eq (s, sym_memlock))
535 return RLIMIT_MEMLOCK;
536 #endif
537 #ifdef RLIMIT_MSGQUEUE
538 if (scm_is_eq (s, sym_msgqueue))
539 return RLIMIT_MSGQUEUE;
540 #endif
541 #ifdef RLIMIT_NICE
542 if (scm_is_eq (s, sym_nice))
543 return RLIMIT_NICE;
544 #endif
545 #ifdef RLIMIT_NOFILE
546 if (scm_is_eq (s, sym_nofile))
547 return RLIMIT_NOFILE;
548 #endif
549 #ifdef RLIMIT_NPROC
550 if (scm_is_eq (s, sym_nproc))
551 return RLIMIT_NPROC;
552 #endif
553 #ifdef RLIMIT_RSS
554 if (scm_is_eq (s, sym_rss))
555 return RLIMIT_RSS;
556 #endif
557 #ifdef RLIMIT_RTPRIO
558 if (scm_is_eq (s, sym_rtprio))
559 return RLIMIT_RTPRIO;
560 #endif
561 #ifdef RLIMIT_RTPRIO
562 if (scm_is_eq (s, sym_rttime))
563 return RLIMIT_RTPRIO;
564 #endif
565 #ifdef RLIMIT_SIGPENDING
566 if (scm_is_eq (s, sym_sigpending))
567 return RLIMIT_SIGPENDING;
568 #endif
569 #ifdef RLIMIT_STACK
570 if (scm_is_eq (s, sym_stack))
571 return RLIMIT_STACK;
572 #endif
573
574 scm_misc_error (func, "invalid rlimit resource ~A", scm_list_1 (s));
575 return 0;
576 }
577
578 SCM_DEFINE (scm_getrlimit, "getrlimit", 1, 0, 0,
579 (SCM resource),
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
586 {
587 int iresource;
588 struct rlimit lim = { 0, 0 };
589
590 iresource = scm_to_resource (resource, FUNC_NAME, 1);
591
592 if (getrlimit (iresource, &lim) != 0)
593 scm_syserror (FUNC_NAME);
594
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)));
599 }
600 #undef FUNC_NAME
601
602
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
612 {
613 int iresource;
614 struct rlimit lim = { 0, 0 };
615
616 iresource = scm_to_resource (resource, FUNC_NAME, 1);
617
618 lim.rlim_cur = scm_is_false (soft) ? RLIM_INFINITY : scm_to_long (soft);
619 lim.rlim_max = scm_is_false (hard) ? RLIM_INFINITY : scm_to_long (hard);
620
621 if (setrlimit (iresource, &lim) != 0)
622 scm_syserror (FUNC_NAME);
623
624 return SCM_UNSPECIFIED;
625 }
626 #undef FUNC_NAME
627 #endif /* HAVE_SETRLIMIT */
628 #endif /* HAVE_GETRLIMIT */
629
630
631 SCM_DEFINE (scm_kill, "kill", 2, 0, 0,
632 (SCM pid, SCM sig),
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"
635 "@table @r\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"
645 "user ID.\n"
646 "@end table\n\n"
647 "@var{sig} should be specified using a variable corresponding to\n"
648 "the Unix symbolic name, e.g.,\n\n"
649 "@defvar SIGHUP\n"
650 "Hang-up signal.\n"
651 "@end defvar\n\n"
652 "@defvar SIGINT\n"
653 "Interrupt signal.\n"
654 "@end defvar")
655 #define FUNC_NAME s_scm_kill
656 {
657 /* Signal values are interned in scm_init_posix(). */
658 #ifdef HAVE_KILL
659 if (kill (scm_to_int (pid), scm_to_int (sig)) != 0)
660 SCM_SYSERROR;
661 #else
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,
664 otherwise bomb. */
665 if (scm_to_int (pid) == getpid ())
666 {
667 if (raise (scm_to_int (sig)) != 0)
668 {
669 err:
670 SCM_SYSERROR;
671 }
672 else
673 {
674 errno = ENOSYS;
675 goto err;
676 }
677 }
678 #endif
679 return SCM_UNSPECIFIED;
680 }
681 #undef FUNC_NAME
682
683 #ifdef HAVE_WAITPID
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"
691 "@table @r\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"
698 "group.\n"
699 "@item @var{pid} less than -1\n"
700 "Request status information for any child process whose process group ID\n"
701 "is -@var{PID}.\n"
702 "@end table\n\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"
705 "@defvar WNOHANG\n"
706 "Return immediately even if there are no child processes to be collected.\n"
707 "@end defvar\n\n"
708 "@defvar WUNTRACED\n"
709 "Report status information for stopped processes as well as terminated\n"
710 "processes.\n"
711 "@end defvar\n\n"
712 "The return value is a pair containing:\n\n"
713 "@enumerate\n"
714 "@item\n"
715 "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
716 "specified and no process was collected.\n"
717 "@item\n"
718 "The integer status value.\n"
719 "@end enumerate")
720 #define FUNC_NAME s_scm_waitpid
721 {
722 int i;
723 int status;
724 int ioptions;
725 if (SCM_UNBNDP (options))
726 ioptions = 0;
727 else
728 {
729 /* Flags are interned in scm_init_posix. */
730 ioptions = scm_to_int (options);
731 }
732 SCM_SYSCALL (i = waitpid (scm_to_int (pid), &status, ioptions));
733 if (i == -1)
734 SCM_SYSERROR;
735 return scm_cons (scm_from_int (i), scm_from_int (status));
736 }
737 #undef FUNC_NAME
738 #endif /* HAVE_WAITPID */
739
740 #ifndef __MINGW32__
741 SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0,
742 (SCM status),
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
747 {
748 int lstatus;
749
750 /* On Ultrix, the WIF... macros assume their argument is an lvalue;
751 go figure. */
752 lstatus = scm_to_int (status);
753 if (WIFEXITED (lstatus))
754 return (scm_from_int (WEXITSTATUS (lstatus)));
755 else
756 return SCM_BOOL_F;
757 }
758 #undef FUNC_NAME
759
760 SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0,
761 (SCM status),
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
765 {
766 int lstatus;
767
768 lstatus = scm_to_int (status);
769 if (WIFSIGNALED (lstatus))
770 return scm_from_int (WTERMSIG (lstatus));
771 else
772 return SCM_BOOL_F;
773 }
774 #undef FUNC_NAME
775
776 SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0,
777 (SCM status),
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
781 {
782 int lstatus;
783
784 lstatus = scm_to_int (status);
785 if (WIFSTOPPED (lstatus))
786 return scm_from_int (WSTOPSIG (lstatus));
787 else
788 return SCM_BOOL_F;
789 }
790 #undef FUNC_NAME
791 #endif /* __MINGW32__ */
792
793 #ifdef HAVE_GETPPID
794 SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0,
795 (),
796 "Return an integer representing the process ID of the parent\n"
797 "process.")
798 #define FUNC_NAME s_scm_getppid
799 {
800 return scm_from_int (getppid ());
801 }
802 #undef FUNC_NAME
803 #endif /* HAVE_GETPPID */
804
805
806 #ifndef __MINGW32__
807 SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0,
808 (),
809 "Return an integer representing the current real user ID.")
810 #define FUNC_NAME s_scm_getuid
811 {
812 return scm_from_int (getuid ());
813 }
814 #undef FUNC_NAME
815
816
817
818 SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0,
819 (),
820 "Return an integer representing the current real group ID.")
821 #define FUNC_NAME s_scm_getgid
822 {
823 return scm_from_int (getgid ());
824 }
825 #undef FUNC_NAME
826
827
828
829 SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0,
830 (),
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
836 {
837 #ifdef HAVE_GETEUID
838 return scm_from_int (geteuid ());
839 #else
840 return scm_from_int (getuid ());
841 #endif
842 }
843 #undef FUNC_NAME
844
845
846 SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0,
847 (),
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
853 {
854 #ifdef HAVE_GETEUID
855 return scm_from_int (getegid ());
856 #else
857 return scm_from_int (getgid ());
858 #endif
859 }
860 #undef FUNC_NAME
861
862
863 SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0,
864 (SCM id),
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
869 {
870 if (setuid (scm_to_int (id)) != 0)
871 SCM_SYSERROR;
872 return SCM_UNSPECIFIED;
873 }
874 #undef FUNC_NAME
875
876 SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0,
877 (SCM id),
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
882 {
883 if (setgid (scm_to_int (id)) != 0)
884 SCM_SYSERROR;
885 return SCM_UNSPECIFIED;
886 }
887 #undef FUNC_NAME
888
889 SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0,
890 (SCM id),
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
897 {
898 int rv;
899
900 #ifdef HAVE_SETEUID
901 rv = seteuid (scm_to_int (id));
902 #else
903 rv = setuid (scm_to_int (id));
904 #endif
905 if (rv != 0)
906 SCM_SYSERROR;
907 return SCM_UNSPECIFIED;
908 }
909 #undef FUNC_NAME
910 #endif /* __MINGW32__ */
911
912
913 #ifdef HAVE_SETEGID
914 SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0,
915 (SCM id),
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
922 {
923 int rv;
924
925 #ifdef HAVE_SETEUID
926 rv = setegid (scm_to_int (id));
927 #else
928 rv = setgid (scm_to_int (id));
929 #endif
930 if (rv != 0)
931 SCM_SYSERROR;
932 return SCM_UNSPECIFIED;
933
934 }
935 #undef FUNC_NAME
936 #endif
937
938
939 #ifdef HAVE_GETPGRP
940 SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0,
941 (),
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
945 {
946 int (*fn)();
947 fn = (int (*) ()) getpgrp;
948 return scm_from_int (fn (0));
949 }
950 #undef FUNC_NAME
951 #endif /* HAVE_GETPGRP */
952
953
954 #ifdef HAVE_SETPGID
955 SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0,
956 (SCM pid, SCM pgid),
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"
959 "current process.\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
963 {
964 /* FIXME(?): may be known as setpgrp. */
965 if (setpgid (scm_to_int (pid), scm_to_int (pgid)) != 0)
966 SCM_SYSERROR;
967 return SCM_UNSPECIFIED;
968 }
969 #undef FUNC_NAME
970 #endif /* HAVE_SETPGID */
971
972 #ifdef HAVE_SETSID
973 SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0,
974 (),
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
980 {
981 pid_t sid = setsid ();
982 if (sid == -1)
983 SCM_SYSERROR;
984 return SCM_UNSPECIFIED;
985 }
986 #undef FUNC_NAME
987 #endif /* HAVE_SETSID */
988
989 #ifdef HAVE_GETSID
990 SCM_DEFINE (scm_getsid, "getsid", 1, 0, 0,
991 (SCM pid),
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
995 {
996 return scm_from_int (getsid (scm_to_int (pid)));
997 }
998 #undef FUNC_NAME
999 #endif /* HAVE_GETSID */
1000
1001
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
1005 easily.
1006
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. */
1009
1010 #ifdef HAVE_TTYNAME
1011 SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0,
1012 (SCM port),
1013 "Return a string with the name of the serial terminal device\n"
1014 "underlying @var{port}.")
1015 #define FUNC_NAME s_scm_ttyname
1016 {
1017 char *result;
1018 int fd, err;
1019 SCM ret = SCM_BOOL_F;
1020
1021 port = SCM_COERCE_OUTPORT (port);
1022 SCM_VALIDATE_OPPORT (1, port);
1023 if (!SCM_FPORTP (port))
1024 return SCM_BOOL_F;
1025 fd = SCM_FPORT_FDES (port);
1026
1027 scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
1028
1029 SCM_SYSCALL (result = ttyname (fd));
1030 err = errno;
1031 if (result != NULL)
1032 result = strdup (result);
1033
1034 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
1035
1036 if (!result)
1037 {
1038 errno = err;
1039 SCM_SYSERROR;
1040 }
1041 else
1042 ret = scm_take_locale_string (result);
1043
1044 return ret;
1045 }
1046 #undef FUNC_NAME
1047 #endif /* HAVE_TTYNAME */
1048
1049
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. */
1055 #ifdef HAVE_CTERMID
1056 SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0,
1057 (),
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
1061 {
1062 char buf[L_ctermid];
1063 char *result = ctermid (buf);
1064 if (*result == '\0')
1065 SCM_SYSERROR;
1066 return scm_from_locale_string (result);
1067 }
1068 #undef FUNC_NAME
1069 #endif /* HAVE_CTERMID */
1070
1071 #ifdef HAVE_TCGETPGRP
1072 SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0,
1073 (SCM port),
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"
1077 "\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"
1083 "foreground.")
1084 #define FUNC_NAME s_scm_tcgetpgrp
1085 {
1086 int fd;
1087 pid_t pgid;
1088
1089 port = SCM_COERCE_OUTPORT (port);
1090
1091 SCM_VALIDATE_OPFPORT (1, port);
1092 fd = SCM_FPORT_FDES (port);
1093 if ((pgid = tcgetpgrp (fd)) == -1)
1094 SCM_SYSERROR;
1095 return scm_from_int (pgid);
1096 }
1097 #undef FUNC_NAME
1098 #endif /* HAVE_TCGETPGRP */
1099
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
1109 {
1110 int fd;
1111
1112 port = SCM_COERCE_OUTPORT (port);
1113
1114 SCM_VALIDATE_OPFPORT (1, port);
1115 fd = SCM_FPORT_FDES (port);
1116 if (tcsetpgrp (fd, scm_to_int (pgid)) == -1)
1117 SCM_SYSERROR;
1118 return SCM_UNSPECIFIED;
1119 }
1120 #undef FUNC_NAME
1121 #endif /* HAVE_TCSETPGRP */
1122
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
1135 {
1136 char *exec_file;
1137 char **exec_argv;
1138
1139 scm_dynwind_begin (0);
1140
1141 exec_file = scm_to_locale_string (filename);
1142 scm_dynwind_free (exec_file);
1143
1144 exec_argv = scm_i_allocate_string_pointers (args);
1145
1146 execv (exec_file,
1147 #ifdef __MINGW32__
1148 /* extra "const" in mingw formals, provokes warning from gcc */
1149 (const char * const *)
1150 #endif
1151 exec_argv);
1152 SCM_SYSERROR;
1153
1154 /* not reached. */
1155 scm_dynwind_end ();
1156 return SCM_BOOL_F;
1157 }
1158 #undef FUNC_NAME
1159
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
1169 {
1170 char *exec_file;
1171 char **exec_argv;
1172
1173 scm_dynwind_begin (0);
1174
1175 exec_file = scm_to_locale_string (filename);
1176 scm_dynwind_free (exec_file);
1177
1178 exec_argv = scm_i_allocate_string_pointers (args);
1179
1180 execvp (exec_file,
1181 #ifdef __MINGW32__
1182 /* extra "const" in mingw formals, provokes warning from gcc */
1183 (const char * const *)
1184 #endif
1185 exec_argv);
1186 SCM_SYSERROR;
1187
1188 /* not reached. */
1189 scm_dynwind_end ();
1190 return SCM_BOOL_F;
1191 }
1192 #undef FUNC_NAME
1193
1194
1195 /* OPTIMIZE-ME: scm_execle doesn't need malloced copies of the environment
1196 list strings the way environ_list_to_c gives. */
1197
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
1206 {
1207 char **exec_argv;
1208 char **exec_env;
1209 char *exec_file;
1210
1211 scm_dynwind_begin (0);
1212
1213 exec_file = scm_to_locale_string (filename);
1214 scm_dynwind_free (exec_file);
1215
1216 exec_argv = scm_i_allocate_string_pointers (args);
1217 exec_env = scm_i_allocate_string_pointers (env);
1218
1219 execve (exec_file,
1220 #ifdef __MINGW32__
1221 /* extra "const" in mingw formals, provokes warning from gcc */
1222 (const char * const *)
1223 #endif
1224 exec_argv,
1225 #ifdef __MINGW32__
1226 /* extra "const" in mingw formals, provokes warning from gcc */
1227 (const char * const *)
1228 #endif
1229 exec_env);
1230 SCM_SYSERROR;
1231
1232 /* not reached. */
1233 scm_dynwind_end ();
1234 return SCM_BOOL_F;
1235 }
1236 #undef FUNC_NAME
1237
1238 #ifdef HAVE_FORK
1239 SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0,
1240 (),
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
1247 {
1248 int pid;
1249 pid = fork ();
1250 if (pid == -1)
1251 SCM_SYSERROR;
1252 return scm_from_int (pid);
1253 }
1254 #undef FUNC_NAME
1255 #endif /* HAVE_FORK */
1256
1257 #ifdef __MINGW32__
1258 # include "win32-uname.h"
1259 #endif
1260
1261 #if defined (HAVE_UNAME) || defined (__MINGW32__)
1262 SCM_DEFINE (scm_uname, "uname", 0, 0, 0,
1263 (),
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
1267 {
1268 struct utsname buf;
1269 SCM result = scm_c_make_vector (5, SCM_UNSPECIFIED);
1270 if (uname (&buf) < 0)
1271 SCM_SYSERROR;
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));
1277 /*
1278 a linux special?
1279 SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (buf.domainname));
1280 */
1281 return result;
1282 }
1283 #undef FUNC_NAME
1284 #endif /* HAVE_UNAME */
1285
1286 SCM_DEFINE (scm_environ, "environ", 0, 1, 0,
1287 (SCM env),
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
1296 {
1297 if (SCM_UNBNDP (env))
1298 return scm_makfromstrs (-1, environ);
1299 else
1300 {
1301 environ = scm_i_allocate_string_pointers (env);
1302 return SCM_UNSPECIFIED;
1303 }
1304 }
1305 #undef FUNC_NAME
1306
1307 #ifdef L_tmpnam
1308
1309 SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0,
1310 (),
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
1317 {
1318 char name[L_tmpnam];
1319 char *rv;
1320
1321 SCM_SYSCALL (rv = tmpnam (name));
1322 if (rv == NULL)
1323 /* not SCM_SYSERROR since errno probably not set. */
1324 SCM_MISC_ERROR ("tmpnam failed", SCM_EOL);
1325 return scm_from_locale_string (name);
1326 }
1327 #undef FUNC_NAME
1328
1329 #endif
1330
1331 SCM_DEFINE (scm_tmpfile, "tmpfile", 0, 0, 0,
1332 (void),
1333 "Return an input/output port to a unique temporary file\n"
1334 "named using the path prefix @code{P_tmpdir} defined in\n"
1335 "@file{stdio.h}.\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
1339 {
1340 FILE *rv;
1341 int fd;
1342
1343 if (! (rv = tmpfile ()))
1344 SCM_SYSERROR;
1345
1346 #ifndef __MINGW32__
1347 fd = dup (fileno (rv));
1348 fclose (rv);
1349 #else
1350 fd = fileno (rv);
1351 /* FIXME: leaking the file, it will never be closed! */
1352 #endif
1353
1354 return scm_fdes_to_port (fd, "w+", SCM_BOOL_F);
1355 }
1356 #undef FUNC_NAME
1357
1358 SCM_DEFINE (scm_utime, "utime", 1, 5, 0,
1359 (SCM pathname, SCM actime, SCM modtime, SCM actimens, SCM modtimens,
1360 SCM flags),
1361 "@code{utime} sets the access and modification times for the\n"
1362 "file named by @var{path}. If @var{actime} or @var{modtime} is\n"
1363 "not supplied, then the current time is used. @var{actime} and\n"
1364 "@var{modtime} must be integer time values as returned by the\n"
1365 "@code{current-time} procedure.\n\n"
1366 "The optional @var{actimens} and @var{modtimens} are nanoseconds\n"
1367 "to add @var{actime} and @var{modtime}. Nanosecond precision is\n"
1368 "only supported on some combinations of file systems and operating\n"
1369 "systems.\n"
1370 "@lisp\n"
1371 "(utime \"foo\" (- (current-time) 3600))\n"
1372 "@end lisp\n"
1373 "will set the access time to one hour in the past and the\n"
1374 "modification time to the current time.")
1375 #define FUNC_NAME s_scm_utime
1376 {
1377 int rv;
1378 time_t atim_sec, mtim_sec;
1379 long atim_nsec, mtim_nsec;
1380 int f;
1381
1382 if (SCM_UNBNDP (actime))
1383 {
1384 #ifdef HAVE_UTIMENSAT
1385 atim_sec = 0;
1386 atim_nsec = UTIME_NOW;
1387 #else
1388 SCM_SYSCALL (time (&atim_sec));
1389 atim_nsec = 0;
1390 #endif
1391 }
1392 else
1393 {
1394 atim_sec = SCM_NUM2ULONG (2, actime);
1395 if (SCM_UNBNDP (actimens))
1396 atim_nsec = 0;
1397 else
1398 atim_nsec = SCM_NUM2LONG (4, actimens);
1399 }
1400
1401 if (SCM_UNBNDP (modtime))
1402 {
1403 #ifdef HAVE_UTIMENSAT
1404 mtim_sec = 0;
1405 mtim_nsec = UTIME_NOW;
1406 #else
1407 SCM_SYSCALL (time (&mtim_sec));
1408 mtim_nsec = 0;
1409 #endif
1410 }
1411 else
1412 {
1413 mtim_sec = SCM_NUM2ULONG (3, modtime);
1414 if (SCM_UNBNDP (modtimens))
1415 mtim_nsec = 0;
1416 else
1417 mtim_nsec = SCM_NUM2LONG (5, modtimens);
1418 }
1419
1420 if (SCM_UNBNDP (flags))
1421 f = 0;
1422 else
1423 f = SCM_NUM2INT (6, flags);
1424
1425 #ifdef HAVE_UTIMENSAT
1426 {
1427 struct timespec times[2];
1428 times[0].tv_sec = atim_sec;
1429 times[0].tv_nsec = atim_nsec;
1430 times[1].tv_sec = mtim_sec;
1431 times[1].tv_nsec = mtim_nsec;
1432
1433 STRING_SYSCALL (pathname, c_pathname,
1434 rv = utimensat (AT_FDCWD, c_pathname, times, f));
1435 }
1436 #else
1437 {
1438 struct utimbuf utm;
1439 utm.actime = atim_sec;
1440 utm.modtime = mtim_sec;
1441
1442 STRING_SYSCALL (pathname, c_pathname,
1443 rv = utime (c_pathname, &utm));
1444 }
1445 #endif
1446
1447 if (rv != 0)
1448 SCM_SYSERROR;
1449 return SCM_UNSPECIFIED;
1450 }
1451 #undef FUNC_NAME
1452
1453 SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0,
1454 (),
1455 "Return an integer representing the current process ID.")
1456 #define FUNC_NAME s_scm_getpid
1457 {
1458 return scm_from_ulong (getpid ());
1459 }
1460 #undef FUNC_NAME
1461
1462 SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0,
1463 (SCM str),
1464 "Modifies the environment of the current process, which is\n"
1465 "also the default environment inherited by child processes.\n\n"
1466 "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n"
1467 "directly into the environment, replacing any existing environment string\n"
1468 "with\n"
1469 "name matching @code{NAME}. If @var{string} does not contain an equal\n"
1470 "sign, then any existing string with name matching @var{string} will\n"
1471 "be removed.\n\n"
1472 "The return value is unspecified.")
1473 #define FUNC_NAME s_scm_putenv
1474 {
1475 int rv;
1476 char *c_str = scm_to_locale_string (str);
1477
1478 /* Leave C_STR in the environment. */
1479
1480 /* Gnulib's `putenv' module honors the semantics described above. */
1481 rv = putenv (c_str);
1482 if (rv < 0)
1483 SCM_SYSERROR;
1484
1485 return SCM_UNSPECIFIED;
1486 }
1487 #undef FUNC_NAME
1488
1489 /* This mutex is used to serialize invocations of `setlocale ()' on non-GNU
1490 systems (i.e., systems where a reentrant locale API is not available). It
1491 is also acquired before calls to `nl_langinfo ()'. See `i18n.c' for
1492 details. */
1493 scm_i_pthread_mutex_t scm_i_locale_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
1494
1495 #ifdef HAVE_SETLOCALE
1496
1497 SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
1498 (SCM category, SCM locale),
1499 "If @var{locale} is omitted, return the current value of the\n"
1500 "specified locale category as a system-dependent string.\n"
1501 "@var{category} should be specified using the values\n"
1502 "@code{LC_COLLATE}, @code{LC_ALL} etc.\n"
1503 "\n"
1504 "Otherwise the specified locale category is set to the string\n"
1505 "@var{locale} and the new value is returned as a\n"
1506 "system-dependent string. If @var{locale} is an empty string,\n"
1507 "the locale will be set using environment variables.\n"
1508 "\n"
1509 "When the locale is changed, the character encoding of the new\n"
1510 "locale (UTF-8, ISO-8859-1, etc.) is used for the current\n"
1511 "input, output, and error ports\n")
1512 #define FUNC_NAME s_scm_setlocale
1513 {
1514 int c_category;
1515 char *clocale;
1516 char *rv;
1517 const char *enc;
1518
1519 scm_dynwind_begin (0);
1520
1521 if (SCM_UNBNDP (locale))
1522 {
1523 clocale = NULL;
1524 }
1525 else
1526 {
1527 clocale = scm_to_locale_string (locale);
1528 scm_dynwind_free (clocale);
1529 }
1530
1531 c_category = scm_i_to_lc_category (category, 1);
1532
1533 scm_i_pthread_mutex_lock (&scm_i_locale_mutex);
1534 rv = setlocale (c_category, clocale);
1535 scm_i_pthread_mutex_unlock (&scm_i_locale_mutex);
1536
1537 if (rv == NULL)
1538 {
1539 /* POSIX and C99 don't say anything about setlocale setting errno, so
1540 force a sensible value here. glibc leaves ENOENT, which would be
1541 fine, but it's not a documented feature. */
1542 errno = EINVAL;
1543 SCM_SYSERROR;
1544 }
1545
1546 enc = locale_charset ();
1547
1548 /* Set the default encoding for new ports. */
1549 scm_i_set_default_port_encoding (enc);
1550
1551 /* Set the encoding for the stdio ports. */
1552 scm_i_set_port_encoding_x (scm_current_input_port (), enc);
1553 scm_i_set_port_encoding_x (scm_current_output_port (), enc);
1554 scm_i_set_port_encoding_x (scm_current_error_port (), enc);
1555
1556 scm_dynwind_end ();
1557 return scm_from_locale_string (rv);
1558 }
1559 #undef FUNC_NAME
1560 #endif /* HAVE_SETLOCALE */
1561
1562 #ifdef HAVE_MKNOD
1563 SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
1564 (SCM path, SCM type, SCM perms, SCM dev),
1565 "Creates a new special file, such as a file corresponding to a device.\n"
1566 "@var{path} specifies the name of the file. @var{type} should\n"
1567 "be one of the following symbols:\n"
1568 "regular, directory, symlink, block-special, char-special,\n"
1569 "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
1570 "@var{dev} (an integer) specifies which device the special file refers\n"
1571 "to. Its exact interpretation depends on the kind of special file\n"
1572 "being created.\n\n"
1573 "E.g.,\n"
1574 "@lisp\n"
1575 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
1576 "@end lisp\n\n"
1577 "The return value is unspecified.")
1578 #define FUNC_NAME s_scm_mknod
1579 {
1580 int val;
1581 const char *p;
1582 int ctype = 0;
1583
1584 SCM_VALIDATE_STRING (1, path);
1585 SCM_VALIDATE_SYMBOL (2, type);
1586
1587 p = scm_i_symbol_chars (type);
1588 if (strcmp (p, "regular") == 0)
1589 ctype = S_IFREG;
1590 else if (strcmp (p, "directory") == 0)
1591 ctype = S_IFDIR;
1592 #ifdef S_IFLNK
1593 /* systems without symlinks probably don't have S_IFLNK defined */
1594 else if (strcmp (p, "symlink") == 0)
1595 ctype = S_IFLNK;
1596 #endif
1597 else if (strcmp (p, "block-special") == 0)
1598 ctype = S_IFBLK;
1599 else if (strcmp (p, "char-special") == 0)
1600 ctype = S_IFCHR;
1601 else if (strcmp (p, "fifo") == 0)
1602 ctype = S_IFIFO;
1603 #ifdef S_IFSOCK
1604 else if (strcmp (p, "socket") == 0)
1605 ctype = S_IFSOCK;
1606 #endif
1607 else
1608 SCM_OUT_OF_RANGE (2, type);
1609
1610 STRING_SYSCALL (path, c_path,
1611 val = mknod (c_path,
1612 ctype | scm_to_int (perms),
1613 scm_to_int (dev)));
1614 if (val != 0)
1615 SCM_SYSERROR;
1616 return SCM_UNSPECIFIED;
1617 }
1618 #undef FUNC_NAME
1619 #endif /* HAVE_MKNOD */
1620
1621 #ifdef HAVE_NICE
1622 SCM_DEFINE (scm_nice, "nice", 1, 0, 0,
1623 (SCM incr),
1624 "Increment the priority of the current process by @var{incr}. A higher\n"
1625 "priority value means that the process runs less often.\n"
1626 "The return value is unspecified.")
1627 #define FUNC_NAME s_scm_nice
1628 {
1629 /* nice() returns "prio-NZERO" on success or -1 on error, but -1 can arise
1630 from "prio-NZERO", so an error must be detected from errno changed */
1631 errno = 0;
1632 nice (scm_to_int (incr));
1633 if (errno != 0)
1634 SCM_SYSERROR;
1635
1636 return SCM_UNSPECIFIED;
1637 }
1638 #undef FUNC_NAME
1639 #endif /* HAVE_NICE */
1640
1641 #ifdef HAVE_SYNC
1642 SCM_DEFINE (scm_sync, "sync", 0, 0, 0,
1643 (),
1644 "Flush the operating system disk buffers.\n"
1645 "The return value is unspecified.")
1646 #define FUNC_NAME s_scm_sync
1647 {
1648 sync();
1649 return SCM_UNSPECIFIED;
1650 }
1651 #undef FUNC_NAME
1652 #endif /* HAVE_SYNC */
1653
1654
1655 /* crypt() returns a pointer to a static buffer, so we use scm_i_misc_mutex
1656 to avoid another thread overwriting it. A test program running crypt
1657 continuously in two threads can be quickly seen tripping this problem.
1658 crypt() is pretty slow normally, so a mutex shouldn't add much overhead.
1659
1660 glibc has a thread-safe crypt_r, but (in version 2.3.2) it runs a lot
1661 slower (about 5x) than plain crypt if you pass an uninitialized data
1662 block each time. Presumably there's some one-time setups. The best way
1663 to use crypt_r for parallel execution in multiple threads would probably
1664 be to maintain a little pool of initialized crypt_data structures, take
1665 one and use it, then return it to the pool. That pool could be garbage
1666 collected so it didn't add permanently to memory use if only a few crypt
1667 calls are made. But we expect crypt will be used rarely, and even more
1668 rarely will there be any desire for lots of parallel execution on
1669 multiple cpus. So for now we don't bother with anything fancy, just
1670 ensure it works. */
1671
1672 #ifdef HAVE_CRYPT
1673 SCM_DEFINE (scm_crypt, "crypt", 2, 0, 0,
1674 (SCM key, SCM salt),
1675 "Encrypt @var{key} using @var{salt} as the salt value to the\n"
1676 "crypt(3) library call.")
1677 #define FUNC_NAME s_scm_crypt
1678 {
1679 SCM ret;
1680 char *c_key, *c_salt, *c_ret;
1681
1682 scm_dynwind_begin (0);
1683 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
1684
1685 c_key = scm_to_locale_string (key);
1686 scm_dynwind_free (c_key);
1687 c_salt = scm_to_locale_string (salt);
1688 scm_dynwind_free (c_salt);
1689
1690 /* The Linux crypt(3) man page says crypt will return NULL and set errno
1691 on error. (Eg. ENOSYS if legal restrictions mean it cannot be
1692 implemented). */
1693 c_ret = crypt (c_key, c_salt);
1694 if (c_ret == NULL)
1695 SCM_SYSERROR;
1696
1697 ret = scm_from_locale_string (c_ret);
1698 scm_dynwind_end ();
1699 return ret;
1700 }
1701 #undef FUNC_NAME
1702 #endif /* HAVE_CRYPT */
1703
1704 #if HAVE_CHROOT
1705 SCM_DEFINE (scm_chroot, "chroot", 1, 0, 0,
1706 (SCM path),
1707 "Change the root directory to that specified in @var{path}.\n"
1708 "This directory will be used for path names beginning with\n"
1709 "@file{/}. The root directory is inherited by all children\n"
1710 "of the current process. Only the superuser may change the\n"
1711 "root directory.")
1712 #define FUNC_NAME s_scm_chroot
1713 {
1714 int rv;
1715
1716 WITH_STRING (path, c_path,
1717 rv = chroot (c_path));
1718 if (rv == -1)
1719 SCM_SYSERROR;
1720 return SCM_UNSPECIFIED;
1721 }
1722 #undef FUNC_NAME
1723 #endif /* HAVE_CHROOT */
1724
1725
1726 #ifdef __MINGW32__
1727 /* Wrapper function to supplying `getlogin()' under Windows. */
1728 static char * getlogin (void)
1729 {
1730 static char user[256];
1731 static unsigned long len = 256;
1732
1733 if (!GetUserName (user, &len))
1734 return NULL;
1735 return user;
1736 }
1737 #endif /* __MINGW32__ */
1738
1739
1740 #if defined (HAVE_GETLOGIN) || defined (__MINGW32__)
1741 SCM_DEFINE (scm_getlogin, "getlogin", 0, 0, 0,
1742 (void),
1743 "Return a string containing the name of the user logged in on\n"
1744 "the controlling terminal of the process, or @code{#f} if this\n"
1745 "information cannot be obtained.")
1746 #define FUNC_NAME s_scm_getlogin
1747 {
1748 char * p;
1749
1750 p = getlogin ();
1751 if (!p || !*p)
1752 return SCM_BOOL_F;
1753 return scm_from_locale_string (p);
1754 }
1755 #undef FUNC_NAME
1756 #endif /* HAVE_GETLOGIN */
1757
1758 #if HAVE_GETPRIORITY
1759 SCM_DEFINE (scm_getpriority, "getpriority", 2, 0, 0,
1760 (SCM which, SCM who),
1761 "Return the scheduling priority of the process, process group\n"
1762 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1763 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1764 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1765 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1766 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1767 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1768 "denotes the current process, process group, or user. Return\n"
1769 "the highest priority (lowest numerical value) of any of the\n"
1770 "specified processes.")
1771 #define FUNC_NAME s_scm_getpriority
1772 {
1773 int cwhich, cwho, ret;
1774
1775 cwhich = scm_to_int (which);
1776 cwho = scm_to_int (who);
1777
1778 /* We have to clear errno and examine it later, because -1 is a
1779 legal return value for getpriority(). */
1780 errno = 0;
1781 ret = getpriority (cwhich, cwho);
1782 if (errno != 0)
1783 SCM_SYSERROR;
1784 return scm_from_int (ret);
1785 }
1786 #undef FUNC_NAME
1787 #endif /* HAVE_GETPRIORITY */
1788
1789 #if HAVE_SETPRIORITY
1790 SCM_DEFINE (scm_setpriority, "setpriority", 3, 0, 0,
1791 (SCM which, SCM who, SCM prio),
1792 "Set the scheduling priority of the process, process group\n"
1793 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1794 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1795 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1796 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1797 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1798 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1799 "denotes the current process, process group, or user.\n"
1800 "@var{prio} is a value in the range -20 and 20, the default\n"
1801 "priority is 0; lower priorities cause more favorable\n"
1802 "scheduling. Sets the priority of all of the specified\n"
1803 "processes. Only the super-user may lower priorities.\n"
1804 "The return value is not specified.")
1805 #define FUNC_NAME s_scm_setpriority
1806 {
1807 int cwhich, cwho, cprio;
1808
1809 cwhich = scm_to_int (which);
1810 cwho = scm_to_int (who);
1811 cprio = scm_to_int (prio);
1812
1813 if (setpriority (cwhich, cwho, cprio) == -1)
1814 SCM_SYSERROR;
1815 return SCM_UNSPECIFIED;
1816 }
1817 #undef FUNC_NAME
1818 #endif /* HAVE_SETPRIORITY */
1819
1820 #ifdef HAVE_SCHED_GETAFFINITY
1821
1822 static SCM
1823 cpu_set_to_bitvector (const cpu_set_t *cs)
1824 {
1825 SCM bv;
1826 size_t cpu;
1827
1828 bv = scm_c_make_bitvector (sizeof (*cs), SCM_BOOL_F);
1829
1830 for (cpu = 0; cpu < sizeof (*cs); cpu++)
1831 {
1832 if (CPU_ISSET (cpu, cs))
1833 /* XXX: This is inefficient but avoids code duplication. */
1834 scm_c_bitvector_set_x (bv, cpu, SCM_BOOL_T);
1835 }
1836
1837 return bv;
1838 }
1839
1840 SCM_DEFINE (scm_getaffinity, "getaffinity", 1, 0, 0,
1841 (SCM pid),
1842 "Return a bitvector representing the CPU affinity mask for\n"
1843 "process @var{pid}. Each CPU the process has affinity with\n"
1844 "has its corresponding bit set in the returned bitvector.\n"
1845 "The number of bits set is a good estimate of how many CPUs\n"
1846 "Guile can use without stepping on other processes' toes.\n\n"
1847 "Currently this procedure is only defined on GNU variants\n"
1848 "(@pxref{CPU Affinity, @code{sched_getaffinity},, libc, The\n"
1849 "GNU C Library Reference Manual}).\n")
1850 #define FUNC_NAME s_scm_getaffinity
1851 {
1852 int err;
1853 cpu_set_t cs;
1854
1855 CPU_ZERO (&cs);
1856 err = sched_getaffinity (scm_to_int (pid), sizeof (cs), &cs);
1857 if (err)
1858 SCM_SYSERROR;
1859
1860 return cpu_set_to_bitvector (&cs);
1861 }
1862 #undef FUNC_NAME
1863
1864 #endif /* HAVE_SCHED_GETAFFINITY */
1865
1866 #ifdef HAVE_SCHED_SETAFFINITY
1867
1868 SCM_DEFINE (scm_setaffinity, "setaffinity", 2, 0, 0,
1869 (SCM pid, SCM mask),
1870 "Install the CPU affinity mask @var{mask}, a bitvector, for\n"
1871 "the process or thread with ID @var{pid}. The return value\n"
1872 "is unspecified.\n\n"
1873 "Currently this procedure is only defined on GNU variants\n"
1874 "(@pxref{CPU Affinity, @code{sched_setaffinity},, libc, The\n"
1875 "GNU C Library Reference Manual}).\n")
1876 #define FUNC_NAME s_scm_setaffinity
1877 {
1878 cpu_set_t cs;
1879 scm_t_array_handle handle;
1880 const scm_t_uint32 *c_mask;
1881 size_t len, off, cpu;
1882 ssize_t inc;
1883 int err;
1884
1885 c_mask = scm_bitvector_elements (mask, &handle, &off, &len, &inc);
1886
1887 CPU_ZERO (&cs);
1888 for (cpu = 0; cpu < len; cpu++)
1889 {
1890 size_t idx;
1891
1892 idx = cpu * inc + off;
1893 if (c_mask[idx / 32] & (1UL << (idx % 32)))
1894 CPU_SET (cpu, &cs);
1895 }
1896
1897 err = sched_setaffinity (scm_to_int (pid), sizeof (cs), &cs);
1898 if (err)
1899 SCM_SYSERROR;
1900
1901 return SCM_UNSPECIFIED;
1902 }
1903 #undef FUNC_NAME
1904
1905 #endif /* HAVE_SCHED_SETAFFINITY */
1906
1907 \f
1908 #if HAVE_GETPASS
1909 SCM_DEFINE (scm_getpass, "getpass", 1, 0, 0,
1910 (SCM prompt),
1911 "Display @var{prompt} to the standard error output and read\n"
1912 "a password from @file{/dev/tty}. If this file is not\n"
1913 "accessible, it reads from standard input. The password may be\n"
1914 "up to 127 characters in length. Additional characters and the\n"
1915 "terminating newline character are discarded. While reading\n"
1916 "the password, echoing and the generation of signals by special\n"
1917 "characters is disabled.")
1918 #define FUNC_NAME s_scm_getpass
1919 {
1920 char * p;
1921 SCM passwd;
1922
1923 SCM_VALIDATE_STRING (1, prompt);
1924
1925 WITH_STRING (prompt, c_prompt,
1926 p = getpass(c_prompt));
1927 passwd = scm_from_locale_string (p);
1928
1929 /* Clear out the password in the static buffer. */
1930 memset (p, 0, strlen (p));
1931
1932 return passwd;
1933 }
1934 #undef FUNC_NAME
1935 #endif /* HAVE_GETPASS */
1936
1937 SCM_DEFINE (scm_flock, "flock", 2, 0, 0,
1938 (SCM file, SCM operation),
1939 "Apply or remove an advisory lock on an open file.\n"
1940 "@var{operation} specifies the action to be done:\n"
1941 "\n"
1942 "@defvar LOCK_SH\n"
1943 "Shared lock. More than one process may hold a shared lock\n"
1944 "for a given file at a given time.\n"
1945 "@end defvar\n"
1946 "@defvar LOCK_EX\n"
1947 "Exclusive lock. Only one process may hold an exclusive lock\n"
1948 "for a given file at a given time.\n"
1949 "@end defvar\n"
1950 "@defvar LOCK_UN\n"
1951 "Unlock the file.\n"
1952 "@end defvar\n"
1953 "@defvar LOCK_NB\n"
1954 "Don't block when locking. This is combined with one of the\n"
1955 "other operations using @code{logior}. If @code{flock} would\n"
1956 "block an @code{EWOULDBLOCK} error is thrown.\n"
1957 "@end defvar\n"
1958 "\n"
1959 "The return value is not specified. @var{file} may be an open\n"
1960 "file descriptor or an open file descriptor port.\n"
1961 "\n"
1962 "Note that @code{flock} does not lock files across NFS.")
1963 #define FUNC_NAME s_scm_flock
1964 {
1965 int fdes;
1966
1967 if (scm_is_integer (file))
1968 fdes = scm_to_int (file);
1969 else
1970 {
1971 SCM_VALIDATE_OPFPORT (2, file);
1972
1973 fdes = SCM_FPORT_FDES (file);
1974 }
1975 if (flock (fdes, scm_to_int (operation)) == -1)
1976 SCM_SYSERROR;
1977 return SCM_UNSPECIFIED;
1978 }
1979 #undef FUNC_NAME
1980
1981 #if HAVE_SETHOSTNAME
1982 SCM_DEFINE (scm_sethostname, "sethostname", 1, 0, 0,
1983 (SCM name),
1984 "Set the host name of the current processor to @var{name}. May\n"
1985 "only be used by the superuser. The return value is not\n"
1986 "specified.")
1987 #define FUNC_NAME s_scm_sethostname
1988 {
1989 int rv;
1990
1991 WITH_STRING (name, c_name,
1992 rv = sethostname (c_name, strlen(c_name)));
1993 if (rv == -1)
1994 SCM_SYSERROR;
1995 return SCM_UNSPECIFIED;
1996 }
1997 #undef FUNC_NAME
1998 #endif /* HAVE_SETHOSTNAME */
1999
2000
2001 #if HAVE_GETHOSTNAME
2002 SCM_DEFINE (scm_gethostname, "gethostname", 0, 0, 0,
2003 (void),
2004 "Return the host name of the current processor.")
2005 #define FUNC_NAME s_scm_gethostname
2006 {
2007 #ifdef MAXHOSTNAMELEN
2008
2009 /* Various systems define MAXHOSTNAMELEN (including Solaris in fact).
2010 * On GNU/Linux this doesn't include the terminating '\0', hence "+ 1". */
2011 const int len = MAXHOSTNAMELEN + 1;
2012 char *const p = scm_malloc (len);
2013 const int res = gethostname (p, len);
2014
2015 scm_dynwind_begin (0);
2016 scm_dynwind_unwind_handler (free, p, 0);
2017
2018 #else
2019
2020 /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
2021 * large enough. SUSv2 specifies 255 maximum too, apparently. */
2022 int len = 256;
2023 int res;
2024 char *p;
2025
2026 # if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX)
2027
2028 /* POSIX specifies the HOST_NAME_MAX system parameter for the max size,
2029 * which may reflect a particular kernel configuration.
2030 * Must watch out for this existing but giving -1, as happens for instance
2031 * in gnu/linux glibc 2.3.2. */
2032 {
2033 const long int n = sysconf (_SC_HOST_NAME_MAX);
2034 if (n != -1L)
2035 len = n;
2036 }
2037
2038 # endif
2039
2040 p = scm_malloc (len);
2041
2042 scm_dynwind_begin (0);
2043 scm_dynwind_unwind_handler (free, p, 0);
2044
2045 res = gethostname (p, len);
2046 while (res == -1 && errno == ENAMETOOLONG)
2047 {
2048 len *= 2;
2049
2050 /* scm_realloc may throw an exception. */
2051 p = scm_realloc (p, len);
2052 res = gethostname (p, len);
2053 }
2054
2055 #endif
2056
2057 if (res == -1)
2058 {
2059 const int save_errno = errno;
2060
2061 /* No guile exceptions can occur before we have freed p's memory. */
2062 scm_dynwind_end ();
2063 free (p);
2064
2065 errno = save_errno;
2066 SCM_SYSERROR;
2067 }
2068 else
2069 {
2070 /* scm_from_locale_string may throw an exception. */
2071 const SCM name = scm_from_locale_string (p);
2072
2073 /* No guile exceptions can occur before we have freed p's memory. */
2074 scm_dynwind_end ();
2075 free (p);
2076
2077 return name;
2078 }
2079 }
2080 #undef FUNC_NAME
2081 #endif /* HAVE_GETHOSTNAME */
2082
2083 \f
2084 void
2085 scm_init_posix ()
2086 {
2087 scm_add_feature ("posix");
2088 #ifdef HAVE_GETEUID
2089 scm_add_feature ("EIDs");
2090 #endif
2091 #ifdef WAIT_ANY
2092 scm_c_define ("WAIT_ANY", scm_from_int (WAIT_ANY));
2093 #endif
2094 #ifdef WAIT_MYPGRP
2095 scm_c_define ("WAIT_MYPGRP", scm_from_int (WAIT_MYPGRP));
2096 #endif
2097 #ifdef WNOHANG
2098 scm_c_define ("WNOHANG", scm_from_int (WNOHANG));
2099 #endif
2100 #ifdef WUNTRACED
2101 scm_c_define ("WUNTRACED", scm_from_int (WUNTRACED));
2102 #endif
2103
2104 #ifdef LC_COLLATE
2105 scm_c_define ("LC_COLLATE", scm_from_int (LC_COLLATE));
2106 #endif
2107 #ifdef LC_CTYPE
2108 scm_c_define ("LC_CTYPE", scm_from_int (LC_CTYPE));
2109 #endif
2110 #ifdef LC_MONETARY
2111 scm_c_define ("LC_MONETARY", scm_from_int (LC_MONETARY));
2112 #endif
2113 #ifdef LC_NUMERIC
2114 scm_c_define ("LC_NUMERIC", scm_from_int (LC_NUMERIC));
2115 #endif
2116 #ifdef LC_TIME
2117 scm_c_define ("LC_TIME", scm_from_int (LC_TIME));
2118 #endif
2119 #ifdef LC_MESSAGES
2120 scm_c_define ("LC_MESSAGES", scm_from_int (LC_MESSAGES));
2121 #endif
2122 #ifdef LC_ALL
2123 scm_c_define ("LC_ALL", scm_from_int (LC_ALL));
2124 #endif
2125 #ifdef LC_PAPER
2126 scm_c_define ("LC_PAPER", scm_from_int (LC_PAPER));
2127 #endif
2128 #ifdef LC_NAME
2129 scm_c_define ("LC_NAME", scm_from_int (LC_NAME));
2130 #endif
2131 #ifdef LC_ADDRESS
2132 scm_c_define ("LC_ADDRESS", scm_from_int (LC_ADDRESS));
2133 #endif
2134 #ifdef LC_TELEPHONE
2135 scm_c_define ("LC_TELEPHONE", scm_from_int (LC_TELEPHONE));
2136 #endif
2137 #ifdef LC_MEASUREMENT
2138 scm_c_define ("LC_MEASUREMENT", scm_from_int (LC_MEASUREMENT));
2139 #endif
2140 #ifdef LC_IDENTIFICATION
2141 scm_c_define ("LC_IDENTIFICATION", scm_from_int (LC_IDENTIFICATION));
2142 #endif
2143 #ifdef PIPE_BUF
2144 scm_c_define ("PIPE_BUF", scm_from_long (PIPE_BUF));
2145 #endif
2146
2147 #ifdef PRIO_PROCESS
2148 scm_c_define ("PRIO_PROCESS", scm_from_int (PRIO_PROCESS));
2149 #endif
2150 #ifdef PRIO_PGRP
2151 scm_c_define ("PRIO_PGRP", scm_from_int (PRIO_PGRP));
2152 #endif
2153 #ifdef PRIO_USER
2154 scm_c_define ("PRIO_USER", scm_from_int (PRIO_USER));
2155 #endif
2156
2157 #ifdef LOCK_SH
2158 scm_c_define ("LOCK_SH", scm_from_int (LOCK_SH));
2159 #endif
2160 #ifdef LOCK_EX
2161 scm_c_define ("LOCK_EX", scm_from_int (LOCK_EX));
2162 #endif
2163 #ifdef LOCK_UN
2164 scm_c_define ("LOCK_UN", scm_from_int (LOCK_UN));
2165 #endif
2166 #ifdef LOCK_NB
2167 scm_c_define ("LOCK_NB", scm_from_int (LOCK_NB));
2168 #endif
2169
2170 #include "libguile/cpp-SIG.c"
2171 #include "libguile/posix.x"
2172 }
2173
2174 /*
2175 Local Variables:
2176 c-file-style: "gnu"
2177 End:
2178 */