OpenSDE Packages Database (without history before r20070)
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.

196 lines
6.0 KiB

  1. /*
  2. * --- T2-COPYRIGHT-NOTE-BEGIN ---
  3. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. *
  5. * T2 SDE: package/.../sysvinit/rc.c
  6. * Copyright (C) 2004 - 2006 The T2 SDE Project
  7. * Copyright (C) 1998 - 2003 Clifford Wolf
  8. *
  9. * More information can be found in the files COPYING and README.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License. A copy of the
  14. * GNU General Public License can be found in the file COPYING.
  15. * --- T2-COPYRIGHT-NOTE-END ---
  16. */
  17. #include <errno.h>
  18. #include <grp.h>
  19. #include <sched.h>
  20. #include <signal.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/resource.h>
  25. #include <sys/stat.h>
  26. #include <sys/time.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. char env_PREVLEVEL[100]="PREVLEVEL=N";
  30. char env_RUNLEVEL[100]="RUNLEVEL=N";
  31. char env_TERM[100]="TERM=linux";
  32. char * clean_env[] = {
  33. "PATH=/bin:/usr/bin",
  34. env_PREVLEVEL,
  35. env_RUNLEVEL,
  36. env_TERM,
  37. NULL
  38. };
  39. /* See bits/resource.h and asm/resource.h */
  40. struct rlimit rlim_cpu = { RLIM_INFINITY, RLIM_INFINITY };
  41. struct rlimit rlim_fsize = { RLIM_INFINITY, RLIM_INFINITY };
  42. struct rlimit rlim_data = { RLIM_INFINITY, RLIM_INFINITY };
  43. struct rlimit rlim_stack = { RLIM_INFINITY, RLIM_INFINITY };
  44. struct rlimit rlim_core = { RLIM_INFINITY, RLIM_INFINITY };
  45. struct rlimit rlim_rss = { RLIM_INFINITY, RLIM_INFINITY };
  46. struct rlimit rlim_nofile = { 1024, 1024 };
  47. struct rlimit rlim_as = { RLIM_INFINITY, RLIM_INFINITY };
  48. struct rlimit rlim_nproc = { 2048, 2048 };
  49. struct rlimit rlim_memlock = { RLIM_INFINITY, RLIM_INFINITY };
  50. struct rlimit rlim_locks = { RLIM_INFINITY, RLIM_INFINITY };
  51. #define GROUP_LIST_SIZE 1
  52. gid_t group_list[GROUP_LIST_SIZE] = { 0 };
  53. /* This prototypes are missing in unistd.h */
  54. int setresuid(uid_t ruid, uid_t euid, uid_t suid);
  55. int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
  56. /* The first (and only) field in sched_param is sched_priority */
  57. struct sched_param sp = { 0 };
  58. #define handle_error(a) if ( a == -1 ) { perror("rc: " #a); return 1; }
  59. int main(int argc, char ** argv) {
  60. char command[1024];
  61. int use_btee = 1;
  62. int btee_pipe[2];
  63. int i;
  64. /* Copy some environment variables to the new environment if set */
  65. if ( getenv("PREVLEVEL") )
  66. sprintf(env_PREVLEVEL, "PREVLEVEL=%.50s", getenv("PREVLEVEL"));
  67. if ( getenv("RUNLEVEL") )
  68. sprintf(env_RUNLEVEL, "RUNLEVEL=%.50s", getenv("RUNLEVEL"));
  69. if ( getenv("TERM") )
  70. sprintf(env_TERM, "TERM=%.50s", getenv("TERM"));
  71. /* we never need argv[0] - skipt it */
  72. argv++; argc--;
  73. /* Handle --nobtee option */
  74. if ( argc > 0 && !strcmp(*argv, "--nobtee") ) {
  75. use_btee = 0;
  76. argv++; argc--;
  77. }
  78. /* Display help message */
  79. if ( argc < 2 ) {
  80. fprintf(stderr,
  81. "\n"
  82. " Run SystemV Init-Scripts with a clean environment and detached from\n"
  83. " the terminal.\n"
  84. "\n"
  85. " Usage: rc [ --nobtee ] <service> { start | stop | ... | help } [ script options ]\n");
  86. }
  87. if ( argc == 0 ) {
  88. fprintf(stderr, "\n"
  89. " <service> might be one of:\n"
  90. "\n");
  91. fflush(stderr);
  92. system("ls /etc/rc.d/init.d >&2");
  93. }
  94. if ( argc < 2 ) {
  95. fprintf(stderr, "\n");
  96. return 1;
  97. }
  98. /* No btee when viewing the help screen for this service */
  99. if ( !strcmp(argv[1], "help") ) use_btee = 0;
  100. /* Forward output to a 'btee' process */
  101. if ( use_btee ) {
  102. if ( pipe(btee_pipe) ) {
  103. perror("rc: Can't create pipe for btee");
  104. return 1;
  105. }
  106. if ( fork() == 0 ) {
  107. dup2(btee_pipe[0], 0);
  108. close(btee_pipe[0]);
  109. close(btee_pipe[1]);
  110. execl("/sbin/btee", "btee", "a",
  111. "/var/log/init.msg", NULL);
  112. perror("rc: Can't exec btee command");
  113. return 1;
  114. } else {
  115. dup2(btee_pipe[1], 1);
  116. close(btee_pipe[0]);
  117. close(btee_pipe[1]);
  118. }
  119. }
  120. /* Set umask, process-group, nice-value and current directory */
  121. handle_error( umask(022) );
  122. handle_error( setpgid(0,0) );
  123. handle_error( setpriority(PRIO_PROCESS, 0, 0) );
  124. handle_error( chdir("/") );
  125. /* Set all ulimits */
  126. handle_error( setrlimit(RLIMIT_CPU, &rlim_cpu) );
  127. handle_error( setrlimit(RLIMIT_FSIZE, &rlim_fsize) );
  128. handle_error( setrlimit(RLIMIT_DATA, &rlim_data) );
  129. handle_error( setrlimit(RLIMIT_STACK, &rlim_stack) );
  130. handle_error( setrlimit(RLIMIT_CORE, &rlim_core) );
  131. handle_error( setrlimit(RLIMIT_RSS, &rlim_rss) );
  132. handle_error( setrlimit(RLIMIT_NOFILE, &rlim_nofile) );
  133. handle_error( setrlimit(RLIMIT_AS, &rlim_as) );
  134. handle_error( setrlimit(RLIMIT_NPROC, &rlim_nproc) );
  135. handle_error( setrlimit(RLIMIT_MEMLOCK, &rlim_memlock) );
  136. handle_error( setrlimit(RLIMIT_LOCKS, &rlim_locks) );
  137. /* Reset all signal handlers */
  138. for (i=1; i<NSIG; i++) {
  139. if ( i == SIGKILL ) continue;
  140. if ( i == SIGSTOP ) continue;
  141. if ( i >= __SIGRTMIN && i < SIGRTMIN ) 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. i = snprintf(command, sizeof(command), "%s%s",
  161. strchr(*argv, '/') ? "" : "/etc/rc.d/init.d/", *argv);
  162. argv++;
  163. while (*argv) /* copy args */
  164. i += snprintf(command+i, sizeof(command)-i, " %s", *argv++);
  165. i+=snprintf(command+i, sizeof(command)-i, " </dev/null 2>&1%s",
  166. use_btee ? "; echo -ne '\004'" : "");
  167. execle("/bin/bash", "-bash", "-l", "-c", command, NULL, clean_env);
  168. /* Oups! Can't exec the shell. */
  169. if ( use_btee ) write(1, "\004", 1);
  170. perror("rc: Can't execute shell for spawning init script");
  171. return 1;
  172. }