mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
6.0 KiB

  1. /*
  2. * --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  3. *
  4. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  5. * Please add additional copyright information _after_ the line containing
  6. * the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  7. * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  8. *
  9. * ROCK Linux: rock-src/package/base/sysfiles/rc.c
  10. * ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version. A copy of the GNU General Public
  16. * License can be found at Documentation/COPYING.
  17. *
  18. * Many people helped and are helping developing ROCK Linux. Please
  19. * have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  20. * file for details.
  21. *
  22. * --- ROCK-COPYRIGHT-NOTE-END ---
  23. */
  24. #include <errno.h>
  25. #include <grp.h>
  26. #include <sched.h>
  27. #include <signal.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/resource.h>
  32. #include <sys/stat.h>
  33. #include <sys/time.h>
  34. #include <sys/types.h>
  35. #include <unistd.h>
  36. char env_PREVLEVEL[100]="PREVLEVEL=N";
  37. char env_RUNLEVEL[100]="RUNLEVEL=N";
  38. char env_TERM[100]="TERM=linux";
  39. char * clean_env[] = {
  40. "PATH=/bin:/usr/bin",
  41. env_PREVLEVEL,
  42. env_RUNLEVEL,
  43. env_TERM,
  44. NULL
  45. };
  46. /* See bits/resource.h and asm/resource.h */
  47. struct rlimit rlim_cpu = { RLIM_INFINITY, RLIM_INFINITY };
  48. struct rlimit rlim_fsize = { RLIM_INFINITY, RLIM_INFINITY };
  49. struct rlimit rlim_data = { RLIM_INFINITY, RLIM_INFINITY };
  50. struct rlimit rlim_stack = { RLIM_INFINITY, RLIM_INFINITY };
  51. struct rlimit rlim_core = { RLIM_INFINITY, RLIM_INFINITY };
  52. struct rlimit rlim_rss = { RLIM_INFINITY, RLIM_INFINITY };
  53. struct rlimit rlim_nofile = { 1024, 1024 };
  54. struct rlimit rlim_as = { RLIM_INFINITY, RLIM_INFINITY };
  55. struct rlimit rlim_nproc = { 2048, 2048 };
  56. struct rlimit rlim_memlock = { RLIM_INFINITY, RLIM_INFINITY };
  57. struct rlimit rlim_locks = { RLIM_INFINITY, RLIM_INFINITY };
  58. #define GROUP_LIST_SIZE 1
  59. gid_t group_list[GROUP_LIST_SIZE] = { 0 };
  60. /* This prototypes are missing in unistd.h */
  61. int setresuid(uid_t ruid, uid_t euid, uid_t suid);
  62. int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
  63. /* The first (and only) field in sched_param is sched_priority */
  64. struct sched_param sp = { 0 };
  65. #define handle_error(a) if ( a == -1 ) { perror("rc: " #a); return 1; }
  66. int main(int argc, char ** argv) {
  67. char command[1024];
  68. int use_btee = 1;
  69. int btee_pipe[2];
  70. int i;
  71. /* Copy some environment variables to the new environment if set */
  72. if ( getenv("PREVLEVEL") )
  73. sprintf(env_PREVLEVEL, "PREVLEVEL=%.50s", getenv("PREVLEVEL"));
  74. if ( getenv("RUNLEVEL") )
  75. sprintf(env_RUNLEVEL, "RUNLEVEL=%.50s", getenv("RUNLEVEL"));
  76. if ( getenv("TERM") )
  77. sprintf(env_TERM, "TERM=%.50s", getenv("TERM"));
  78. /* Handle --nobtee option */
  79. if ( argc > 1 && !strcmp(argv[1], "--nobtee") ) {
  80. use_btee = 0;
  81. argv++; argc--;
  82. }
  83. /* Display help message */
  84. if ( argc != 3 ) {
  85. fprintf(stderr,
  86. "\n"
  87. " Run SystemV Init-Scripts with a clean environment and detached from\n"
  88. " the terminal.\n"
  89. "\n"
  90. " Usage: rc [ --nobtee ] <service> { start | stop | ... | help }\n"
  91. "\n"
  92. " <service> might be one of:\n"
  93. "\n");
  94. fflush(stderr);
  95. system("ls /etc/rc.d/init.d >&2");
  96. fprintf(stderr, "\n");
  97. return 1;
  98. }
  99. /* No btee when viewing the help screen for this service */
  100. if ( !strcmp(argv[2], "help") ) use_btee = 0;
  101. /* Forward output to a 'btee' process */
  102. if ( use_btee ) {
  103. if ( pipe(btee_pipe) ) {
  104. perror("rc: Can't create pipe for btee");
  105. return 1;
  106. }
  107. if ( fork() == 0 ) {
  108. dup2(btee_pipe[0], 0);
  109. close(btee_pipe[0]);
  110. close(btee_pipe[1]);
  111. execl("/sbin/btee", "btee", "a",
  112. "/var/log/init.msg", NULL);
  113. perror("rc: Can't exec btee command");
  114. return 1;
  115. } else {
  116. dup2(btee_pipe[1], 1);
  117. close(btee_pipe[0]);
  118. close(btee_pipe[1]);
  119. }
  120. }
  121. /* Set umask, process-group, nice-value and current directory */
  122. handle_error( umask(022) );
  123. handle_error( setpgid(0,0) );
  124. handle_error( setpriority(PRIO_PROCESS, 0, 0) );
  125. handle_error( chdir("/") );
  126. /* Set all ulimits */
  127. handle_error( setrlimit(RLIMIT_CPU, &rlim_cpu) );
  128. handle_error( setrlimit(RLIMIT_FSIZE, &rlim_fsize) );
  129. handle_error( setrlimit(RLIMIT_DATA, &rlim_data) );
  130. handle_error( setrlimit(RLIMIT_STACK, &rlim_stack) );
  131. handle_error( setrlimit(RLIMIT_CORE, &rlim_core) );
  132. handle_error( setrlimit(RLIMIT_RSS, &rlim_rss) );
  133. handle_error( setrlimit(RLIMIT_NOFILE, &rlim_nofile) );
  134. handle_error( setrlimit(RLIMIT_AS, &rlim_as) );
  135. handle_error( setrlimit(RLIMIT_NPROC, &rlim_nproc) );
  136. handle_error( setrlimit(RLIMIT_MEMLOCK, &rlim_memlock) );
  137. handle_error( setrlimit(RLIMIT_LOCKS, &rlim_locks) );
  138. /* Reset all signal handlers */
  139. for (i=1; i<64; i++) {
  140. if ( i == SIGKILL ) continue;
  141. if ( i == SIGSTOP ) continue;
  142. if( signal(i, SIG_DFL) == SIG_ERR ) {
  143. fprintf(stderr, "rc: Can't reset signal #%d: "
  144. "%s\n", i, strerror(errno) );
  145. return 1;
  146. }
  147. }
  148. /* Close all file-descriptors > 2 (1024 seams to be a good value -
  149. * linux/fs.h defines NR_OPEN to 1024*1024, which is too big ;-) */
  150. for (i=3; i<=1024; i++) close(i);
  151. /* Set user and group ids */
  152. handle_error( setgroups(GROUP_LIST_SIZE, group_list) );
  153. handle_error( setresuid(0, 0, 0) );
  154. handle_error( setresgid(0, 0, 0) );
  155. /* clear rt scheduling */
  156. handle_error( sched_setscheduler(0, SCHED_OTHER, &sp) );
  157. /* Run the command in a (non-interactive) login shell (i.e. read
  158. * /etc/profile) and send \004 after running the script if we are
  159. * using btee. */
  160. snprintf(command, 1024, "%s%s %s </dev/null 2>&1%s",
  161. strchr(argv[1], '/') ? "" : "/etc/rc.d/init.d/",
  162. argv[1], argv[2], use_btee ? "; echo -ne '\004'" : "");
  163. execle("/bin/bash", "-bash", "-l", "-c", command, NULL, clean_env);
  164. /* Oups! Can't exec the shell. */
  165. if ( use_btee ) write(1, "\004", 1);
  166. perror("rc: Can't execute shell for spawning init script");
  167. return 1;
  168. }