os-posix.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * os-posix.c
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2010 Red Hat, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include <sys/wait.h>
  27. #include <pwd.h>
  28. #include <grp.h>
  29. #include <libgen.h>
  30. #include "qemu-common.h"
  31. /* Needed early for CONFIG_BSD etc. */
  32. #include "net/slirp.h"
  33. #include "qemu-options.h"
  34. #include "qemu/error-report.h"
  35. #include "qemu/log.h"
  36. #include "sysemu/runstate.h"
  37. #include "qemu/cutils.h"
  38. #ifdef CONFIG_LINUX
  39. #include <sys/prctl.h>
  40. #endif
  41. /*
  42. * Must set all three of these at once.
  43. * Legal combinations are unset by name by uid
  44. */
  45. static struct passwd *user_pwd; /* NULL non-NULL NULL */
  46. static uid_t user_uid = (uid_t)-1; /* -1 -1 >=0 */
  47. static gid_t user_gid = (gid_t)-1; /* -1 -1 >=0 */
  48. static const char *chroot_dir;
  49. static int daemonize;
  50. static int daemon_pipe;
  51. void os_setup_early_signal_handling(void)
  52. {
  53. struct sigaction act;
  54. sigfillset(&act.sa_mask);
  55. act.sa_flags = 0;
  56. act.sa_handler = SIG_IGN;
  57. sigaction(SIGPIPE, &act, NULL);
  58. }
  59. static void termsig_handler(int signal, siginfo_t *info, void *c)
  60. {
  61. qemu_system_killed(info->si_signo, info->si_pid);
  62. }
  63. void os_setup_signal_handling(void)
  64. {
  65. struct sigaction act;
  66. memset(&act, 0, sizeof(act));
  67. act.sa_sigaction = termsig_handler;
  68. act.sa_flags = SA_SIGINFO;
  69. sigaction(SIGINT, &act, NULL);
  70. sigaction(SIGHUP, &act, NULL);
  71. sigaction(SIGTERM, &act, NULL);
  72. }
  73. /*
  74. * Find a likely location for support files using the location of the binary.
  75. * When running from the build tree this will be "$bindir/../pc-bios".
  76. * Otherwise, this is CONFIG_QEMU_DATADIR.
  77. */
  78. char *os_find_datadir(void)
  79. {
  80. g_autofree char *exec_dir = NULL;
  81. g_autofree char *dir = NULL;
  82. exec_dir = qemu_get_exec_dir();
  83. g_return_val_if_fail(exec_dir != NULL, NULL);
  84. dir = g_build_filename(exec_dir, "..", "pc-bios", NULL);
  85. if (g_file_test(dir, G_FILE_TEST_IS_DIR)) {
  86. return g_steal_pointer(&dir);
  87. }
  88. return g_strdup(CONFIG_QEMU_DATADIR);
  89. }
  90. void os_set_proc_name(const char *s)
  91. {
  92. #if defined(PR_SET_NAME)
  93. char name[16];
  94. if (!s)
  95. return;
  96. pstrcpy(name, sizeof(name), s);
  97. /* Could rewrite argv[0] too, but that's a bit more complicated.
  98. This simple way is enough for `top'. */
  99. if (prctl(PR_SET_NAME, name)) {
  100. error_report("unable to change process name: %s", strerror(errno));
  101. exit(1);
  102. }
  103. #else
  104. error_report("Change of process name not supported by your OS");
  105. exit(1);
  106. #endif
  107. }
  108. static bool os_parse_runas_uid_gid(const char *optarg)
  109. {
  110. unsigned long lv;
  111. const char *ep;
  112. uid_t got_uid;
  113. gid_t got_gid;
  114. int rc;
  115. rc = qemu_strtoul(optarg, &ep, 0, &lv);
  116. got_uid = lv; /* overflow here is ID in C99 */
  117. if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) {
  118. return false;
  119. }
  120. rc = qemu_strtoul(ep + 1, 0, 0, &lv);
  121. got_gid = lv; /* overflow here is ID in C99 */
  122. if (rc || got_gid != lv || got_gid == (gid_t)-1) {
  123. return false;
  124. }
  125. user_pwd = NULL;
  126. user_uid = got_uid;
  127. user_gid = got_gid;
  128. return true;
  129. }
  130. /*
  131. * Parse OS specific command line options.
  132. * return 0 if option handled, -1 otherwise
  133. */
  134. int os_parse_cmd_args(int index, const char *optarg)
  135. {
  136. switch (index) {
  137. case QEMU_OPTION_runas:
  138. user_pwd = getpwnam(optarg);
  139. if (user_pwd) {
  140. user_uid = -1;
  141. user_gid = -1;
  142. } else if (!os_parse_runas_uid_gid(optarg)) {
  143. error_report("User \"%s\" doesn't exist"
  144. " (and is not <uid>:<gid>)",
  145. optarg);
  146. exit(1);
  147. }
  148. break;
  149. case QEMU_OPTION_chroot:
  150. chroot_dir = optarg;
  151. break;
  152. case QEMU_OPTION_daemonize:
  153. daemonize = 1;
  154. break;
  155. #if defined(CONFIG_LINUX)
  156. case QEMU_OPTION_enablefips:
  157. fips_set_state(true);
  158. break;
  159. #endif
  160. default:
  161. return -1;
  162. }
  163. return 0;
  164. }
  165. static void change_process_uid(void)
  166. {
  167. assert((user_uid == (uid_t)-1) || user_pwd == NULL);
  168. assert((user_uid == (uid_t)-1) ==
  169. (user_gid == (gid_t)-1));
  170. if (user_pwd || user_uid != (uid_t)-1) {
  171. gid_t intended_gid = user_pwd ? user_pwd->pw_gid : user_gid;
  172. uid_t intended_uid = user_pwd ? user_pwd->pw_uid : user_uid;
  173. if (setgid(intended_gid) < 0) {
  174. error_report("Failed to setgid(%d)", intended_gid);
  175. exit(1);
  176. }
  177. if (user_pwd) {
  178. if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) {
  179. error_report("Failed to initgroups(\"%s\", %d)",
  180. user_pwd->pw_name, user_pwd->pw_gid);
  181. exit(1);
  182. }
  183. } else {
  184. if (setgroups(1, &user_gid) < 0) {
  185. error_report("Failed to setgroups(1, [%d])",
  186. user_gid);
  187. exit(1);
  188. }
  189. }
  190. if (setuid(intended_uid) < 0) {
  191. error_report("Failed to setuid(%d)", intended_uid);
  192. exit(1);
  193. }
  194. if (setuid(0) != -1) {
  195. error_report("Dropping privileges failed");
  196. exit(1);
  197. }
  198. }
  199. }
  200. static void change_root(void)
  201. {
  202. if (chroot_dir) {
  203. if (chroot(chroot_dir) < 0) {
  204. error_report("chroot failed");
  205. exit(1);
  206. }
  207. if (chdir("/")) {
  208. error_report("not able to chdir to /: %s", strerror(errno));
  209. exit(1);
  210. }
  211. }
  212. }
  213. void os_daemonize(void)
  214. {
  215. if (daemonize) {
  216. pid_t pid;
  217. int fds[2];
  218. if (pipe(fds) == -1) {
  219. exit(1);
  220. }
  221. pid = fork();
  222. if (pid > 0) {
  223. uint8_t status;
  224. ssize_t len;
  225. close(fds[1]);
  226. do {
  227. len = read(fds[0], &status, 1);
  228. } while (len < 0 && errno == EINTR);
  229. /* only exit successfully if our child actually wrote
  230. * a one-byte zero to our pipe, upon successful init */
  231. exit(len == 1 && status == 0 ? 0 : 1);
  232. } else if (pid < 0) {
  233. exit(1);
  234. }
  235. close(fds[0]);
  236. daemon_pipe = fds[1];
  237. qemu_set_cloexec(daemon_pipe);
  238. setsid();
  239. pid = fork();
  240. if (pid > 0) {
  241. exit(0);
  242. } else if (pid < 0) {
  243. exit(1);
  244. }
  245. umask(027);
  246. signal(SIGTSTP, SIG_IGN);
  247. signal(SIGTTOU, SIG_IGN);
  248. signal(SIGTTIN, SIG_IGN);
  249. }
  250. }
  251. void os_setup_post(void)
  252. {
  253. int fd = 0;
  254. if (daemonize) {
  255. if (chdir("/")) {
  256. error_report("not able to chdir to /: %s", strerror(errno));
  257. exit(1);
  258. }
  259. TFR(fd = qemu_open("/dev/null", O_RDWR));
  260. if (fd == -1) {
  261. exit(1);
  262. }
  263. }
  264. change_root();
  265. change_process_uid();
  266. if (daemonize) {
  267. uint8_t status = 0;
  268. ssize_t len;
  269. dup2(fd, 0);
  270. dup2(fd, 1);
  271. /* In case -D is given do not redirect stderr to /dev/null */
  272. if (!qemu_logfile) {
  273. dup2(fd, 2);
  274. }
  275. close(fd);
  276. do {
  277. len = write(daemon_pipe, &status, 1);
  278. } while (len < 0 && errno == EINTR);
  279. if (len != 1) {
  280. exit(1);
  281. }
  282. }
  283. }
  284. void os_set_line_buffering(void)
  285. {
  286. setvbuf(stdout, NULL, _IOLBF, 0);
  287. }
  288. bool is_daemonized(void)
  289. {
  290. return daemonize;
  291. }
  292. int os_mlock(void)
  293. {
  294. int ret = 0;
  295. ret = mlockall(MCL_CURRENT | MCL_FUTURE);
  296. if (ret < 0) {
  297. error_report("mlockall: %s", strerror(errno));
  298. }
  299. return ret;
  300. }