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