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