os-posix.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. /* Needed early for CONFIG_BSD etc. */
  31. #include "net/slirp.h"
  32. #include "qemu/qemu-options.h"
  33. #include "qemu/error-report.h"
  34. #include "qemu/log.h"
  35. #include "sysemu/runstate.h"
  36. #include "qemu/cutils.h"
  37. #include "qemu/config-file.h"
  38. #include "qemu/option.h"
  39. #include "qemu/module.h"
  40. #ifdef CONFIG_LINUX
  41. #include <sys/prctl.h>
  42. #include "qemu/async-teardown.h"
  43. #endif
  44. /*
  45. * Must set all three of these at once.
  46. * Legal combinations are unset by name by uid
  47. */
  48. static struct passwd *user_pwd; /* NULL non-NULL NULL */
  49. static uid_t user_uid = (uid_t)-1; /* -1 -1 >=0 */
  50. static gid_t user_gid = (gid_t)-1; /* -1 -1 >=0 */
  51. static const char *chroot_dir;
  52. static int daemonize;
  53. static int daemon_pipe;
  54. void os_setup_early_signal_handling(void)
  55. {
  56. struct sigaction act;
  57. sigfillset(&act.sa_mask);
  58. act.sa_flags = 0;
  59. act.sa_handler = SIG_IGN;
  60. sigaction(SIGPIPE, &act, NULL);
  61. }
  62. static void termsig_handler(int signal, siginfo_t *info, void *c)
  63. {
  64. qemu_system_killed(info->si_signo, info->si_pid);
  65. }
  66. void os_setup_signal_handling(void)
  67. {
  68. struct sigaction act;
  69. memset(&act, 0, sizeof(act));
  70. act.sa_sigaction = termsig_handler;
  71. act.sa_flags = SA_SIGINFO;
  72. sigaction(SIGINT, &act, NULL);
  73. sigaction(SIGHUP, &act, NULL);
  74. sigaction(SIGTERM, &act, NULL);
  75. }
  76. void os_set_proc_name(const char *s)
  77. {
  78. #if defined(PR_SET_NAME)
  79. char name[16];
  80. if (!s)
  81. return;
  82. pstrcpy(name, sizeof(name), s);
  83. /* Could rewrite argv[0] too, but that's a bit more complicated.
  84. This simple way is enough for `top'. */
  85. if (prctl(PR_SET_NAME, name)) {
  86. error_report("unable to change process name: %s", strerror(errno));
  87. exit(1);
  88. }
  89. #else
  90. error_report("Change of process name not supported by your OS");
  91. exit(1);
  92. #endif
  93. }
  94. static bool os_parse_runas_uid_gid(const char *optarg)
  95. {
  96. unsigned long lv;
  97. const char *ep;
  98. uid_t got_uid;
  99. gid_t got_gid;
  100. int rc;
  101. rc = qemu_strtoul(optarg, &ep, 0, &lv);
  102. got_uid = lv; /* overflow here is ID in C99 */
  103. if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) {
  104. return false;
  105. }
  106. rc = qemu_strtoul(ep + 1, 0, 0, &lv);
  107. got_gid = lv; /* overflow here is ID in C99 */
  108. if (rc || got_gid != lv || got_gid == (gid_t)-1) {
  109. return false;
  110. }
  111. user_pwd = NULL;
  112. user_uid = got_uid;
  113. user_gid = got_gid;
  114. return true;
  115. }
  116. /*
  117. * Parse OS specific command line options.
  118. * return 0 if option handled, -1 otherwise
  119. */
  120. int os_parse_cmd_args(int index, const char *optarg)
  121. {
  122. switch (index) {
  123. case QEMU_OPTION_runas:
  124. user_pwd = getpwnam(optarg);
  125. if (user_pwd) {
  126. user_uid = -1;
  127. user_gid = -1;
  128. } else if (!os_parse_runas_uid_gid(optarg)) {
  129. error_report("User \"%s\" doesn't exist"
  130. " (and is not <uid>:<gid>)",
  131. optarg);
  132. exit(1);
  133. }
  134. break;
  135. case QEMU_OPTION_chroot:
  136. warn_report("option is deprecated, use '-run-with chroot=...' instead");
  137. chroot_dir = optarg;
  138. break;
  139. case QEMU_OPTION_daemonize:
  140. daemonize = 1;
  141. break;
  142. #if defined(CONFIG_LINUX)
  143. /* deprecated */
  144. case QEMU_OPTION_asyncteardown:
  145. init_async_teardown();
  146. break;
  147. #endif
  148. case QEMU_OPTION_run_with: {
  149. const char *str;
  150. QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("run-with"),
  151. optarg, false);
  152. if (!opts) {
  153. exit(1);
  154. }
  155. #if defined(CONFIG_LINUX)
  156. if (qemu_opt_get_bool(opts, "async-teardown", false)) {
  157. init_async_teardown();
  158. }
  159. #endif
  160. str = qemu_opt_get(opts, "chroot");
  161. if (str) {
  162. chroot_dir = str;
  163. }
  164. break;
  165. }
  166. default:
  167. return -1;
  168. }
  169. return 0;
  170. }
  171. static void change_process_uid(void)
  172. {
  173. assert((user_uid == (uid_t)-1) || user_pwd == NULL);
  174. assert((user_uid == (uid_t)-1) ==
  175. (user_gid == (gid_t)-1));
  176. if (user_pwd || user_uid != (uid_t)-1) {
  177. gid_t intended_gid = user_pwd ? user_pwd->pw_gid : user_gid;
  178. uid_t intended_uid = user_pwd ? user_pwd->pw_uid : user_uid;
  179. if (setgid(intended_gid) < 0) {
  180. error_report("Failed to setgid(%d)", intended_gid);
  181. exit(1);
  182. }
  183. if (user_pwd) {
  184. if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) {
  185. error_report("Failed to initgroups(\"%s\", %d)",
  186. user_pwd->pw_name, user_pwd->pw_gid);
  187. exit(1);
  188. }
  189. } else {
  190. if (setgroups(1, &user_gid) < 0) {
  191. error_report("Failed to setgroups(1, [%d])",
  192. user_gid);
  193. exit(1);
  194. }
  195. }
  196. if (setuid(intended_uid) < 0) {
  197. error_report("Failed to setuid(%d)", intended_uid);
  198. exit(1);
  199. }
  200. if (setuid(0) != -1) {
  201. error_report("Dropping privileges failed");
  202. exit(1);
  203. }
  204. }
  205. }
  206. static void change_root(void)
  207. {
  208. if (chroot_dir) {
  209. if (chroot(chroot_dir) < 0) {
  210. error_report("chroot failed");
  211. exit(1);
  212. }
  213. if (chdir("/")) {
  214. error_report("not able to chdir to /: %s", strerror(errno));
  215. exit(1);
  216. }
  217. }
  218. }
  219. void os_daemonize(void)
  220. {
  221. if (daemonize) {
  222. pid_t pid;
  223. int fds[2];
  224. if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) {
  225. exit(1);
  226. }
  227. pid = fork();
  228. if (pid > 0) {
  229. uint8_t status;
  230. ssize_t len;
  231. close(fds[1]);
  232. do {
  233. len = read(fds[0], &status, 1);
  234. } while (len < 0 && errno == EINTR);
  235. /* only exit successfully if our child actually wrote
  236. * a one-byte zero to our pipe, upon successful init */
  237. exit(len == 1 && status == 0 ? 0 : 1);
  238. } else if (pid < 0) {
  239. exit(1);
  240. }
  241. close(fds[0]);
  242. daemon_pipe = fds[1];
  243. setsid();
  244. pid = fork();
  245. if (pid > 0) {
  246. exit(0);
  247. } else if (pid < 0) {
  248. exit(1);
  249. }
  250. umask(027);
  251. signal(SIGTSTP, SIG_IGN);
  252. signal(SIGTTOU, SIG_IGN);
  253. signal(SIGTTIN, SIG_IGN);
  254. }
  255. }
  256. void os_setup_post(void)
  257. {
  258. int fd = 0;
  259. if (daemonize) {
  260. if (chdir("/")) {
  261. error_report("not able to chdir to /: %s", strerror(errno));
  262. exit(1);
  263. }
  264. fd = RETRY_ON_EINTR(qemu_open_old("/dev/null", O_RDWR));
  265. if (fd == -1) {
  266. exit(1);
  267. }
  268. }
  269. change_root();
  270. change_process_uid();
  271. if (daemonize) {
  272. uint8_t status = 0;
  273. ssize_t len;
  274. dup2(fd, 0);
  275. dup2(fd, 1);
  276. /* In case -D is given do not redirect stderr to /dev/null */
  277. if (!qemu_log_enabled()) {
  278. dup2(fd, 2);
  279. }
  280. close(fd);
  281. do {
  282. len = write(daemon_pipe, &status, 1);
  283. } while (len < 0 && errno == EINTR);
  284. if (len != 1) {
  285. exit(1);
  286. }
  287. }
  288. }
  289. void os_set_line_buffering(void)
  290. {
  291. setvbuf(stdout, NULL, _IOLBF, 0);
  292. }
  293. bool is_daemonized(void)
  294. {
  295. return daemonize;
  296. }
  297. int os_set_daemonize(bool d)
  298. {
  299. daemonize = d;
  300. return 0;
  301. }
  302. int os_mlock(void)
  303. {
  304. #ifdef HAVE_MLOCKALL
  305. int ret = 0;
  306. ret = mlockall(MCL_CURRENT | MCL_FUTURE);
  307. if (ret < 0) {
  308. error_report("mlockall: %s", strerror(errno));
  309. }
  310. return ret;
  311. #else
  312. return -ENOSYS;
  313. #endif
  314. }
  315. static QemuOptsList qemu_run_with_opts = {
  316. .name = "run-with",
  317. .head = QTAILQ_HEAD_INITIALIZER(qemu_run_with_opts.head),
  318. .desc = {
  319. #if defined(CONFIG_LINUX)
  320. {
  321. .name = "async-teardown",
  322. .type = QEMU_OPT_BOOL,
  323. },
  324. #endif
  325. {
  326. .name = "chroot",
  327. .type = QEMU_OPT_STRING,
  328. },
  329. { /* end of list */ }
  330. },
  331. };
  332. static void register_runwith(void)
  333. {
  334. qemu_add_opts(&qemu_run_with_opts);
  335. }
  336. opts_init(register_runwith);