install.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* vccd/scripts/install.c
  2. *
  3. * This file is part of vccd.
  4. *
  5. * vccd is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vccd is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vccd. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <limits.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <vcc/vcc.h>
  27. #include <vcc/vccd.h>
  28. static const char usage[] = "Usage: install\n";
  29. static inline int fatal(const char *s) {
  30. printf("fatal: %s\n", s);
  31. exit(1);
  32. }
  33. static inline int warn(const char *s) {
  34. fprintf(stderr, "warning: %s\n", s);
  35. return 0;
  36. }
  37. /* put $HOME to /etc/vccd-runtime */
  38. int main(int argc, const char **argv) {
  39. char *home;
  40. char buf[PATH_MAX];
  41. int fd;
  42. if (unlikely(argc != 1))
  43. fatal(usage);
  44. if (unlikely(getuid()))
  45. fatal("root required. ");
  46. if (unlikely(!(home = getenv("HOME")))) {
  47. warn("$HOME doesn't set. using './'. ");
  48. home = "./";
  49. }
  50. if (unlikely((fd = open("/etc/vccd-runtime", O_WRONLY | O_CREAT)) < 0)) {
  51. perror(argv[0]);
  52. return 1;
  53. }
  54. sprintf(buf, "%s/.vccd-runtime/", home);
  55. write(fd, buf, strlen(buf));
  56. close(fd);
  57. if (unlikely(mkdir(buf, 0777))) {
  58. perror(argv[0]);
  59. return 1;
  60. }
  61. sprintf(buf, "%s/.vccd-runtime/users", home);
  62. if (unlikely(mkdir(buf, 0777) < 0)) {
  63. perror(argv[0]);
  64. return 1;
  65. }
  66. return 0;
  67. }