2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
20 /***************************************************************************
21 * Copyright (C) 2008 by Intra2net AG - Thomas Jarosch *
22 ***************************************************************************/
23 #include <sys/types.h>
31 #include "daemonfunc.hpp"
32 #include "stringfunc.hxx"
33 #include "filefunc.hxx"
44 * Fork into the background.
52 throw runtime_error("fork() failed");
59 // pid==0 -> child process: continue
63 * Drop root privileges
64 * @param username User to become. Don't change user if empty
65 * @param group Group to become. Don't change group if empty
66 * @param get_group_from_user Get group GID from user information if group is empty.
67 * @return true if all is fine, false otherwise
69 bool drop_root_privileges(const std::string &username,
70 const std::string &group, bool get_group_from_user)
74 Group my_group(group);
75 if (!my_group.is_valid())
78 if (setgid((my_group.Gid)))
82 if (!username.empty())
84 User my_user(username);
85 if (!my_user.is_valid())
88 if (get_group_from_user && group.empty())
90 if (setgid((my_user.Gid)))
94 // Initialize additional groups the user is a member of
95 if (initgroups(username.c_str(), getgid()) == -1)
98 if (setuid(my_user.Uid))
105 static const char *const EXE_DELETED_MARKER = " (deleted)";
107 /** @brief Remove the deletion marker from a binary name, if applicable.
109 * @param exe Binary name as obtained from ``/proc/$PID/exe``.
111 * @return The input binary name with a trailing `` (deleted)`` removed.
116 * […] If the pathname has been unlinked, the symbolic link will
117 * contain the string '(deleted)' appended to the original pathname.
119 * Which introduces an ambiguity if the path of the actual binary ends in
122 static inline std::string
123 strip_deleted_marker (const std::string &exe)
125 if (exe.size () <= sizeof (*EXE_DELETED_MARKER)) {
126 /* This binary name can’t possibly contain a deleted marker. */
130 const size_t found = exe.rfind (EXE_DELETED_MARKER);
131 if (found == std::string::npos) {
132 /* Input does not contain the deleted marker. */
136 return exe.substr (0, found);
140 * @brief determine the pids for a given program
141 * @param[in] name name (or full path) of the binary
142 * @param[out] result the pids associated with the name.
143 * @return @a true if the function performed without errors.
145 * Walk though the /proc/\<pid\>'s and search for the name.
147 * @note Since this function uses /proc, it's system specific. Currently:
150 * @todo check cmdline and stat in /proc/\<pid\> dir for the searched name.
152 bool pid_of(const std::string& name, std::vector< pid_t >& result)
154 std::vector< std::string > entries;
155 std::vector< pid_t > fuzz1_result;
156 std::vector< pid_t > fuzz2_result;
159 if (name.empty()) return false;
160 if (!get_dir("/proc", entries)) return false;
162 for (std::vector< std::string >::const_iterator it= entries.begin();
167 if (! string_to<pid_t>(*it, pid)) continue;
168 std::string base_path= std::string("/proc/") + *it;
169 std::string exe_path= base_path + "/exe";
170 I2n::Stat stat(exe_path, false);
171 if (not stat or not stat.is_link()) continue;
172 std::string real_exe= read_link(exe_path);
173 if (!real_exe.empty())
175 // we got the path of the exe
176 if (real_exe == name || strip_deleted_marker (real_exe) == name)
178 result.push_back( pid );
182 std::string proc_stat= read_file( base_path + "/stat");
183 if (proc_stat.empty()) continue; // process vanished
185 //TODO some more fuzz tests here?! (cmdline, stat(us))
187 real_exe = basename (real_exe);
188 if (real_exe == name || strip_deleted_marker (real_exe) == name)
190 fuzz2_result.push_back(pid);
196 // we haven't got the path of the exe
197 // this can happen e.g. with processes owned by other users
198 // -> parse the commandline instead
199 std::string cmdline = read_file(base_path + "/cmdline");
200 if (cmdline.empty()) continue;
202 // in /proc/*/cmdline, the parameters are split by nullbytes, we only care for the first parameter
203 if (cmdline.find('\0') != string::npos)
204 cmdline.erase(cmdline.find('\0'));
208 result.push_back( pid );
212 if (basename(cmdline) == name)
214 fuzz2_result.push_back(pid);
221 result.swap(fuzz1_result);
225 result.swap(fuzz2_result);
228 } // eo pidOf(const std::string&,std::vector< pid_t >&)