os-posix.c 8.8 KB

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