coldplug.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include <sys/mount.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include "../procd.h"
  20. #include "../libc-compat.h"
  21. #include "hotplug.h"
  22. #include "../container.h"
  23. static struct uloop_process udevtrigger;
  24. static void coldplug_complete(struct uloop_timeout *t)
  25. {
  26. DEBUG(4, "Coldplug complete\n");
  27. hotplug_last_event(NULL);
  28. procd_state_next();
  29. }
  30. static void udevtrigger_complete(struct uloop_process *proc, int ret)
  31. {
  32. DEBUG(4, "Finished udevtrigger\n");
  33. hotplug_last_event(coldplug_complete);
  34. }
  35. void procd_coldplug(void)
  36. {
  37. char *argv[] = { "udevtrigger", NULL };
  38. unsigned int oldumask = umask(0);
  39. if (!is_container()) {
  40. umount2("/dev/pts", MNT_DETACH);
  41. umount2("/dev/", MNT_DETACH);
  42. mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755,size=512K");
  43. mkdir("/dev/pts", 0755);
  44. mount("devpts", "/dev/pts", "devpts", MS_NOEXEC | MS_NOSUID, 0);
  45. }
  46. ignore(symlink("/tmp/shm", "/dev/shm"));
  47. umask(oldumask);
  48. udevtrigger.cb = udevtrigger_complete;
  49. udevtrigger.pid = fork();
  50. if (!udevtrigger.pid) {
  51. execvp(argv[0], argv);
  52. ERROR("Failed to start coldplug: %m\n");
  53. exit(EXIT_FAILURE);
  54. }
  55. if (udevtrigger.pid <= 0) {
  56. ERROR("Failed to start new coldplug instance: %m\n");
  57. return;
  58. }
  59. uloop_process_add(&udevtrigger);
  60. DEBUG(4, "Launched coldplug instance, pid=%d\n", (int) udevtrigger.pid);
  61. }