~kvik/lu9-p9

ref: c85dc2c7a75a78c497c55ce5ad276f8f9fa04e17 lu9-p9/base/proc.c -rw-r--r-- 3.6 KiB
c85dc2c7kvik misc: use usize instead of lua_Unsigned 1 year, 3 months ago
                                                                                
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
static int
p9_abort(lua_State*)
{
	abort();
	/* never */ return 0;
}

static int
p9_exits(lua_State *L)
{
	exits(luaL_optstring(L, 1, nil));
	/* never */ return 0;
}

static int
p9_fatal(lua_State *L)
{
	if(lua_gettop(L) > 1
	&& lua_getglobal(L, "string") == LUA_TTABLE
	&& lua_getfield(L, -1, "format") == LUA_TFUNCTION){
		lua_remove(L, -2);
		lua_insert(L, 1);
		if(lua_pcall(L, lua_gettop(L) - 1, 1, 0) == LUA_OK)
			sysfatal(lua_tostring(L, -1));
	}
	sysfatal(luaL_optstring(L, 1, "fatal"));
	/* never */ return 0;
}

static int
p9_sleep(lua_State *L)
{
	long t;
	
	t = luaL_checkinteger(L, 1);
	lua_pushboolean(L,
		sleep(t) == -1 ? 0 : 1
	);
	return 1;
}

static int
p9_alarm(lua_State *L)
{
	long t, rem;
	
	t = luaL_checkinteger(L, 1);
	rem = alarm(t);
	lua_pushinteger(L, rem);
	return 1;
}

static int
p9_wdir(lua_State *L)
{
	const char *path;
	char *buf;
	luaL_Buffer b;
	
	path = luaL_optstring(L, 1, nil);
	if(path != nil){
		if(chdir(path) == -1)
			return error(L, "chdir: %r");
		lua_pushboolean(L, 1);
		return 1;
	}
	luaL_buffinitsize(L, &b, Iosize);
	buf = luaL_buffaddr(&b);
	if(getwd(buf, Iosize) == nil)
		return error(L, "getwd: %r");
	luaL_pushresultsize(&b, strlen(buf));
	return 1;
}

static int
p9_user(lua_State *L)
{
	lua_pushstring(L, getuser());
	return 1;
}

static int
p9_sysname(lua_State *L)
{
	lua_pushstring(L, sysname());
	return 1;
}

static int
p9_pid(lua_State *L)
{
	lua_pushinteger(L, getpid());
	return 1;
}

static int
p9_ppid(lua_State *L)
{
	lua_pushinteger(L, getppid());
	return 1;
}

static int
p9_rfork(lua_State *L)
{
	int flags, i, n, r;
	char *f[12];
	
	flags = RFENVG|RFNAMEG|RFNOTEG;
	n = getfields(luaL_optstring(L, 1, ""), f, sizeof f, 0, " \t\n");
	if(n > 0) for(flags = 0, i = 0; i < n; i++){
		if     (strcmp(f[i], "name") == 0)
			flags |= RFNAMEG;
		else if(strcmp(f[i], "cname") == 0)
			flags |= RFCNAMEG;
		else if(strcmp(f[i], "env") == 0)
			flags |= RFENVG;
		else if(strcmp(f[i], "cenv") == 0)
			flags |= RFCENVG;
		else if(strcmp(f[i], "note") == 0)
			flags |= RFNOTEG;
		else if(strcmp(f[i], "fd") == 0)
			flags |= RFFDG;
		else if(strcmp(f[i], "cfd") == 0)
			flags |= RFCFDG;
		else if(strcmp(f[i], "nomnt") == 0)
			flags |= RFNOMNT;
		else if(strcmp(f[i], "proc") == 0)
			flags |= RFPROC;
		else if(strcmp(f[i], "nowait") == 0)
			flags |= RFNOWAIT;
		else if(strcmp(f[i], "rend") == 0)
			flags |= RFREND;
		else if(strcmp(f[i], "mem") == 0)
			flags |= RFMEM;
		else
			return luaL_error(L, "unknown rfork flag '%s'", f[i]);
	}
	if((r = rfork(flags)) == -1)
		return error(L, "rfork %r");
	lua_pushinteger(L, r);
	return 1;
}

static int
p9_exec(lua_State *L)
{
	int argc, i;
	const char **argv, *p;
	char buf[Smallbuf];
	
	argc = lua_gettop(L);
	if(argc < 1)
		luaL_argerror(L, 1, "string arguments expected");
	argv = Lmalloc(L, (argc+1) * sizeof(char*));
	for(i = 1; i <= argc; i++)
		argv[i-1] = luaL_checkstring(L, i);
	argv[argc] = nil;
	p = argv[0];
	if(p[0] != '/' && (p[0] != '.' && p[1] != '/')){
		snprint(buf, sizeof buf, "/bin/%s", argv[0]);
		argv[0] = buf;
	}
	exec(argv[0], argv);
	Lfree(L, argv);
	return error(L, "exec: %r");
}

static int
p9_wait(lua_State *L)
{
	Waitmsg *w;
	
	w = wait();
	if(w == nil)
		return error(L, "wait: %r");
	lua_pushboolean(L, w->msg[0] == 0 ? 1 : 0);
	lua_createtable(L, 0, 3);
		lua_pushinteger(L, w->pid);
			lua_setfield(L, -2, "pid");
		lua_pushstring(L, w->msg);
			lua_setfield(L, -2, "status");
		lua_createtable(L, 3, 0);
			lua_pushinteger(L, w->time[0]);
				lua_setfield(L, -2, "user");
			lua_pushinteger(L, w->time[1]);
				lua_setfield(L, -2, "system");
			lua_pushinteger(L, w->time[2]);
				lua_setfield(L, -2, "real");
		lua_setfield(L, -2, "time");
	free(w);
	return 2;
}