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
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.
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.
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
25 #if defined(HAVE_LIBUNWIND)
26 #include <libunwind-ptrace.h>
27 #endif /* defined(HAVE_LIBUNWIND) */
31 #include <sys/types.h>
44 change_uid(const char * command)
46 uid_t run_uid, run_euid;
47 gid_t run_gid, run_egid;
52 if (getuid() != 0 || geteuid() != 0) {
54 "you must be root to use the -u option\n");
57 if ((pent = getpwnam(options.user)) == NULL) {
58 fprintf(stderr, "cannot find user `%s'\n", options.user);
61 run_uid = pent->pw_uid;
62 run_gid = pent->pw_gid;
64 if (initgroups(options.user, run_gid) < 0) {
65 perror("ltrace: initgroups");
72 if (options.user || !geteuid()) {
77 if (!stat(command, &statbuf)) {
78 if (statbuf.st_mode & S_ISUID) {
79 run_euid = statbuf.st_uid;
81 if (statbuf.st_mode & S_ISGID) {
82 run_egid = statbuf.st_gid;
85 if (setregid(run_gid, run_egid) < 0) {
86 perror("ltrace: setregid");
89 if (setreuid(run_uid, run_euid) < 0) {
90 perror("ltrace: setreuid");
97 execute_program(const char * command, char **argv)
101 debug(1, "Executing `%s'...", command);
106 perror("ltrace: fork");
108 } else if (!pid) { /* child */
111 execvp(command, argv);
112 fprintf(stderr, "Can't execute `%s': %s\n", command,
117 if (wait_for_proc(pid) < 0)
120 debug(1, "PID=%d", pid);