Imported Upstream version 0.7.2
[platform/upstream/ltrace.git] / execute_program.c
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4  * Copyright (C) 2010 Joe Damato
5  * Copyright (C) 1998,1999,2003,2008,2009 Juan Cespedes
6  * Copyright (C) 2006 Ian Wienand
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23 #include "config.h"
24
25 #if defined(HAVE_LIBUNWIND)
26 #include <libunwind-ptrace.h>
27 #endif /* defined(HAVE_LIBUNWIND) */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <pwd.h>
37 #include <grp.h>
38
39 #include "backend.h"
40 #include "options.h"
41 #include "debug.h"
42
43 static void
44 change_uid(const char * command)
45 {
46         uid_t run_uid, run_euid;
47         gid_t run_gid, run_egid;
48
49         if (options.user) {
50                 struct passwd *pent;
51
52                 if (getuid() != 0 || geteuid() != 0) {
53                         fprintf(stderr,
54                                 "you must be root to use the -u option\n");
55                         exit(1);
56                 }
57                 if ((pent = getpwnam(options.user)) == NULL) {
58                         fprintf(stderr, "cannot find user `%s'\n", options.user);
59                         exit(1);
60                 }
61                 run_uid = pent->pw_uid;
62                 run_gid = pent->pw_gid;
63
64                 if (initgroups(options.user, run_gid) < 0) {
65                         perror("ltrace: initgroups");
66                         exit(1);
67                 }
68         } else {
69                 run_uid = getuid();
70                 run_gid = getgid();
71         }
72         if (options.user || !geteuid()) {
73                 struct stat statbuf;
74                 run_euid = run_uid;
75                 run_egid = run_gid;
76
77                 if (!stat(command, &statbuf)) {
78                         if (statbuf.st_mode & S_ISUID) {
79                                 run_euid = statbuf.st_uid;
80                         }
81                         if (statbuf.st_mode & S_ISGID) {
82                                 run_egid = statbuf.st_gid;
83                         }
84                 }
85                 if (setregid(run_gid, run_egid) < 0) {
86                         perror("ltrace: setregid");
87                         exit(1);
88                 }
89                 if (setreuid(run_uid, run_euid) < 0) {
90                         perror("ltrace: setreuid");
91                         exit(1);
92                 }
93         }
94 }
95
96 pid_t
97 execute_program(const char * command, char **argv)
98 {
99         pid_t pid;
100
101         debug(1, "Executing `%s'...", command);
102
103         pid = fork();
104         if (pid < 0) {
105         fail:
106                 perror("ltrace: fork");
107                 exit(1);
108         } else if (!pid) {      /* child */
109                 change_uid(command);
110                 trace_me();
111                 execvp(command, argv);
112                 fprintf(stderr, "Can't execute `%s': %s\n", command,
113                         strerror(errno));
114                 _exit(1);
115         }
116
117         if (wait_for_proc(pid) < 0)
118                 goto fail;
119
120         debug(1, "PID=%d", pid);
121         return pid;
122 }