File: wait.c

package info (click to toggle)
lua-posix 36.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,720 kB
  • sloc: ansic: 5,462; makefile: 21; sh: 6
file content (117 lines) | stat: -rw-r--r-- 2,702 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * POSIX library for Lua 5.1, 5.2, 5.3 & 5.4.
 * Copyright (C) 2013-2025 Gary V. Vaughan
 * Copyright (C) 2010-2013 Reuben Thomas <rrt@sc3d.org>
 * Copyright (C) 2008-2010 Natanael Copa <natanael.copa@gmail.com>
 * Clean up and bug fixes by Leo Razoumov <slonik.az@gmail.com> 2006-10-11
 * Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> 07 Apr 2006 23:17:49
 * Based on original by Claudio Terra for Lua 3.x.
 * With contributions by Roberto Ierusalimschy.
 * With documentation from Steve Donovan 2012
 */
/***
 Wait for Process Termination.

@module posix.sys.wait
*/

#include <sys/wait.h>

#include "_helpers.c"


/***
Wait for child process to terminate.
@function wait
@int[opt=-1] pid child process id to wait for, or -1 for any child process
@int[opt] options bitwise OR of `WNOHANG` and `WUNTRACED`
@treturn[1] int pid of running child, if not exited yet and called with `WNOHANG`
@treturn[1] string "running"
@treturn[2] int pid of terminated child, if successful
@treturn[2] string "exited", "killed" or "stopped"
@treturn[2] int exit status, or signal number responsible for "killed" or "stopped"
@return[3] nil
@treturn[3] string error message
@treturn[3] int errnum
@see waitpid(2)
@see posix.unistd.fork
*/
static int
Pwait(lua_State *L)
{
	int status = 0;
	pid_t pid = (pid_t)optinteger(L, 1, -1);
	int options = optint(L, 2, 0);
	checknargs(L, 2);

	pid = waitpid(pid, &status, options);
	if (pid == -1)
		return pusherror(L, NULL);
	lua_pushinteger(L, pid);
	if (pid == 0)
	{
		lua_pushliteral(L,"running");
		return 2;
	}
	else if (WIFEXITED(status))
	{
		lua_pushliteral(L,"exited");
		lua_pushinteger(L, WEXITSTATUS(status));
		return 3;
	}
	else if (WIFSIGNALED(status))
	{
		lua_pushliteral(L,"killed");
		lua_pushinteger(L, WTERMSIG(status));
		return 3;
	}
	else if (WIFSTOPPED(status))
	{
		lua_pushliteral(L,"stopped");
		lua_pushinteger(L, WSTOPSIG(status));
		return 3;
	}
	return 1;
}


static const luaL_Reg posix_sys_wait_fns[] =
{
	LPOSIX_FUNC( Pwait		),
	{NULL, NULL}
};


/***
Constants.
@section constants
*/

/***
Wait constants.
Any constants not available in the underlying system will be `nil` valued.
@table posix.sys.wait
@int WNOHANG don't block waiting
@int WUNTRACED report status of stopped children
@usage
  -- Print wait constants supported on this host.
  for name, value in pairs (require "posix.sys.wait") do
    if type (value) == "number" then
      print (name, value)
     end
  end
*/

LUALIB_API int
luaopen_posix_sys_wait(lua_State *L)
{
	luaL_newlib(L, posix_sys_wait_fns);

	lua_pushstring(L, LPOSIX_VERSION_STRING("sys.wait"));
	lua_setfield(L, -2, "version");

	LPOSIX_CONST( WNOHANG		);
	LPOSIX_CONST( WUNTRACED		);

	return 1;
}