Move `{total,current}-processor-count' outside of `posix.c'.
[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 (s == sym_as)
515 return RLIMIT_AS;
516 #endif
517 #ifdef RLIMIT_CORE
518 if (s == sym_core)
519 return RLIMIT_CORE;
520 #endif
521 #ifdef RLIMIT_CPU
522 if (s == sym_cpu)
523 return RLIMIT_CPU;
524 #endif
525 #ifdef RLIMIT_DATA
526 if (s == sym_data)
527 return RLIMIT_DATA;
528 #endif
529 #ifdef RLIMIT_FSIZE
530 if (s == sym_fsize)
531 return RLIMIT_FSIZE;
532 #endif
533 #ifdef RLIMIT_MEMLOCK
534 if (s == sym_memlock)
535 return RLIMIT_MEMLOCK;
536 #endif
537 #ifdef RLIMIT_MSGQUEUE
538 if (s == sym_msgqueue)
539 return RLIMIT_MSGQUEUE;
540 #endif
541 #ifdef RLIMIT_NICE
542 if (s == sym_nice)
543 return RLIMIT_NICE;
544 #endif
545 #ifdef RLIMIT_NOFILE
546 if (s == sym_nofile)
547 return RLIMIT_NOFILE;
548 #endif
549 #ifdef RLIMIT_NPROC
550 if (s == sym_nproc)
551 return RLIMIT_NPROC;
552 #endif
553 #ifdef RLIMIT_RSS
554 if (s == sym_rss)
555 return RLIMIT_RSS;
556 #endif
557 #ifdef RLIMIT_RTPRIO
558 if (s == sym_rtprio)
559 return RLIMIT_RTPRIO;
560 #endif
561 #ifdef RLIMIT_RTPRIO
562 if (s == sym_rttime)
563 return RLIMIT_RTPRIO;
564 #endif
565 #ifdef RLIMIT_SIGPENDING
566 if (s == sym_sigpending)
567 return RLIMIT_SIGPENDING;
568 #endif
569 #ifdef RLIMIT_STACK
570 if (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 = (soft == SCM_BOOL_F) ? RLIM_INFINITY : scm_to_long (soft);
619 lim.rlim_max = (hard == SCM_BOOL_F) ? 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
1342 if (! (rv = tmpfile ()))
1343 SCM_SYSERROR;
1344 return scm_fdes_to_port (fileno (rv), "w+", SCM_BOOL_F);
1345 }
1346 #undef FUNC_NAME
1347
1348 SCM_DEFINE (scm_utime, "utime", 1, 5, 0,
1349 (SCM pathname, SCM actime, SCM modtime, SCM actimens, SCM modtimens,
1350 SCM flags),
1351 "@code{utime} sets the access and modification times for the\n"
1352 "file named by @var{path}. If @var{actime} or @var{modtime} is\n"
1353 "not supplied, then the current time is used. @var{actime} and\n"
1354 "@var{modtime} must be integer time values as returned by the\n"
1355 "@code{current-time} procedure.\n\n"
1356 "The optional @var{actimens} and @var{modtimens} are nanoseconds\n"
1357 "to add @var{actime} and @var{modtime}. Nanosecond precision is\n"
1358 "only supported on some combinations of file systems and operating\n"
1359 "systems.\n"
1360 "@lisp\n"
1361 "(utime \"foo\" (- (current-time) 3600))\n"
1362 "@end lisp\n"
1363 "will set the access time to one hour in the past and the\n"
1364 "modification time to the current time.")
1365 #define FUNC_NAME s_scm_utime
1366 {
1367 int rv;
1368 time_t atim_sec, mtim_sec;
1369 long atim_nsec, mtim_nsec;
1370 int f;
1371
1372 if (SCM_UNBNDP (actime))
1373 {
1374 #ifdef HAVE_UTIMENSAT
1375 atim_sec = 0;
1376 atim_nsec = UTIME_NOW;
1377 #else
1378 SCM_SYSCALL (time (&atim_sec));
1379 atim_nsec = 0;
1380 #endif
1381 }
1382 else
1383 {
1384 atim_sec = SCM_NUM2ULONG (2, actime);
1385 if (SCM_UNBNDP (actimens))
1386 atim_nsec = 0;
1387 else
1388 atim_nsec = SCM_NUM2LONG (4, actimens);
1389 }
1390
1391 if (SCM_UNBNDP (modtime))
1392 {
1393 #ifdef HAVE_UTIMENSAT
1394 mtim_sec = 0;
1395 mtim_nsec = UTIME_NOW;
1396 #else
1397 SCM_SYSCALL (time (&mtim_sec));
1398 mtim_nsec = 0;
1399 #endif
1400 }
1401 else
1402 {
1403 mtim_sec = SCM_NUM2ULONG (3, modtime);
1404 if (SCM_UNBNDP (modtimens))
1405 mtim_nsec = 0;
1406 else
1407 mtim_nsec = SCM_NUM2LONG (5, modtimens);
1408 }
1409
1410 if (SCM_UNBNDP (flags))
1411 f = 0;
1412 else
1413 f = SCM_NUM2INT (6, flags);
1414
1415 #ifdef HAVE_UTIMENSAT
1416 {
1417 struct timespec times[2];
1418 times[0].tv_sec = atim_sec;
1419 times[0].tv_nsec = atim_nsec;
1420 times[1].tv_sec = mtim_sec;
1421 times[1].tv_nsec = mtim_nsec;
1422
1423 STRING_SYSCALL (pathname, c_pathname,
1424 rv = utimensat (AT_FDCWD, c_pathname, times, f));
1425 }
1426 #else
1427 {
1428 struct utimbuf utm;
1429 utm.actime = atim_sec;
1430 utm.modtime = mtim_sec;
1431
1432 STRING_SYSCALL (pathname, c_pathname,
1433 rv = utime (c_pathname, &utm));
1434 }
1435 #endif
1436
1437 if (rv != 0)
1438 SCM_SYSERROR;
1439 return SCM_UNSPECIFIED;
1440 }
1441 #undef FUNC_NAME
1442
1443 SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0,
1444 (),
1445 "Return an integer representing the current process ID.")
1446 #define FUNC_NAME s_scm_getpid
1447 {
1448 return scm_from_ulong (getpid ());
1449 }
1450 #undef FUNC_NAME
1451
1452 SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0,
1453 (SCM str),
1454 "Modifies the environment of the current process, which is\n"
1455 "also the default environment inherited by child processes.\n\n"
1456 "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n"
1457 "directly into the environment, replacing any existing environment string\n"
1458 "with\n"
1459 "name matching @code{NAME}. If @var{string} does not contain an equal\n"
1460 "sign, then any existing string with name matching @var{string} will\n"
1461 "be removed.\n\n"
1462 "The return value is unspecified.")
1463 #define FUNC_NAME s_scm_putenv
1464 {
1465 int rv;
1466 char *c_str = scm_to_locale_string (str);
1467
1468 /* Leave C_STR in the environment. */
1469
1470 /* Gnulib's `putenv' module honors the semantics described above. */
1471 rv = putenv (c_str);
1472 if (rv < 0)
1473 SCM_SYSERROR;
1474
1475 return SCM_UNSPECIFIED;
1476 }
1477 #undef FUNC_NAME
1478
1479 /* This mutex is used to serialize invocations of `setlocale ()' on non-GNU
1480 systems (i.e., systems where a reentrant locale API is not available). It
1481 is also acquired before calls to `nl_langinfo ()'. See `i18n.c' for
1482 details. */
1483 scm_i_pthread_mutex_t scm_i_locale_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
1484
1485 #ifdef HAVE_SETLOCALE
1486
1487 SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
1488 (SCM category, SCM locale),
1489 "If @var{locale} is omitted, return the current value of the\n"
1490 "specified locale category as a system-dependent string.\n"
1491 "@var{category} should be specified using the values\n"
1492 "@code{LC_COLLATE}, @code{LC_ALL} etc.\n"
1493 "\n"
1494 "Otherwise the specified locale category is set to the string\n"
1495 "@var{locale} and the new value is returned as a\n"
1496 "system-dependent string. If @var{locale} is an empty string,\n"
1497 "the locale will be set using environment variables.\n"
1498 "\n"
1499 "When the locale is changed, the character encoding of the new\n"
1500 "locale (UTF-8, ISO-8859-1, etc.) is used for the current\n"
1501 "input, output, and error ports\n")
1502 #define FUNC_NAME s_scm_setlocale
1503 {
1504 int c_category;
1505 char *clocale;
1506 char *rv;
1507 const char *enc;
1508
1509 scm_dynwind_begin (0);
1510
1511 if (SCM_UNBNDP (locale))
1512 {
1513 clocale = NULL;
1514 }
1515 else
1516 {
1517 clocale = scm_to_locale_string (locale);
1518 scm_dynwind_free (clocale);
1519 }
1520
1521 c_category = scm_i_to_lc_category (category, 1);
1522
1523 scm_i_pthread_mutex_lock (&scm_i_locale_mutex);
1524 rv = setlocale (c_category, clocale);
1525 scm_i_pthread_mutex_unlock (&scm_i_locale_mutex);
1526
1527 if (rv == NULL)
1528 {
1529 /* POSIX and C99 don't say anything about setlocale setting errno, so
1530 force a sensible value here. glibc leaves ENOENT, which would be
1531 fine, but it's not a documented feature. */
1532 errno = EINVAL;
1533 SCM_SYSERROR;
1534 }
1535
1536 enc = locale_charset ();
1537
1538 /* Set the default encoding for new ports. */
1539 scm_i_set_default_port_encoding (enc);
1540
1541 /* Set the encoding for the stdio ports. */
1542 scm_i_set_port_encoding_x (scm_current_input_port (), enc);
1543 scm_i_set_port_encoding_x (scm_current_output_port (), enc);
1544 scm_i_set_port_encoding_x (scm_current_error_port (), enc);
1545
1546 scm_dynwind_end ();
1547 return scm_from_locale_string (rv);
1548 }
1549 #undef FUNC_NAME
1550 #endif /* HAVE_SETLOCALE */
1551
1552 #ifdef HAVE_MKNOD
1553 SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
1554 (SCM path, SCM type, SCM perms, SCM dev),
1555 "Creates a new special file, such as a file corresponding to a device.\n"
1556 "@var{path} specifies the name of the file. @var{type} should\n"
1557 "be one of the following symbols:\n"
1558 "regular, directory, symlink, block-special, char-special,\n"
1559 "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
1560 "@var{dev} (an integer) specifies which device the special file refers\n"
1561 "to. Its exact interpretation depends on the kind of special file\n"
1562 "being created.\n\n"
1563 "E.g.,\n"
1564 "@lisp\n"
1565 "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
1566 "@end lisp\n\n"
1567 "The return value is unspecified.")
1568 #define FUNC_NAME s_scm_mknod
1569 {
1570 int val;
1571 const char *p;
1572 int ctype = 0;
1573
1574 SCM_VALIDATE_STRING (1, path);
1575 SCM_VALIDATE_SYMBOL (2, type);
1576
1577 p = scm_i_symbol_chars (type);
1578 if (strcmp (p, "regular") == 0)
1579 ctype = S_IFREG;
1580 else if (strcmp (p, "directory") == 0)
1581 ctype = S_IFDIR;
1582 #ifdef S_IFLNK
1583 /* systems without symlinks probably don't have S_IFLNK defined */
1584 else if (strcmp (p, "symlink") == 0)
1585 ctype = S_IFLNK;
1586 #endif
1587 else if (strcmp (p, "block-special") == 0)
1588 ctype = S_IFBLK;
1589 else if (strcmp (p, "char-special") == 0)
1590 ctype = S_IFCHR;
1591 else if (strcmp (p, "fifo") == 0)
1592 ctype = S_IFIFO;
1593 #ifdef S_IFSOCK
1594 else if (strcmp (p, "socket") == 0)
1595 ctype = S_IFSOCK;
1596 #endif
1597 else
1598 SCM_OUT_OF_RANGE (2, type);
1599
1600 STRING_SYSCALL (path, c_path,
1601 val = mknod (c_path,
1602 ctype | scm_to_int (perms),
1603 scm_to_int (dev)));
1604 if (val != 0)
1605 SCM_SYSERROR;
1606 return SCM_UNSPECIFIED;
1607 }
1608 #undef FUNC_NAME
1609 #endif /* HAVE_MKNOD */
1610
1611 #ifdef HAVE_NICE
1612 SCM_DEFINE (scm_nice, "nice", 1, 0, 0,
1613 (SCM incr),
1614 "Increment the priority of the current process by @var{incr}. A higher\n"
1615 "priority value means that the process runs less often.\n"
1616 "The return value is unspecified.")
1617 #define FUNC_NAME s_scm_nice
1618 {
1619 /* nice() returns "prio-NZERO" on success or -1 on error, but -1 can arise
1620 from "prio-NZERO", so an error must be detected from errno changed */
1621 errno = 0;
1622 nice (scm_to_int (incr));
1623 if (errno != 0)
1624 SCM_SYSERROR;
1625
1626 return SCM_UNSPECIFIED;
1627 }
1628 #undef FUNC_NAME
1629 #endif /* HAVE_NICE */
1630
1631 #ifdef HAVE_SYNC
1632 SCM_DEFINE (scm_sync, "sync", 0, 0, 0,
1633 (),
1634 "Flush the operating system disk buffers.\n"
1635 "The return value is unspecified.")
1636 #define FUNC_NAME s_scm_sync
1637 {
1638 sync();
1639 return SCM_UNSPECIFIED;
1640 }
1641 #undef FUNC_NAME
1642 #endif /* HAVE_SYNC */
1643
1644
1645 /* crypt() returns a pointer to a static buffer, so we use scm_i_misc_mutex
1646 to avoid another thread overwriting it. A test program running crypt
1647 continuously in two threads can be quickly seen tripping this problem.
1648 crypt() is pretty slow normally, so a mutex shouldn't add much overhead.
1649
1650 glibc has a thread-safe crypt_r, but (in version 2.3.2) it runs a lot
1651 slower (about 5x) than plain crypt if you pass an uninitialized data
1652 block each time. Presumably there's some one-time setups. The best way
1653 to use crypt_r for parallel execution in multiple threads would probably
1654 be to maintain a little pool of initialized crypt_data structures, take
1655 one and use it, then return it to the pool. That pool could be garbage
1656 collected so it didn't add permanently to memory use if only a few crypt
1657 calls are made. But we expect crypt will be used rarely, and even more
1658 rarely will there be any desire for lots of parallel execution on
1659 multiple cpus. So for now we don't bother with anything fancy, just
1660 ensure it works. */
1661
1662 #ifdef HAVE_CRYPT
1663 SCM_DEFINE (scm_crypt, "crypt", 2, 0, 0,
1664 (SCM key, SCM salt),
1665 "Encrypt @var{key} using @var{salt} as the salt value to the\n"
1666 "crypt(3) library call.")
1667 #define FUNC_NAME s_scm_crypt
1668 {
1669 SCM ret;
1670 char *c_key, *c_salt, *c_ret;
1671
1672 scm_dynwind_begin (0);
1673 scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
1674
1675 c_key = scm_to_locale_string (key);
1676 scm_dynwind_free (c_key);
1677 c_salt = scm_to_locale_string (salt);
1678 scm_dynwind_free (c_salt);
1679
1680 /* The Linux crypt(3) man page says crypt will return NULL and set errno
1681 on error. (Eg. ENOSYS if legal restrictions mean it cannot be
1682 implemented). */
1683 c_ret = crypt (c_key, c_salt);
1684 if (c_ret == NULL)
1685 SCM_SYSERROR;
1686
1687 ret = scm_from_locale_string (c_ret);
1688 scm_dynwind_end ();
1689 return ret;
1690 }
1691 #undef FUNC_NAME
1692 #endif /* HAVE_CRYPT */
1693
1694 #if HAVE_CHROOT
1695 SCM_DEFINE (scm_chroot, "chroot", 1, 0, 0,
1696 (SCM path),
1697 "Change the root directory to that specified in @var{path}.\n"
1698 "This directory will be used for path names beginning with\n"
1699 "@file{/}. The root directory is inherited by all children\n"
1700 "of the current process. Only the superuser may change the\n"
1701 "root directory.")
1702 #define FUNC_NAME s_scm_chroot
1703 {
1704 int rv;
1705
1706 WITH_STRING (path, c_path,
1707 rv = chroot (c_path));
1708 if (rv == -1)
1709 SCM_SYSERROR;
1710 return SCM_UNSPECIFIED;
1711 }
1712 #undef FUNC_NAME
1713 #endif /* HAVE_CHROOT */
1714
1715
1716 #ifdef __MINGW32__
1717 /* Wrapper function to supplying `getlogin()' under Windows. */
1718 static char * getlogin (void)
1719 {
1720 static char user[256];
1721 static unsigned long len = 256;
1722
1723 if (!GetUserName (user, &len))
1724 return NULL;
1725 return user;
1726 }
1727 #endif /* __MINGW32__ */
1728
1729
1730 #if defined (HAVE_GETLOGIN) || defined (__MINGW32__)
1731 SCM_DEFINE (scm_getlogin, "getlogin", 0, 0, 0,
1732 (void),
1733 "Return a string containing the name of the user logged in on\n"
1734 "the controlling terminal of the process, or @code{#f} if this\n"
1735 "information cannot be obtained.")
1736 #define FUNC_NAME s_scm_getlogin
1737 {
1738 char * p;
1739
1740 p = getlogin ();
1741 if (!p || !*p)
1742 return SCM_BOOL_F;
1743 return scm_from_locale_string (p);
1744 }
1745 #undef FUNC_NAME
1746 #endif /* HAVE_GETLOGIN */
1747
1748 #if HAVE_GETPRIORITY
1749 SCM_DEFINE (scm_getpriority, "getpriority", 2, 0, 0,
1750 (SCM which, SCM who),
1751 "Return the scheduling priority of the process, process group\n"
1752 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1753 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1754 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1755 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1756 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1757 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1758 "denotes the current process, process group, or user. Return\n"
1759 "the highest priority (lowest numerical value) of any of the\n"
1760 "specified processes.")
1761 #define FUNC_NAME s_scm_getpriority
1762 {
1763 int cwhich, cwho, ret;
1764
1765 cwhich = scm_to_int (which);
1766 cwho = scm_to_int (who);
1767
1768 /* We have to clear errno and examine it later, because -1 is a
1769 legal return value for getpriority(). */
1770 errno = 0;
1771 ret = getpriority (cwhich, cwho);
1772 if (errno != 0)
1773 SCM_SYSERROR;
1774 return scm_from_int (ret);
1775 }
1776 #undef FUNC_NAME
1777 #endif /* HAVE_GETPRIORITY */
1778
1779 #if HAVE_SETPRIORITY
1780 SCM_DEFINE (scm_setpriority, "setpriority", 3, 0, 0,
1781 (SCM which, SCM who, SCM prio),
1782 "Set the scheduling priority of the process, process group\n"
1783 "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
1784 "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
1785 "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
1786 "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
1787 "process group identifier for @code{PRIO_PGRP}, and a user\n"
1788 "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
1789 "denotes the current process, process group, or user.\n"
1790 "@var{prio} is a value in the range -20 and 20, the default\n"
1791 "priority is 0; lower priorities cause more favorable\n"
1792 "scheduling. Sets the priority of all of the specified\n"
1793 "processes. Only the super-user may lower priorities.\n"
1794 "The return value is not specified.")
1795 #define FUNC_NAME s_scm_setpriority
1796 {
1797 int cwhich, cwho, cprio;
1798
1799 cwhich = scm_to_int (which);
1800 cwho = scm_to_int (who);
1801 cprio = scm_to_int (prio);
1802
1803 if (setpriority (cwhich, cwho, cprio) == -1)
1804 SCM_SYSERROR;
1805 return SCM_UNSPECIFIED;
1806 }
1807 #undef FUNC_NAME
1808 #endif /* HAVE_SETPRIORITY */
1809
1810 #ifdef HAVE_SCHED_GETAFFINITY
1811
1812 static SCM
1813 cpu_set_to_bitvector (const cpu_set_t *cs)
1814 {
1815 SCM bv;
1816 size_t cpu;
1817
1818 bv = scm_c_make_bitvector (sizeof (*cs), SCM_BOOL_F);
1819
1820 for (cpu = 0; cpu < sizeof (*cs); cpu++)
1821 {
1822 if (CPU_ISSET (cpu, cs))
1823 /* XXX: This is inefficient but avoids code duplication. */
1824 scm_c_bitvector_set_x (bv, cpu, SCM_BOOL_T);
1825 }
1826
1827 return bv;
1828 }
1829
1830 SCM_DEFINE (scm_getaffinity, "getaffinity", 1, 0, 0,
1831 (SCM pid),
1832 "Return a bitvector representing the CPU affinity mask for\n"
1833 "process @var{pid}. Each CPU the process has affinity with\n"
1834 "has its corresponding bit set in the returned bitvector.\n"
1835 "The number of bits set is a good estimate of how many CPUs\n"
1836 "Guile can use without stepping on other processes' toes.\n\n"
1837 "Currently this procedure is only defined on GNU variants\n"
1838 "(@pxref{CPU Affinity, @code{sched_getaffinity},, libc, The\n"
1839 "GNU C Library Reference Manual}).\n")
1840 #define FUNC_NAME s_scm_getaffinity
1841 {
1842 int err;
1843 cpu_set_t cs;
1844
1845 CPU_ZERO (&cs);
1846 err = sched_getaffinity (scm_to_int (pid), sizeof (cs), &cs);
1847 if (err)
1848 SCM_SYSERROR;
1849
1850 return cpu_set_to_bitvector (&cs);
1851 }
1852 #undef FUNC_NAME
1853
1854 #endif /* HAVE_SCHED_GETAFFINITY */
1855
1856 #ifdef HAVE_SCHED_SETAFFINITY
1857
1858 SCM_DEFINE (scm_setaffinity, "setaffinity", 2, 0, 0,
1859 (SCM pid, SCM mask),
1860 "Install the CPU affinity mask @var{mask}, a bitvector, for\n"
1861 "the process or thread with ID @var{pid}. The return value\n"
1862 "is unspecified.\n\n"
1863 "Currently this procedure is only defined on GNU variants\n"
1864 "(@pxref{CPU Affinity, @code{sched_setaffinity},, libc, The\n"
1865 "GNU C Library Reference Manual}).\n")
1866 #define FUNC_NAME s_scm_setaffinity
1867 {
1868 cpu_set_t cs;
1869 scm_t_array_handle handle;
1870 const scm_t_uint32 *c_mask;
1871 size_t len, off, cpu;
1872 ssize_t inc;
1873 int err;
1874
1875 c_mask = scm_bitvector_elements (mask, &handle, &off, &len, &inc);
1876
1877 CPU_ZERO (&cs);
1878 for (cpu = 0; cpu < len; cpu++)
1879 {
1880 size_t idx;
1881
1882 idx = cpu * inc + off;
1883 if (c_mask[idx / 32] & (1UL << (idx % 32)))
1884 CPU_SET (cpu, &cs);
1885 }
1886
1887 err = sched_setaffinity (scm_to_int (pid), sizeof (cs), &cs);
1888 if (err)
1889 SCM_SYSERROR;
1890
1891 return SCM_UNSPECIFIED;
1892 }
1893 #undef FUNC_NAME
1894
1895 #endif /* HAVE_SCHED_SETAFFINITY */
1896
1897 \f
1898 #if HAVE_GETPASS
1899 SCM_DEFINE (scm_getpass, "getpass", 1, 0, 0,
1900 (SCM prompt),
1901 "Display @var{prompt} to the standard error output and read\n"
1902 "a password from @file{/dev/tty}. If this file is not\n"
1903 "accessible, it reads from standard input. The password may be\n"
1904 "up to 127 characters in length. Additional characters and the\n"
1905 "terminating newline character are discarded. While reading\n"
1906 "the password, echoing and the generation of signals by special\n"
1907 "characters is disabled.")
1908 #define FUNC_NAME s_scm_getpass
1909 {
1910 char * p;
1911 SCM passwd;
1912
1913 SCM_VALIDATE_STRING (1, prompt);
1914
1915 WITH_STRING (prompt, c_prompt,
1916 p = getpass(c_prompt));
1917 passwd = scm_from_locale_string (p);
1918
1919 /* Clear out the password in the static buffer. */
1920 memset (p, 0, strlen (p));
1921
1922 return passwd;
1923 }
1924 #undef FUNC_NAME
1925 #endif /* HAVE_GETPASS */
1926
1927 SCM_DEFINE (scm_flock, "flock", 2, 0, 0,
1928 (SCM file, SCM operation),
1929 "Apply or remove an advisory lock on an open file.\n"
1930 "@var{operation} specifies the action to be done:\n"
1931 "\n"
1932 "@defvar LOCK_SH\n"
1933 "Shared lock. More than one process may hold a shared lock\n"
1934 "for a given file at a given time.\n"
1935 "@end defvar\n"
1936 "@defvar LOCK_EX\n"
1937 "Exclusive lock. Only one process may hold an exclusive lock\n"
1938 "for a given file at a given time.\n"
1939 "@end defvar\n"
1940 "@defvar LOCK_UN\n"
1941 "Unlock the file.\n"
1942 "@end defvar\n"
1943 "@defvar LOCK_NB\n"
1944 "Don't block when locking. This is combined with one of the\n"
1945 "other operations using @code{logior}. If @code{flock} would\n"
1946 "block an @code{EWOULDBLOCK} error is thrown.\n"
1947 "@end defvar\n"
1948 "\n"
1949 "The return value is not specified. @var{file} may be an open\n"
1950 "file descriptor or an open file descriptor port.\n"
1951 "\n"
1952 "Note that @code{flock} does not lock files across NFS.")
1953 #define FUNC_NAME s_scm_flock
1954 {
1955 int fdes;
1956
1957 if (scm_is_integer (file))
1958 fdes = scm_to_int (file);
1959 else
1960 {
1961 SCM_VALIDATE_OPFPORT (2, file);
1962
1963 fdes = SCM_FPORT_FDES (file);
1964 }
1965 if (flock (fdes, scm_to_int (operation)) == -1)
1966 SCM_SYSERROR;
1967 return SCM_UNSPECIFIED;
1968 }
1969 #undef FUNC_NAME
1970
1971 #if HAVE_SETHOSTNAME
1972 SCM_DEFINE (scm_sethostname, "sethostname", 1, 0, 0,
1973 (SCM name),
1974 "Set the host name of the current processor to @var{name}. May\n"
1975 "only be used by the superuser. The return value is not\n"
1976 "specified.")
1977 #define FUNC_NAME s_scm_sethostname
1978 {
1979 int rv;
1980
1981 WITH_STRING (name, c_name,
1982 rv = sethostname (c_name, strlen(c_name)));
1983 if (rv == -1)
1984 SCM_SYSERROR;
1985 return SCM_UNSPECIFIED;
1986 }
1987 #undef FUNC_NAME
1988 #endif /* HAVE_SETHOSTNAME */
1989
1990
1991 #if HAVE_GETHOSTNAME
1992 SCM_DEFINE (scm_gethostname, "gethostname", 0, 0, 0,
1993 (void),
1994 "Return the host name of the current processor.")
1995 #define FUNC_NAME s_scm_gethostname
1996 {
1997 #ifdef MAXHOSTNAMELEN
1998
1999 /* Various systems define MAXHOSTNAMELEN (including Solaris in fact).
2000 * On GNU/Linux this doesn't include the terminating '\0', hence "+ 1". */
2001 const int len = MAXHOSTNAMELEN + 1;
2002 char *const p = scm_malloc (len);
2003 const int res = gethostname (p, len);
2004
2005 scm_dynwind_begin (0);
2006 scm_dynwind_unwind_handler (free, p, 0);
2007
2008 #else
2009
2010 /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
2011 * large enough. SUSv2 specifies 255 maximum too, apparently. */
2012 int len = 256;
2013 int res;
2014 char *p;
2015
2016 # if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX)
2017
2018 /* POSIX specifies the HOST_NAME_MAX system parameter for the max size,
2019 * which may reflect a particular kernel configuration.
2020 * Must watch out for this existing but giving -1, as happens for instance
2021 * in gnu/linux glibc 2.3.2. */
2022 {
2023 const long int n = sysconf (_SC_HOST_NAME_MAX);
2024 if (n != -1L)
2025 len = n;
2026 }
2027
2028 # endif
2029
2030 p = scm_malloc (len);
2031
2032 scm_dynwind_begin (0);
2033 scm_dynwind_unwind_handler (free, p, 0);
2034
2035 res = gethostname (p, len);
2036 while (res == -1 && errno == ENAMETOOLONG)
2037 {
2038 len *= 2;
2039
2040 /* scm_realloc may throw an exception. */
2041 p = scm_realloc (p, len);
2042 res = gethostname (p, len);
2043 }
2044
2045 #endif
2046
2047 if (res == -1)
2048 {
2049 const int save_errno = errno;
2050
2051 /* No guile exceptions can occur before we have freed p's memory. */
2052 scm_dynwind_end ();
2053 free (p);
2054
2055 errno = save_errno;
2056 SCM_SYSERROR;
2057 }
2058 else
2059 {
2060 /* scm_from_locale_string may throw an exception. */
2061 const SCM name = scm_from_locale_string (p);
2062
2063 /* No guile exceptions can occur before we have freed p's memory. */
2064 scm_dynwind_end ();
2065 free (p);
2066
2067 return name;
2068 }
2069 }
2070 #undef FUNC_NAME
2071 #endif /* HAVE_GETHOSTNAME */
2072
2073 \f
2074 void
2075 scm_init_posix ()
2076 {
2077 scm_add_feature ("posix");
2078 #ifdef HAVE_GETEUID
2079 scm_add_feature ("EIDs");
2080 #endif
2081 #ifdef WAIT_ANY
2082 scm_c_define ("WAIT_ANY", scm_from_int (WAIT_ANY));
2083 #endif
2084 #ifdef WAIT_MYPGRP
2085 scm_c_define ("WAIT_MYPGRP", scm_from_int (WAIT_MYPGRP));
2086 #endif
2087 #ifdef WNOHANG
2088 scm_c_define ("WNOHANG", scm_from_int (WNOHANG));
2089 #endif
2090 #ifdef WUNTRACED
2091 scm_c_define ("WUNTRACED", scm_from_int (WUNTRACED));
2092 #endif
2093
2094 #ifdef LC_COLLATE
2095 scm_c_define ("LC_COLLATE", scm_from_int (LC_COLLATE));
2096 #endif
2097 #ifdef LC_CTYPE
2098 scm_c_define ("LC_CTYPE", scm_from_int (LC_CTYPE));
2099 #endif
2100 #ifdef LC_MONETARY
2101 scm_c_define ("LC_MONETARY", scm_from_int (LC_MONETARY));
2102 #endif
2103 #ifdef LC_NUMERIC
2104 scm_c_define ("LC_NUMERIC", scm_from_int (LC_NUMERIC));
2105 #endif
2106 #ifdef LC_TIME
2107 scm_c_define ("LC_TIME", scm_from_int (LC_TIME));
2108 #endif
2109 #ifdef LC_MESSAGES
2110 scm_c_define ("LC_MESSAGES", scm_from_int (LC_MESSAGES));
2111 #endif
2112 #ifdef LC_ALL
2113 scm_c_define ("LC_ALL", scm_from_int (LC_ALL));
2114 #endif
2115 #ifdef LC_PAPER
2116 scm_c_define ("LC_PAPER", scm_from_int (LC_PAPER));
2117 #endif
2118 #ifdef LC_NAME
2119 scm_c_define ("LC_NAME", scm_from_int (LC_NAME));
2120 #endif
2121 #ifdef LC_ADDRESS
2122 scm_c_define ("LC_ADDRESS", scm_from_int (LC_ADDRESS));
2123 #endif
2124 #ifdef LC_TELEPHONE
2125 scm_c_define ("LC_TELEPHONE", scm_from_int (LC_TELEPHONE));
2126 #endif
2127 #ifdef LC_MEASUREMENT
2128 scm_c_define ("LC_MEASUREMENT", scm_from_int (LC_MEASUREMENT));
2129 #endif
2130 #ifdef LC_IDENTIFICATION
2131 scm_c_define ("LC_IDENTIFICATION", scm_from_int (LC_IDENTIFICATION));
2132 #endif
2133 #ifdef PIPE_BUF
2134 scm_c_define ("PIPE_BUF", scm_from_long (PIPE_BUF));
2135 #endif
2136
2137 #ifdef PRIO_PROCESS
2138 scm_c_define ("PRIO_PROCESS", scm_from_int (PRIO_PROCESS));
2139 #endif
2140 #ifdef PRIO_PGRP
2141 scm_c_define ("PRIO_PGRP", scm_from_int (PRIO_PGRP));
2142 #endif
2143 #ifdef PRIO_USER
2144 scm_c_define ("PRIO_USER", scm_from_int (PRIO_USER));
2145 #endif
2146
2147 #ifdef LOCK_SH
2148 scm_c_define ("LOCK_SH", scm_from_int (LOCK_SH));
2149 #endif
2150 #ifdef LOCK_EX
2151 scm_c_define ("LOCK_EX", scm_from_int (LOCK_EX));
2152 #endif
2153 #ifdef LOCK_UN
2154 scm_c_define ("LOCK_UN", scm_from_int (LOCK_UN));
2155 #endif
2156 #ifdef LOCK_NB
2157 scm_c_define ("LOCK_NB", scm_from_int (LOCK_NB));
2158 #endif
2159
2160 #include "libguile/cpp-SIG.c"
2161 #include "libguile/posix.x"
2162 }
2163
2164 /*
2165 Local Variables:
2166 c-file-style: "gnu"
2167 End:
2168 */