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