2
0

os-posix.c 7.9 KB

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