Skip to content

Commit 4864c11

Browse files
yujincheng08topjohnwu
authored andcommitted
no pty for -c by default, and add -i to force pty
1 parent 9ddeab0 commit 4864c11

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

native/src/core/su/su.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ int quit_signals[] = { SIGALRM, SIGABRT, SIGHUP, SIGPIPE, SIGQUIT, SIGTERM, SIGI
3939
"Usage: su [options] [-] [user [argument...]]\n\n"
4040
"Options:\n"
4141
" -c, --command COMMAND Pass COMMAND to the invoked shell\n"
42+
" -i, --interactive Force pseudo-terminal allocation when using -c\n"
4243
" -g, --group GROUP Specify the primary group\n"
43-
" -G, --supp-group GROUP Specify a supplementary group.\n"
44+
" -G, --supp-group GROUP Specify a supplementary group\n"
4445
" The first specified supplementary group is also used\n"
45-
" as a primary group if the option -g is not specified.\n"
46+
" as a primary group if the option -g is not specified\n"
4647
" -Z, --context CONTEXT Change SELinux context\n"
4748
" -t, --target PID PID to take mount namespace from\n"
4849
" -h, --help Display this help message and exit\n"
@@ -103,6 +104,7 @@ int su_client_main(int argc, char *argv[]) {
103104
{ "target", required_argument, nullptr, 't' },
104105
{ "group", required_argument, nullptr, 'g' },
105106
{ "supp-group", required_argument, nullptr, 'G' },
107+
{ "interactive", no_argument, nullptr, 'i' },
106108
{ nullptr, 0, nullptr, 0 },
107109
};
108110

@@ -117,7 +119,9 @@ int su_client_main(int argc, char *argv[]) {
117119
strcpy(argv[i], "-M");
118120
}
119121

120-
while ((c = getopt_long(argc, argv, "c:hlmps:VvuZ:Mt:g:G:", long_opts, nullptr)) != -1) {
122+
bool interactive = false;
123+
124+
while ((c = getopt_long(argc, argv, "c:hlimps:VvuZ:Mt:g:G:", long_opts, nullptr)) != -1) {
121125
switch (c) {
122126
case 'c': {
123127
string command;
@@ -132,6 +136,9 @@ int su_client_main(int argc, char *argv[]) {
132136
}
133137
case 'h':
134138
usage(EXIT_SUCCESS);
139+
case 'i':
140+
interactive = true;
141+
break;
135142
case 'l':
136143
req.login = true;
137144
break;
@@ -217,10 +224,11 @@ int su_client_main(int argc, char *argv[]) {
217224
}
218225

219226
// Determine which one of our streams are attached to a TTY
227+
interactive |= req.command.empty();
220228
int atty = 0;
221-
if (isatty(STDIN_FILENO)) atty |= ATTY_IN;
222-
if (isatty(STDOUT_FILENO)) atty |= ATTY_OUT;
223-
if (isatty(STDERR_FILENO)) atty |= ATTY_ERR;
229+
if (isatty(STDIN_FILENO) && interactive) atty |= ATTY_IN;
230+
if (isatty(STDOUT_FILENO) && interactive) atty |= ATTY_OUT;
231+
if (isatty(STDERR_FILENO) && interactive) atty |= ATTY_ERR;
224232

225233
// Send stdin
226234
send_fd(fd, (atty & ATTY_IN) ? -1 : STDIN_FILENO);

0 commit comments

Comments
 (0)