3 ** Copyright 2008, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
23 #include <sys/types.h>
32 #include <private/android_filesystem_config.h>
35 * SU can be given a specific command to exec. UID _must_ be
36 * specified for this (ie argc => 3).
42 int main(int argc, char **argv)
50 pw = getpwnam(argv[1]);
53 uid = gid = atoi(argv[1]);
60 /* Until we have something better, only root and the shell can use su. */
62 if (myuid != AID_ROOT && myuid != AID_SHELL) {
63 fprintf(stderr,"su: uid %d not allowed to su\n", myuid);
67 if(setgid(gid) || setuid(uid)) {
68 fprintf(stderr,"su: permission denied\n");
72 /* User specified command for exec. */
74 if (execlp(argv[2], argv[2], NULL) < 0) {
75 fprintf(stderr, "su: exec failed for %s Error:%s\n", argv[2],
79 } else if (argc > 3) {
80 /* Copy the rest of the args from main. */
81 char *exec_args[argc - 1];
82 memset(exec_args, 0, sizeof(exec_args));
83 memcpy(exec_args, &argv[2], sizeof(exec_args));
84 if (execvp(argv[2], exec_args) < 0) {
85 fprintf(stderr, "su: exec failed for %s Error:%s\n", argv[2],
91 /* Default exec shell. */
92 execlp("/system/bin/sh", "sh", NULL);
94 fprintf(stderr, "su: exec failed\n");