os-posix.c 7.6 KB

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