options-processing.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <vector>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <sys/types.h>
  5. #include <pwd.h>
  6. #include <unistd.h>
  7. #include "options-processing.h"
  8. const char *service_dir_opt::user_home_path = nullptr;
  9. const char * service_dir_opt::get_user_home()
  10. {
  11. if (user_home_path == nullptr) {
  12. user_home_path = getenv("HOME");
  13. if (user_home_path == nullptr) {
  14. struct passwd * pwuid_p = getpwuid(getuid());
  15. if (pwuid_p != nullptr) {
  16. user_home_path = pwuid_p->pw_dir;
  17. }
  18. }
  19. }
  20. return user_home_path;
  21. }
  22. void service_dir_opt::build_paths(bool am_system_init)
  23. {
  24. if (service_dirs.empty()) {
  25. bool home_service_dir_set = false;
  26. /* service directory name */
  27. if (! am_system_init) {
  28. const char * user_home = get_user_home();
  29. if (user_home != nullptr) {
  30. size_t user_home_len = strlen(user_home);
  31. size_t dinit_d_len = strlen("/.config/dinit.d");
  32. size_t full_len = user_home_len + dinit_d_len + 1;
  33. char *service_dir_w = new char[full_len];
  34. std::memcpy(service_dir_w, user_home, user_home_len);
  35. std::memcpy(service_dir_w + user_home_len, "/.config/dinit.d", dinit_d_len);
  36. service_dir_w[full_len - 1] = 0;
  37. service_dir_paths.emplace_back(service_dir_w, /*dyn_allocd=*/true);
  38. home_service_dir_set = true;
  39. }
  40. }
  41. if (! home_service_dir_set) {
  42. service_dir_paths.emplace_back("/etc/dinit.d", /*dyn_allocd=*/false);
  43. service_dir_paths.emplace_back("/usr/local/lib/dinit.d", false);
  44. service_dir_paths.emplace_back("/lib/dinit.d", false);
  45. }
  46. }
  47. else {
  48. for (const char * dir : service_dirs) {
  49. service_dir_paths.emplace_back(dir, /*dyn_allocd=*/false);
  50. }
  51. }
  52. }