2
0

os-posix.c 8.5 KB

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