*/
void daemonize()
{
- int pid=fork();
-
- if (pid < 0)
- {
- throw runtime_error("fork() failed");
- }
- if (pid > 0)
- {
- // parent process
- exit (0);
- }
- // pid==0 -> child process: continue
+ int pid=fork();
+
+ if (pid < 0)
+ {
+ throw runtime_error("fork() failed");
+ }
+ if (pid > 0)
+ {
+ // parent process
+ exit (0);
+ }
+ // pid==0 -> child process: continue
}
/**
* Drop root privileges
* @param username User to become. Don't change user if empty
* @param group Group to become. Don't change group if empty
+ * @param get_group_from_user Get group GID from user information if group is empty.
+ * @return true if all is fine, false otherwise
*/
-void drop_root_privileges(const std::string &username,
- const std::string &group)
+bool drop_root_privileges(const std::string &username,
+ const std::string &group, bool get_group_from_user)
{
-/*
- if (!group.empty()) {
- if (setgid(daemon::lookup_gid(group))) {
- throw runtime_error("Can't change to group " + group);
- }
- }
-
- if (!username.empty()) {
- if (setuid(daemon::lookup_uid(username))) {
- throw runtime_error("Can't change to user " + username);
- }
- }
-*/
+ if (!group.empty())
+ {
+ Group my_group(group);
+ if (!my_group.is_valid())
+ return false;
+
+ if (setgid((my_group.Gid)))
+ return false;
+ }
+
+ if (!username.empty())
+ {
+ User my_user(username);
+ if (!my_user.is_valid())
+ return false;
+
+ if (get_group_from_user && group.empty())
+ {
+ if (setgid((my_user.Gid)))
+ return false;
+ }
+
+ if (setuid(my_user.Uid))
+ return false;
+ }
}
/**
*/
bool pid_of(const std::string& name, std::vector< pid_t >& result)
{
- std::vector< std::string > entries;
- std::vector< pid_t > fuzz1_result;
- std::vector< pid_t > fuzz2_result;
- result.clear();
- if (!get_dir("/proc", entries)) return false;
- for(std::vector< std::string >::const_iterator it= entries.begin();
- it != entries.end();
- ++it)
- {
- pid_t pid;
- if (! string_to<pid_t>(*it, pid)) continue;
- std::string base_path= std::string("/proc/") + *it;
- std::string exe_path= base_path + "/exe";
- I2n::Stat stat(exe_path, false);
- if (not stat or not stat.is_link()) continue;
- std::string real_exe= read_link(exe_path);
- if (real_exe == name)
- {
- result.push_back( pid );
- continue;
- }
-
- std::string proc_stat= read_file( base_path + "/stat");
- if (proc_stat.empty()) continue; // process vanished
-
- //TODO some more fuzz tests here?! (cmdline, stat(us))
-
- if (basename(real_exe) == name)
- {
- fuzz2_result.push_back(pid);
- continue;
- }
- }
- if (result.empty())
- {
- result.swap(fuzz1_result);
- }
- if (result.empty())
- {
- result.swap(fuzz2_result);
- }
- return true;
+ std::vector< std::string > entries;
+ std::vector< pid_t > fuzz1_result;
+ std::vector< pid_t > fuzz2_result;
+ result.clear();
+ if (!get_dir("/proc", entries)) return false;
+ for (std::vector< std::string >::const_iterator it= entries.begin();
+ it != entries.end();
+ ++it)
+ {
+ pid_t pid;
+ if (! string_to<pid_t>(*it, pid)) continue;
+ std::string base_path= std::string("/proc/") + *it;
+ std::string exe_path= base_path + "/exe";
+ I2n::Stat stat(exe_path, false);
+ if (not stat or not stat.is_link()) continue;
+ std::string real_exe= read_link(exe_path);
+ if (real_exe == name)
+ {
+ result.push_back( pid );
+ continue;
+ }
+
+ std::string proc_stat= read_file( base_path + "/stat");
+ if (proc_stat.empty()) continue; // process vanished
+
+ //TODO some more fuzz tests here?! (cmdline, stat(us))
+
+ if (basename(real_exe) == name)
+ {
+ fuzz2_result.push_back(pid);
+ continue;
+ }
+ }
+ if (result.empty())
+ {
+ result.swap(fuzz1_result);
+ }
+ if (result.empty())
+ {
+ result.swap(fuzz2_result);
+ }
+ return true;
} // eo pidOf(const std::string&,std::vector< pid_t >&)
}
PidFile pidfile(Filename);
bool rtn_write = pidfile.write();
- CPPUNIT_ASSERT_EQUAL(rtn_write, true);
+ CPPUNIT_ASSERT_EQUAL(true, rtn_write);
}
void AutoRemoval()
}
bool exists = I2n::file_exists(Filename);
- CPPUNIT_ASSERT_EQUAL(rtn_write, true);
- CPPUNIT_ASSERT_EQUAL(exists, false);
+ CPPUNIT_ASSERT_EQUAL(true, rtn_write);
+ CPPUNIT_ASSERT_EQUAL(false, exists);
}
void NoAutoRemoval()
bool exists = I2n::file_exists(Filename);
I2n::unlink(Filename);
- CPPUNIT_ASSERT_EQUAL(rtn_write, true);
- CPPUNIT_ASSERT_EQUAL(exists, true);
+ CPPUNIT_ASSERT_EQUAL(true, rtn_write);
+ CPPUNIT_ASSERT_EQUAL(true, exists);
}
void NotRunning()
PidFile pidfile(Filename);
bool rtn_check = pidfile.check_already_running();
- CPPUNIT_ASSERT_EQUAL(rtn_check, false);
+ CPPUNIT_ASSERT_EQUAL(false, rtn_check);
}
void IsAlreadyRunning()
bool rtn_file1_check = pidfile1.check_already_running();
bool rtn_file1_write = pidfile1.write();
- CPPUNIT_ASSERT_EQUAL(rtn_file1_check, false);
- CPPUNIT_ASSERT_EQUAL(rtn_file1_write, true);
+ CPPUNIT_ASSERT_EQUAL(false, rtn_file1_check);
+ CPPUNIT_ASSERT_EQUAL(true, rtn_file1_write);
PidFile pidfile2(Filename);
- bool rtn_file2_check = pidfile2.check_already_running();
+ pid_t my_pid = 0;
+ bool rtn_file2_check = pidfile2.check_already_running(&my_pid);
- CPPUNIT_ASSERT_EQUAL(rtn_file2_check, true);
+ CPPUNIT_ASSERT_EQUAL(true, rtn_file2_check);
+ CPPUNIT_ASSERT_EQUAL(getpid(), my_pid);
}
private: