1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-08 08:18:32 -07:00
|
|
|
#include <mlvalues.h>
|
|
|
|
#include <alloc.h>
|
|
|
|
#include <memory.h>
|
1997-05-13 08:46:49 -07:00
|
|
|
#include <signals.h>
|
1996-09-04 07:15:31 -07:00
|
|
|
#include "unixsupport.h"
|
1995-05-08 08:18:32 -07:00
|
|
|
|
1996-04-12 08:57:28 -07:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
1996-05-07 01:15:03 -07:00
|
|
|
#if !(defined(WIFEXITED) && defined(WEXITSTATUS) && defined(WIFSTOPPED) && \
|
|
|
|
defined(WSTOPSIG) && defined(WTERMSIG))
|
1997-06-13 08:52:43 -07:00
|
|
|
/* Assume old-style V7 status word */
|
|
|
|
#define WIFEXITED(status) (((status) & 0xFF) == 0)
|
1996-04-12 08:57:28 -07:00
|
|
|
#define WEXITSTATUS(status) (((status) >> 8) & 0xFF)
|
1997-06-13 08:52:43 -07:00
|
|
|
#define WIFSTOPPED(status) (((status) & 0xFF) == 0xFF)
|
1996-04-12 08:57:28 -07:00
|
|
|
#define WSTOPSIG(status) (((status) >> 8) & 0xFF)
|
|
|
|
#define WTERMSIG(status) ((status) & 0x3F)
|
|
|
|
#endif
|
|
|
|
|
1997-06-13 08:52:43 -07:00
|
|
|
#define TAG_WEXITED 0
|
|
|
|
#define TAG_WSIGNALED 1
|
|
|
|
#define TAG_WSTOPPED 2
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static value alloc_process_status(int pid, int status)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
1996-04-12 08:57:28 -07:00
|
|
|
value st, res;
|
|
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
1998-10-26 11:19:32 -08:00
|
|
|
st = alloc_small(1, TAG_WEXITED);
|
1996-04-12 08:57:28 -07:00
|
|
|
Field(st, 0) = Val_int(WEXITSTATUS(status));
|
|
|
|
}
|
|
|
|
else if (WIFSTOPPED(status)) {
|
1998-10-26 11:19:32 -08:00
|
|
|
st = alloc_small(1, TAG_WSTOPPED);
|
1996-04-12 08:57:28 -07:00
|
|
|
Field(st, 0) = Val_int(WSTOPSIG(status));
|
|
|
|
}
|
|
|
|
else {
|
1998-10-26 11:19:32 -08:00
|
|
|
st = alloc_small(1, TAG_WSIGNALED);
|
1996-04-12 08:57:28 -07:00
|
|
|
Field(st, 0) = Val_int(WTERMSIG(status));
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
1997-05-26 10:16:31 -07:00
|
|
|
Begin_root (st);
|
1998-10-26 11:19:32 -08:00
|
|
|
res = alloc_small(2, 0);
|
1997-05-26 10:16:31 -07:00
|
|
|
Field(res, 0) = Val_int(pid);
|
|
|
|
Field(res, 1) = st;
|
|
|
|
End_roots();
|
1995-05-08 08:18:32 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_wait(void)
|
1996-04-12 08:57:28 -07:00
|
|
|
{
|
|
|
|
int pid, status;
|
1996-07-23 01:12:41 -07:00
|
|
|
|
|
|
|
enter_blocking_section();
|
1996-04-12 08:57:28 -07:00
|
|
|
pid = wait(&status);
|
1996-07-23 01:12:41 -07:00
|
|
|
leave_blocking_section();
|
1996-04-12 08:57:28 -07:00
|
|
|
if (pid == -1) uerror("wait", Nothing);
|
|
|
|
return alloc_process_status(pid, status);
|
|
|
|
}
|
|
|
|
|
1996-06-25 02:55:26 -07:00
|
|
|
#if defined(HAS_WAITPID) || defined(HAS_WAIT4)
|
|
|
|
|
|
|
|
#ifndef HAS_WAITPID
|
|
|
|
#define waitpid(pid,status,opts) wait4(pid,status,opts,NULL)
|
|
|
|
#endif
|
1996-04-12 08:57:28 -07:00
|
|
|
|
|
|
|
static int wait_flag_table[] = {
|
|
|
|
WNOHANG, WUNTRACED
|
|
|
|
};
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_waitpid(value flags, value pid_req)
|
1996-04-12 08:57:28 -07:00
|
|
|
{
|
|
|
|
int pid, status;
|
|
|
|
|
1996-07-23 01:12:41 -07:00
|
|
|
enter_blocking_section();
|
1996-04-12 08:57:28 -07:00
|
|
|
pid = waitpid(Int_val(pid_req), &status,
|
|
|
|
convert_flag_list(flags, wait_flag_table));
|
1996-07-23 01:12:41 -07:00
|
|
|
leave_blocking_section();
|
1996-04-12 08:57:28 -07:00
|
|
|
if (pid == -1) uerror("waitpid", Nothing);
|
|
|
|
return alloc_process_status(pid, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_waitpid(value flags, value pid_req)
|
1997-09-02 05:55:01 -07:00
|
|
|
{ invalid_argument("waitpid not implemented"); }
|
1996-04-12 08:57:28 -07:00
|
|
|
|
|
|
|
#endif
|