-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvterm_exec.c
More file actions
99 lines (79 loc) · 1.96 KB
/
Copy pathvterm_exec.c
File metadata and controls
99 lines (79 loc) · 1.96 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
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include "vterm.h"
#include "vterm_private.h"
#include "vterm_exec.h"
#include "stringv.h"
void
vterm_set_exec(vterm_t *vterm, char *path, char **exec_argv)
{
if(vterm == NULL) return;
if(path == NULL) return;
if(exec_argv == NULL) return;
if(vterm->exec_path != NULL)
{
free(path);
path = NULL;
}
if(vterm->exec_argv != NULL)
{
strfreev(vterm->exec_argv);
vterm->exec_argv = NULL;
}
vterm->exec_path = strdup(path);
vterm->exec_argv = strdupv(exec_argv, -1);
return;
}
/*
child-side only (post-forkpty). when VTERM_FLAG_START_HOME is set,
move into the user's home before exec so the shell or alternate
binary does not inherit the host process cwd.
*/
static void
_vterm_chdir_home(void)
{
const char *home;
struct passwd *pw;
home = getenv("HOME");
if(home == NULL || home[0] == '\0')
{
pw = getpwuid(getuid());
if(pw != NULL) home = pw->pw_dir;
}
if(home != NULL && home[0] != '\0')
{
if(chdir(home) == -1)
return;
}
return;
}
int
vterm_exec_binary(vterm_t *vterm)
{
struct passwd *user_profile;
char *user_shell = NULL;
if(vterm->flags & VTERM_FLAG_START_HOME)
_vterm_chdir_home();
if(vterm->exec_path == NULL)
{
user_profile = getpwuid(getuid());
if(user_profile == NULL) user_shell = "/bin/sh";
else if(user_profile->pw_shell == NULL) user_shell = "/bin/sh";
else user_shell = user_profile->pw_shell;
if(user_shell == NULL) user_shell="/bin/sh";
// start the shell
if(execl(user_shell, user_shell, NULL) == -1)
{
return -1;
}
return 0;
}
// caller provided an alternate binary to run
if(execv(vterm->exec_path, vterm->exec_argv) == -1)
{
return -1;
}
return 0;
}