-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathTeeJee.Process.vala
More file actions
508 lines (408 loc) · 13.8 KB
/
TeeJee.Process.vala
File metadata and controls
508 lines (408 loc) · 13.8 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* TeeJee.ProcessHelper.vala
*
* Copyright 2012-2018 Tony George <teejeetech@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
namespace TeeJee.ProcessHelper{
using TeeJee.Logging;
using TeeJee.FileSystem;
using TeeJee.Misc;
public string TEMP_DIR;
/* Convenience functions for executing commands and managing processes */
// execute process ---------------------------------
public static void init_tmp(){
// a list of folders where temp files could be stored
string[] tempPlaces = {
Environment.get_tmp_dir(), // system temp dir
"/var/tmp", // another system temp dir, if the first one failed, this one is likely to fail too
Environment.get_home_dir() + "/.temp", // user temp dir
"/dev/shm", // shared memory
};
foreach (string tempPlace in tempPlaces) {
string std_out, std_err;
TEMP_DIR = tempPlace + "/timeshift-" + random_string();
dir_create(TEMP_DIR);
Posix.chmod(TEMP_DIR, 0750);
exec_script_sync("echo 'ok'",out std_out,out std_err, true);
if ((std_out == null) || (std_out.strip() != "ok")){
// this dir does not work for some reason - probably no disk space
dir_delete(TEMP_DIR);
} else {
// script worked - we have found a tempdir to use
return;
}
}
stderr.printf("No usable temp directory was found!\n");
}
public int exec_sync (string cmd, out string? std_out = null, out string? std_err = null){
/* Executes single command synchronously.
* Pipes and multiple commands are not supported.
* std_out, std_err can be null. Output will be written to terminal if null. */
try {
int status;
Process.spawn_command_line_sync(cmd, out std_out, out std_err, out status);
return status;
}
catch (Error e){
log_error (e.message);
return -1;
}
}
public int exec_script_sync (string script,
out string? std_out = null, out string? std_err = null,
bool supress_errors = false, bool run_as_admin = false,
bool cleanup_tmp = true, bool print_to_terminal = false){
/* Executes commands synchronously.
* Pipes and multiple commands are fully supported.
* Commands are written to a temporary bash script and executed.
* std_out, std_err can be null. Output will be written to terminal if null.
* */
string? sh_file = save_bash_script_temp(script, null, true, supress_errors);
if (sh_file == null) {
// saving the script failed
return -1;
}
string sh_file_admin = "";
if (run_as_admin){
var script_admin = "#!/usr/bin/env bash\n";
script_admin += "pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY";
script_admin += " '%s'".printf(escape_single_quote(sh_file));
sh_file_admin = GLib.Path.build_filename(file_parent(sh_file),"script-admin.sh");
save_bash_script_temp(script_admin, sh_file_admin, true, supress_errors);
}
try {
string[] argv = new string[1];
if (run_as_admin){
argv[0] = sh_file_admin;
}
else{
argv[0] = sh_file;
}
string[] env = Environ.get();
int exit_code;
if (print_to_terminal){
Process.spawn_sync (
TEMP_DIR, //working dir
argv, //argv
env, //environment
SpawnFlags.SEARCH_PATH,
null, // child_setup
null,
null,
out exit_code
);
}
else{
Process.spawn_sync (
TEMP_DIR, //working dir
argv, //argv
env, //environment
SpawnFlags.SEARCH_PATH,
null, // child_setup
out std_out,
out std_err,
out exit_code
);
}
if (cleanup_tmp){
file_delete(sh_file);
if (run_as_admin){
file_delete(sh_file_admin);
}
}
return exit_code;
}
catch (Error e){
if (!supress_errors){
log_error (e.message);
}
return -1;
}
}
public int exec_script_async (string script){
/* Executes commands synchronously.
* Pipes and multiple commands are fully supported.
* Commands are written to a temporary bash script and executed.
* Return value indicates if script was started successfully.
* */
try {
string scriptfile = save_bash_script_temp (script);
string[] argv = new string[1];
argv[0] = scriptfile;
string[] env = Environ.get();
Pid child_pid;
Process.spawn_async_with_pipes(
TEMP_DIR, //working dir
argv, //argv
env, //environment
SpawnFlags.SEARCH_PATH,
null,
out child_pid);
return 0;
}
catch (Error e){
log_error (e.message);
return 1;
}
}
/**
executes a command as the "normal" unprivileged user async
may execute the command as root if the user could not be determined or the name could not be resolved
*/
public static int exec_user_async(string command) {
// find correct user
int uid = TeeJee.System.get_user_id();
string cmd = command;
if(uid > 0) {
// non root
string? user = TeeJee.System.get_username_from_uid(uid);
if(user != null) {
cmd = "pkexec --user %s env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS ".printf(user) + cmd;
}
}
log_debug(cmd);
return TeeJee.ProcessHelper.exec_script_async(cmd);
}
public string? save_bash_script_temp (string commands, string? script_path = null,
bool force_locale = true, bool supress_errors = false){
string sh_path = script_path;
/* Creates a temporary bash script with given commands
* Returns the script file path */
var script = new StringBuilder();
script.append ("#!/usr/bin/env bash\n");
script.append ("\n");
if (force_locale){
script.append("LANG=C\n");
script.append("LC_ALL=C.UTF-8\n");
}
script.append ("\n");
script.append ("%s\n".printf(commands));
script.append ("\n\nexitCode=$?\n");
script.append ("echo ${exitCode} > status\n");
if ((sh_path == null) || (sh_path.length == 0)){
sh_path = get_temp_file_path();
}
try{
//write script file
var file = File.new_for_path (sh_path);
if (file.query_exists ()) {
file.delete ();
}
var file_stream = file.create (FileCreateFlags.REPLACE_DESTINATION);
var data_stream = new DataOutputStream (file_stream);
data_stream.put_string (script.str);
data_stream.close();
// set execute permission
Posix.chmod (sh_path, 0744);
return sh_path;
}
catch (Error e) {
if (!supress_errors){
log_error (e.message);
}
}
return null;
}
public string get_temp_file_path(){
/* Generates temporary file path */
return TEMP_DIR + "/" + timestamp_numeric() + (new Rand()).next_int().to_string();
}
// find process -------------------------------
public static bool cmd_exists(string cmd_tool){
string? path = Environment.find_program_in_path(cmd_tool);
return (path != null) && (path.length > 0);
}
// return the name of the executable of a given pid or self if pid is <= 0
// returns an empty string on error or if the pid could not be found
public string get_process_exe_name(long pid = -1){
string pidStr = (pid <= 0 ? "self" : pid.to_string());
string path = "/proc/%s/exe".printf(pidStr);
string link;
try {
link = GLib.FileUtils.read_link(path);
} catch (Error e) {
return "";
}
return GLib.Path.get_basename(link);
}
// get the parent pid of process or self
public Pid get_process_parent(Pid process = -1) {
string pidStr = (process <= 0 ? "self" : process.to_string());
string path = "/proc/%s/stat".printf(pidStr);
string stats = file_read(path);
string details = stats.split(")", 2)[1];
string[] splitted = details.split(" ", 3);
if(splitted.length == 3) {
return int.parse(splitted[2]);
}
log_debug("can not parse process stat %s".printf(stats));
return -1;
}
// get the effective user pid of an process
// returns -1 on error
public int get_euid_of_process(Pid process) {
GLib.File file = GLib.File.new_for_path("/proc/%d".printf(process));
try {
GLib.FileInfo info = file.query_info(FileAttribute.UNIX_UID, GLib.FileQueryInfoFlags.NONE);
return (int) info.get_attribute_uint32(FileAttribute.UNIX_UID);
} catch(GLib.Error e) {
log_debug("failed to fetch user of process %i %s".printf(process, e.message));
}
return -1;
}
// find the first parent process, that is owned by the user and not root
public Pid get_user_process() {
Pid ppid = -1;
int targetUser = TeeJee.System.get_user_id();
int user = 0;
do {
ppid = get_process_parent(ppid);
user = get_euid_of_process (ppid);
} while(user != targetUser && ppid > 1);
if(user == targetUser) {
return ppid;
}
return -1;
}
// get the env of an process
public string[]? get_process_env(Pid pid) {
if(pid < 1) {
return null;
}
return file_read_array("/proc/%i/environ".printf(pid), '\0');
}
// get the value of name in env if it exists or return default_value
public string? get_env(string[] env, string name, string? default_value = null) {
foreach(string env_var in env) {
string[] splitted = env_var.split("=", 2);
if(splitted[0] == name) {
if (splitted.length == 2) {
return splitted[1];
}
}
}
return default_value;
}
public Pid[] get_process_children (Pid parent_pid){
/* Returns the list of child processes owned by a given process
This does not contain grand children
*/
// no explicit check for the existence of /proc/ as this might be a time-of-check-time-of-use bug.
File procfs = File.new_for_path("/proc/");
try {
FileEnumerator enumerator = procfs.enumerate_children(FileAttribute.STANDARD_NAME, FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
FileInfo info;
Pid[] childList = {};
while ((info = enumerator.next_file()) != null) {
if(info.get_file_type() != FileType.DIRECTORY) {
// only interested in directories
continue;
}
string name = info.get_name();
uint64 pid;
if(!uint64.try_parse(name, out pid)) {
// make sure to not access any other directories that may be present in /proc for some reason
continue;
}
string? fileCont = file_read("/proc/%s/stat".printf(name));
if(fileCont == null) {
// stat file of pid might not be readable (because of permissions or the process died since we got its pid)
continue;
}
// the format of the stat file is documented in man 5 proc
// it begging is: pid (comm) status ppid ...
// the process name could contain a space or ) and confuse the parsing.
// so we make sure to take the last ) and only parse the stuff after that.
int index = fileCont.last_index_of_char(')');
string parseline = fileCont.substring(index);
string[] split = parseline.split(" ", 4); // we are not interested in the part after ppid so just leave it a big string
if(split.length != 4) {
// format of stat file is not matching - should never happen
log_error("can not parse state of %ld".printf((long) pid));
continue;
}
uint64 ppid = uint64.parse(split[2]);
if(ppid != 0 && ppid == parent_pid) {
// the process is a child of the target parent process
childList += (Pid) pid;
}
}
return childList;
} catch (Error e) {
log_error(e.message);
log_error("Failed to get child processes of %ld".printf(parent_pid));
}
return {};
}
// manage process ---------------------------------
public void process_quit(Pid process_pid, bool killChildren = true){
/* Terminates specified process and its children (optional).
* Sends signal SIGTERM to the process to allow it to quit gracefully.
* */
process_send_signal(process_pid, Posix.Signal.TERM, killChildren);
}
public void process_kill(Pid process_pid, bool killChildren = true) {
/* Kills specified process and its children (optional).
* Sends signal SIGKILL to the process to kill it forcefully.
* It is recommended to use the function process_quit() instead.
* */
process_send_signal(process_pid, Posix.Signal.KILL, killChildren);
}
public void process_send_signal(Pid process_pid, Posix.Signal sig, bool children = true) {
/* Sends a signal to a process and its children (optional). */
// get the childs before sending the signal, as the childs might not be accessible afterwards
Pid[] child_pids = get_process_children (process_pid);
Posix.kill (process_pid, sig);
if (children){
foreach (Pid pid in child_pids){
Posix.kill (pid, sig);
}
}
}
// process priority ---------------------------------------
public void process_set_priority (Pid procID, int prio){
/* Set process priority */
if (Posix.getpriority (Posix.PRIO_PROCESS, procID) != prio)
Posix.setpriority (Posix.PRIO_PROCESS, procID, prio);
}
public int process_get_priority (Pid procID){
/* Get process priority */
return Posix.getpriority (Posix.PRIO_PROCESS, procID);
}
public void process_set_priority_normal (Pid procID){
/* Set normal priority for process */
process_set_priority (procID, 0);
}
public void process_set_priority_low (Pid procID){
/* Set low priority for process */
process_set_priority (procID, 5);
}
/*
Wrapper for the ioprio_set syscall (has no libc wrapper)
see: man 2 ioprio_set
pid may be 0 (self)
prio shall be constructed using IoPrio.prioValue
returns true on success
sets Posix.errno on error
*/
public static bool ioprio_set(Pid pid, int prio) {
return 0 == IoPrio.syscall(IoPrio.SYS_ioprio_set, IoPrio.IOPRIO_WHO_PROCESS, pid, prio);
}
}