2
0

os-posix.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 <unistd.h>
  26. #include <fcntl.h>
  27. #include <signal.h>
  28. #include <sys/types.h>
  29. #include <sys/wait.h>
  30. /*needed for MAP_POPULATE before including qemu-options.h */
  31. #include <sys/mman.h>
  32. #include <pwd.h>
  33. #include <grp.h>
  34. #include <libgen.h>
  35. /* Needed early for CONFIG_BSD etc. */
  36. #include "config-host.h"
  37. #include "sysemu.h"
  38. #include "net/slirp.h"
  39. #include "qemu-options.h"
  40. #ifdef CONFIG_LINUX
  41. #include <sys/prctl.h>
  42. #endif
  43. #ifdef __FreeBSD__
  44. #include <sys/sysctl.h>
  45. #endif
  46. static struct passwd *user_pwd;
  47. static const char *chroot_dir;
  48. static int daemonize;
  49. static int fds[2];
  50. void os_setup_early_signal_handling(void)
  51. {
  52. struct sigaction act;
  53. sigfillset(&act.sa_mask);
  54. act.sa_flags = 0;
  55. act.sa_handler = SIG_IGN;
  56. sigaction(SIGPIPE, &act, NULL);
  57. }
  58. static void termsig_handler(int signal, siginfo_t *info, void *c)
  59. {
  60. qemu_system_killed(info->si_signo, info->si_pid);
  61. }
  62. void os_setup_signal_handling(void)
  63. {
  64. struct sigaction act;
  65. memset(&act, 0, sizeof(act));
  66. act.sa_sigaction = termsig_handler;
  67. act.sa_flags = SA_SIGINFO;
  68. sigaction(SIGINT, &act, NULL);
  69. sigaction(SIGHUP, &act, NULL);
  70. sigaction(SIGTERM, &act, NULL);
  71. }
  72. /* Find a likely location for support files using the location of the binary.
  73. For installed binaries this will be "$bindir/../share/qemu". When
  74. running from the build tree this will be "$bindir/../pc-bios". */
  75. #define SHARE_SUFFIX "/share/qemu"
  76. #define BUILD_SUFFIX "/pc-bios"
  77. char *os_find_datadir(const char *argv0)
  78. {
  79. char *dir;
  80. char *p = NULL;
  81. char *res;
  82. char buf[PATH_MAX];
  83. size_t max_len;
  84. #if defined(__linux__)
  85. {
  86. int len;
  87. len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
  88. if (len > 0) {
  89. buf[len] = 0;
  90. p = buf;
  91. }
  92. }
  93. #elif defined(__FreeBSD__)
  94. {
  95. static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
  96. size_t len = sizeof(buf) - 1;
  97. *buf = '\0';
  98. if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
  99. *buf) {
  100. buf[sizeof(buf) - 1] = '\0';
  101. p = buf;
  102. }
  103. }
  104. #endif
  105. /* If we don't have any way of figuring out the actual executable
  106. location then try argv[0]. */
  107. if (!p) {
  108. p = realpath(argv0, buf);
  109. if (!p) {
  110. return NULL;
  111. }
  112. }
  113. dir = dirname(p);
  114. dir = dirname(dir);
  115. max_len = strlen(dir) +
  116. MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
  117. res = g_malloc0(max_len);
  118. snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
  119. if (access(res, R_OK)) {
  120. snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
  121. if (access(res, R_OK)) {
  122. g_free(res);
  123. res = NULL;
  124. }
  125. }
  126. return res;
  127. }
  128. #undef SHARE_SUFFIX
  129. #undef BUILD_SUFFIX
  130. void os_set_proc_name(const char *s)
  131. {
  132. #if defined(PR_SET_NAME)
  133. char name[16];
  134. if (!s)
  135. return;
  136. name[sizeof(name) - 1] = 0;
  137. strncpy(name, s, sizeof(name));
  138. /* Could rewrite argv[0] too, but that's a bit more complicated.
  139. This simple way is enough for `top'. */
  140. if (prctl(PR_SET_NAME, name)) {
  141. perror("unable to change process name");
  142. exit(1);
  143. }
  144. #else
  145. fprintf(stderr, "Change of process name not supported by your OS\n");
  146. exit(1);
  147. #endif
  148. }
  149. /*
  150. * Parse OS specific command line options.
  151. * return 0 if option handled, -1 otherwise
  152. */
  153. void os_parse_cmd_args(int index, const char *optarg)
  154. {
  155. switch (index) {
  156. #ifdef CONFIG_SLIRP
  157. case QEMU_OPTION_smb:
  158. if (net_slirp_smb(optarg) < 0)
  159. exit(1);
  160. break;
  161. #endif
  162. case QEMU_OPTION_runas:
  163. user_pwd = getpwnam(optarg);
  164. if (!user_pwd) {
  165. fprintf(stderr, "User \"%s\" doesn't exist\n", optarg);
  166. exit(1);
  167. }
  168. break;
  169. case QEMU_OPTION_chroot:
  170. chroot_dir = optarg;
  171. break;
  172. case QEMU_OPTION_daemonize:
  173. daemonize = 1;
  174. break;
  175. #if defined(CONFIG_LINUX)
  176. case QEMU_OPTION_enablefips:
  177. fips_set_state(true);
  178. break;
  179. #endif
  180. }
  181. return;
  182. }
  183. static void change_process_uid(void)
  184. {
  185. if (user_pwd) {
  186. if (setgid(user_pwd->pw_gid) < 0) {
  187. fprintf(stderr, "Failed to setgid(%d)\n", user_pwd->pw_gid);
  188. exit(1);
  189. }
  190. if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) {
  191. fprintf(stderr, "Failed to initgroups(\"%s\", %d)\n",
  192. user_pwd->pw_name, user_pwd->pw_gid);
  193. exit(1);
  194. }
  195. if (setuid(user_pwd->pw_uid) < 0) {
  196. fprintf(stderr, "Failed to setuid(%d)\n", user_pwd->pw_uid);
  197. exit(1);
  198. }
  199. if (setuid(0) != -1) {
  200. fprintf(stderr, "Dropping privileges failed\n");
  201. exit(1);
  202. }
  203. }
  204. }
  205. static void change_root(void)
  206. {
  207. if (chroot_dir) {
  208. if (chroot(chroot_dir) < 0) {
  209. fprintf(stderr, "chroot failed\n");
  210. exit(1);
  211. }
  212. if (chdir("/")) {
  213. perror("not able to chdir to /");
  214. exit(1);
  215. }
  216. }
  217. }
  218. void os_daemonize(void)
  219. {
  220. if (daemonize) {
  221. pid_t pid;
  222. if (pipe(fds) == -1)
  223. exit(1);
  224. pid = fork();
  225. if (pid > 0) {
  226. uint8_t status;
  227. ssize_t len;
  228. close(fds[1]);
  229. again:
  230. len = read(fds[0], &status, 1);
  231. if (len == -1 && (errno == EINTR))
  232. goto again;
  233. if (len != 1)
  234. exit(1);
  235. else if (status == 1) {
  236. fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
  237. exit(1);
  238. } else
  239. exit(0);
  240. } else if (pid < 0)
  241. exit(1);
  242. close(fds[0]);
  243. qemu_set_cloexec(fds[1]);
  244. setsid();
  245. pid = fork();
  246. if (pid > 0)
  247. exit(0);
  248. else if (pid < 0)
  249. exit(1);
  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. uint8_t status = 0;
  261. ssize_t len;
  262. again1:
  263. len = write(fds[1], &status, 1);
  264. if (len == -1 && (errno == EINTR))
  265. goto again1;
  266. if (len != 1)
  267. exit(1);
  268. if (chdir("/")) {
  269. perror("not able to chdir to /");
  270. exit(1);
  271. }
  272. TFR(fd = qemu_open("/dev/null", O_RDWR));
  273. if (fd == -1)
  274. exit(1);
  275. }
  276. change_root();
  277. change_process_uid();
  278. if (daemonize) {
  279. dup2(fd, 0);
  280. dup2(fd, 1);
  281. dup2(fd, 2);
  282. close(fd);
  283. }
  284. }
  285. void os_pidfile_error(void)
  286. {
  287. if (daemonize) {
  288. uint8_t status = 1;
  289. if (write(fds[1], &status, 1) != 1) {
  290. perror("daemonize. Writing to pipe\n");
  291. }
  292. } else
  293. fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
  294. }
  295. void os_set_line_buffering(void)
  296. {
  297. setvbuf(stdout, NULL, _IOLBF, 0);
  298. }
  299. int qemu_create_pidfile(const char *filename)
  300. {
  301. char buffer[128];
  302. int len;
  303. int fd;
  304. fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
  305. if (fd == -1) {
  306. return -1;
  307. }
  308. if (lockf(fd, F_TLOCK, 0) == -1) {
  309. close(fd);
  310. return -1;
  311. }
  312. len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid());
  313. if (write(fd, buffer, len) != len) {
  314. close(fd);
  315. return -1;
  316. }
  317. /* keep pidfile open & locked forever */
  318. return 0;
  319. }
  320. bool is_daemonized(void)
  321. {
  322. return daemonize;
  323. }