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