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 */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* Automatique. Distributed only by permission. */
|
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $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))
|
1996-04-12 08:57:28 -07:00
|
|
|
#define WIFEXITED(status) ((status) & 0xFF == 0)
|
|
|
|
#define WEXITSTATUS(status) (((status) >> 8) & 0xFF)
|
|
|
|
#define WIFSTOPPED(status) ((status) & 0xFF == 0xFF)
|
|
|
|
#define WSTOPSIG(status) (((status) >> 8) & 0xFF)
|
|
|
|
#define WTERMSIG(status) ((status) & 0x3F)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static value alloc_process_status(pid, status)
|
|
|
|
int pid, status;
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
1996-04-12 08:57:28 -07:00
|
|
|
value st, res;
|
|
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
1995-05-08 08:18:32 -07:00
|
|
|
st = alloc(1, 0);
|
1996-04-12 08:57:28 -07:00
|
|
|
Field(st, 0) = Val_int(WEXITSTATUS(status));
|
|
|
|
}
|
|
|
|
else if (WIFSTOPPED(status)) {
|
1995-05-08 08:18:32 -07:00
|
|
|
st = alloc(1, 2);
|
1996-04-12 08:57:28 -07:00
|
|
|
Field(st, 0) = Val_int(WSTOPSIG(status));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
st = alloc(1, 1);
|
|
|
|
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);
|
|
|
|
res = alloc_tuple(2);
|
|
|
|
Field(res, 0) = Val_int(pid);
|
|
|
|
Field(res, 1) = st;
|
|
|
|
End_roots();
|
1995-05-08 08:18:32 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1996-04-12 08:57:28 -07:00
|
|
|
value unix_wait() /* ML */
|
|
|
|
{
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
value unix_waitpid(flags, pid_req)
|
|
|
|
value flags, pid_req;
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
|
value unix_waitpid() { invalid_argument("waitpid not implemented"); }
|
|
|
|
|
|
|
|
#endif
|