os-posix.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. /*needed for MAP_POPULATE before including qemu-options.h */
  28. #include <pwd.h>
  29. #include <grp.h>
  30. #include <libgen.h>
  31. /* Needed early for CONFIG_BSD etc. */
  32. #include "sysemu/sysemu.h"
  33. #include "net/slirp.h"
  34. #include "qemu-options.h"
  35. #include "qemu/rcu.h"
  36. #include "qemu/error-report.h"
  37. #include "qemu/log.h"
  38. #include "qemu/cutils.h"
  39. #ifdef CONFIG_LINUX
  40. #include <sys/prctl.h>
  41. #endif
  42. static struct passwd *user_pwd;
  43. static const char *chroot_dir;
  44. static int daemonize;
  45. static int daemon_pipe;
  46. void os_setup_early_signal_handling(void)
  47. {
  48. struct sigaction act;
  49. sigfillset(&act.sa_mask);
  50. act.sa_flags = 0;
  51. act.sa_handler = SIG_IGN;
  52. sigaction(SIGPIPE, &act, NULL);
  53. }
  54. static void termsig_handler(int signal, siginfo_t *info, void *c)
  55. {
  56. qemu_system_killed(info->si_signo, info->si_pid);
  57. }
  58. void os_setup_signal_handling(void)
  59. {
  60. struct sigaction act;
  61. memset(&act, 0, sizeof(act));
  62. act.sa_sigaction = termsig_handler;
  63. act.sa_flags = SA_SIGINFO;
  64. sigaction(SIGINT, &act, NULL);
  65. sigaction(SIGHUP, &act, NULL);
  66. sigaction(SIGTERM, &act, NULL);
  67. }
  68. /* Find a likely location for support files using the location of the binary.
  69. For installed binaries this will be "$bindir/../share/qemu". When
  70. running from the build tree this will be "$bindir/../pc-bios". */
  71. #define SHARE_SUFFIX "/share/qemu"
  72. #define BUILD_SUFFIX "/pc-bios"
  73. char *os_find_datadir(void)
  74. {
  75. char *dir, *exec_dir;
  76. char *res;
  77. size_t max_len;
  78. exec_dir = qemu_get_exec_dir();
  79. if (exec_dir == NULL) {
  80. return NULL;
  81. }
  82. dir = g_path_get_dirname(exec_dir);
  83. max_len = strlen(dir) +
  84. MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
  85. res = g_malloc0(max_len);
  86. snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
  87. if (access(res, R_OK)) {
  88. snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
  89. if (access(res, R_OK)) {
  90. g_free(res);
  91. res = NULL;
  92. }
  93. }
  94. g_free(dir);
  95. g_free(exec_dir);
  96. return res;
  97. }
  98. #undef SHARE_SUFFIX
  99. #undef BUILD_SUFFIX
  100. void os_set_proc_name(const char *s)
  101. {
  102. #if defined(PR_SET_NAME)
  103. char name[16];
  104. if (!s)
  105. return;
  106. pstrcpy(name, sizeof(name), s);
  107. /* Could rewrite argv[0] too, but that's a bit more complicated.
  108. This simple way is enough for `top'. */
  109. if (prctl(PR_SET_NAME, name)) {
  110. perror("unable to change process name");
  111. exit(1);
  112. }
  113. #else
  114. fprintf(stderr, "Change of process name not supported by your OS\n");
  115. exit(1);
  116. #endif
  117. }
  118. /*
  119. * Parse OS specific command line options.
  120. * return 0 if option handled, -1 otherwise
  121. */
  122. void os_parse_cmd_args(int index, const char *optarg)
  123. {
  124. switch (index) {
  125. #ifdef CONFIG_SLIRP
  126. case QEMU_OPTION_smb:
  127. error_report("The -smb option is deprecated. "
  128. "Please use '-netdev user,smb=...' instead.");
  129. if (net_slirp_smb(optarg) < 0)
  130. exit(1);
  131. break;
  132. #endif
  133. case QEMU_OPTION_runas:
  134. user_pwd = getpwnam(optarg);
  135. if (!user_pwd) {
  136. fprintf(stderr, "User \"%s\" doesn't exist\n", optarg);
  137. exit(1);
  138. }
  139. break;
  140. case QEMU_OPTION_chroot:
  141. chroot_dir = optarg;
  142. break;
  143. case QEMU_OPTION_daemonize:
  144. daemonize = 1;
  145. break;
  146. #if defined(CONFIG_LINUX)
  147. case QEMU_OPTION_enablefips:
  148. fips_set_state(true);
  149. break;
  150. #endif
  151. }
  152. }
  153. static void change_process_uid(void)
  154. {
  155. if (user_pwd) {
  156. if (setgid(user_pwd->pw_gid) < 0) {
  157. fprintf(stderr, "Failed to setgid(%d)\n", user_pwd->pw_gid);
  158. exit(1);
  159. }
  160. if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) {
  161. fprintf(stderr, "Failed to initgroups(\"%s\", %d)\n",
  162. user_pwd->pw_name, user_pwd->pw_gid);
  163. exit(1);
  164. }
  165. if (setuid(user_pwd->pw_uid) < 0) {
  166. fprintf(stderr, "Failed to setuid(%d)\n", user_pwd->pw_uid);
  167. exit(1);
  168. }
  169. if (setuid(0) != -1) {
  170. fprintf(stderr, "Dropping privileges failed\n");
  171. exit(1);
  172. }
  173. }
  174. }
  175. static void change_root(void)
  176. {
  177. if (chroot_dir) {
  178. if (chroot(chroot_dir) < 0) {
  179. fprintf(stderr, "chroot failed\n");
  180. exit(1);
  181. }
  182. if (chdir("/")) {
  183. perror("not able to chdir to /");
  184. exit(1);
  185. }
  186. }
  187. }
  188. void os_daemonize(void)
  189. {
  190. if (daemonize) {
  191. pid_t pid;
  192. int fds[2];
  193. if (pipe(fds) == -1) {
  194. exit(1);
  195. }
  196. pid = fork();
  197. if (pid > 0) {
  198. uint8_t status;
  199. ssize_t len;
  200. close(fds[1]);
  201. do {
  202. len = read(fds[0], &status, 1);
  203. } while (len < 0 && errno == EINTR);
  204. /* only exit successfully if our child actually wrote
  205. * a one-byte zero to our pipe, upon successful init */
  206. exit(len == 1 && status == 0 ? 0 : 1);
  207. } else if (pid < 0) {
  208. exit(1);
  209. }
  210. close(fds[0]);
  211. daemon_pipe = fds[1];
  212. qemu_set_cloexec(daemon_pipe);
  213. setsid();
  214. pid = fork();
  215. if (pid > 0) {
  216. exit(0);
  217. } else if (pid < 0) {
  218. exit(1);
  219. }
  220. umask(027);
  221. signal(SIGTSTP, SIG_IGN);
  222. signal(SIGTTOU, SIG_IGN);
  223. signal(SIGTTIN, SIG_IGN);
  224. rcu_after_fork();
  225. }
  226. }
  227. void os_setup_post(void)
  228. {
  229. int fd = 0;
  230. if (daemonize) {
  231. if (chdir("/")) {
  232. perror("not able to chdir to /");
  233. exit(1);
  234. }
  235. TFR(fd = qemu_open("/dev/null", O_RDWR));
  236. if (fd == -1) {
  237. exit(1);
  238. }
  239. }
  240. change_root();
  241. change_process_uid();
  242. if (daemonize) {
  243. uint8_t status = 0;
  244. ssize_t len;
  245. dup2(fd, 0);
  246. dup2(fd, 1);
  247. /* In case -D is given do not redirect stderr to /dev/null */
  248. if (!qemu_logfile) {
  249. dup2(fd, 2);
  250. }
  251. close(fd);
  252. do {
  253. len = write(daemon_pipe, &status, 1);
  254. } while (len < 0 && errno == EINTR);
  255. if (len != 1) {
  256. exit(1);
  257. }
  258. }
  259. }
  260. void os_set_line_buffering(void)
  261. {
  262. setvbuf(stdout, NULL, _IOLBF, 0);
  263. }
  264. int qemu_create_pidfile(const char *filename)
  265. {
  266. char buffer[128];
  267. int len;
  268. int fd;
  269. fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
  270. if (fd == -1) {
  271. return -1;
  272. }
  273. if (lockf(fd, F_TLOCK, 0) == -1) {
  274. close(fd);
  275. return -1;
  276. }
  277. len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid());
  278. if (write(fd, buffer, len) != len) {
  279. close(fd);
  280. return -1;
  281. }
  282. /* keep pidfile open & locked forever */
  283. return 0;
  284. }
  285. bool is_daemonized(void)
  286. {
  287. return daemonize;
  288. }
  289. int os_mlock(void)
  290. {
  291. int ret = 0;
  292. ret = mlockall(MCL_CURRENT | MCL_FUTURE);
  293. if (ret < 0) {
  294. perror("mlockall");
  295. }
  296. return ret;
  297. }