2
0

os-posix.c 8.4 KB

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