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>
|
|
|
|
#include "unix.h"
|
|
|
|
|
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;
|
1995-05-08 08:18:32 -07:00
|
|
|
Push_roots(r, 1);
|
1996-04-12 08:57:28 -07:00
|
|
|
|
|
|
|
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
|
|
|
}
|
1996-04-12 08:57:28 -07:00
|
|
|
r[0] = st;
|
1995-05-08 08:18:32 -07:00
|
|
|
res = alloc_tuple(2);
|
|
|
|
Field(res, 0) = Val_int(pid);
|
1996-04-12 08:57:28 -07:00
|
|
|
Field(res, 1) = r[0];
|
1995-05-08 08:18:32 -07:00
|
|
|
Pop_roots();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1996-04-12 08:57:28 -07:00
|
|
|
value unix_wait() /* ML */
|
|
|
|
{
|
|
|
|
int pid, status;
|
|
|
|
Push_roots(r, 1);
|
|
|
|
pid = wait(&status);
|
|
|
|
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;
|
|
|
|
|
|
|
|
pid = waitpid(Int_val(pid_req), &status,
|
|
|
|
convert_flag_list(flags, wait_flag_table));
|
|
|
|
if (pid == -1) uerror("waitpid", Nothing);
|
|
|
|
return alloc_process_status(pid, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
value unix_waitpid() { invalid_argument("waitpid not implemented"); }
|
|
|
|
|
|
|
|
#endif
|