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.

197 lines
6.1 KiB

  1. /*
  2. * --- SDE-COPYRIGHT-NOTE-BEGIN ---
  3. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. *
  5. * Filename: package/.../sysvinit/rc.c
  6. * Copyright (C) 2007 The OpenSDE Project
  7. * Copyright (C) 2004 - 2006 The T2 SDE Project
  8. * Copyright (C) 1998 - 2003 Clifford Wolf
  9. *
  10. * More information can be found in the files COPYING and README.
  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; version 2 of the License. A copy of the
  15. * GNU General Public License can be found in the file COPYING.
  16. * --- SDE-COPYRIGHT-NOTE-END ---
  17. */
  18. #include <errno.h>
  19. #include <grp.h>
  20. #include <sched.h>
  21. #include <signal.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/resource.h>
  26. #include <sys/stat.h>
  27. #include <sys/time.h>
  28. #include <sys/types.h>
  29. #include <unistd.h>
  30. char env_PREVLEVEL[100]="PREVLEVEL=N";
  31. char env_RUNLEVEL[100]="RUNLEVEL=N";
  32. char env_TERM[100]="TERM=linux";
  33. char * clean_env[] = {
  34. "PATH=/bin:/usr/bin:/sbin:/usr/sbin",
  35. env_PREVLEVEL,
  36. env_RUNLEVEL,
  37. env_TERM,
  38. NULL
  39. };
  40. /* See bits/resource.h and asm/resource.h */
  41. struct rlimit rlim_cpu = { RLIM_INFINITY, RLIM_INFINITY };
  42. struct rlimit rlim_fsize = { RLIM_INFINITY, RLIM_INFINITY };
  43. struct rlimit rlim_data = { RLIM_INFINITY, RLIM_INFINITY };
  44. struct rlimit rlim_stack = { RLIM_INFINITY, RLIM_INFINITY };
  45. struct rlimit rlim_core = { RLIM_INFINITY, RLIM_INFINITY };
  46. struct rlimit rlim_rss = { RLIM_INFINITY, RLIM_INFINITY };
  47. struct rlimit rlim_nofile = { 1024, 1024 };
  48. struct rlimit rlim_as = { RLIM_INFINITY, RLIM_INFINITY };
  49. struct rlimit rlim_nproc = { 2048, 2048 };
  50. struct rlimit rlim_memlock = { RLIM_INFINITY, RLIM_INFINITY };
  51. struct rlimit rlim_locks = { RLIM_INFINITY, RLIM_INFINITY };
  52. #define GROUP_LIST_SIZE 1
  53. gid_t group_list[GROUP_LIST_SIZE] = { 0 };
  54. /* This prototypes are missing in unistd.h */
  55. int setresuid(uid_t ruid, uid_t euid, uid_t suid);
  56. int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
  57. /* The first (and only) field in sched_param is sched_priority */
  58. struct sched_param sp = { 0 };
  59. #define handle_error(a) if ( a == -1 ) { perror("rc: " #a); return 1; }
  60. int main(int argc, char ** argv) {
  61. char command[1024];
  62. int use_btee = 1;
  63. int btee_pipe[2];
  64. int i;
  65. /* Copy some environment variables to the new environment if set */
  66. if ( getenv("PREVLEVEL") )
  67. sprintf(env_PREVLEVEL, "PREVLEVEL=%.50s", getenv("PREVLEVEL"));
  68. if ( getenv("RUNLEVEL") )
  69. sprintf(env_RUNLEVEL, "RUNLEVEL=%.50s", getenv("RUNLEVEL"));
  70. if ( getenv("TERM") )
  71. sprintf(env_TERM, "TERM=%.50s", getenv("TERM"));
  72. /* we never need argv[0] - skipt it */
  73. argv++; argc--;
  74. /* Handle --nobtee option */
  75. if ( argc > 0 && !strcmp(*argv, "--nobtee") ) {
  76. use_btee = 0;
  77. argv++; argc--;
  78. }
  79. /* Display help message */
  80. if ( argc < 2 ) {
  81. fprintf(stderr,
  82. "\n"
  83. " Run SystemV Init-Scripts with a clean environment and detached from\n"
  84. " the terminal.\n"
  85. "\n"
  86. " Usage: rc [ --nobtee ] <service> { start | stop | ... | help } [ script options ]\n");
  87. }
  88. if ( argc == 0 ) {
  89. fprintf(stderr, "\n"
  90. " <service> might be one of:\n"
  91. "\n");
  92. fflush(stderr);
  93. system("ls /etc/rc.d/init.d >&2");
  94. }
  95. if ( argc < 2 ) {
  96. fprintf(stderr, "\n");
  97. return 1;
  98. }
  99. /* No btee when viewing the help screen for this service */
  100. if ( !strcmp(argv[1], "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<NSIG; i++) {
  140. if ( i == SIGKILL ) continue;
  141. if ( i == SIGSTOP ) continue;
  142. if ( i >= __SIGRTMIN && i < SIGRTMIN ) continue;
  143. if( signal(i, SIG_DFL) == SIG_ERR ) {
  144. fprintf(stderr, "rc: Can't reset signal #%d: "
  145. "%s\n", i, strerror(errno) );
  146. return 1;
  147. }
  148. }
  149. /* Close all file-descriptors > 2 (1024 seams to be a good value -
  150. * linux/fs.h defines NR_OPEN to 1024*1024, which is too big ;-) */
  151. for (i=3; i<=1024; i++) close(i);
  152. /* Set user and group ids */
  153. handle_error( setgroups(GROUP_LIST_SIZE, group_list) );
  154. handle_error( setresuid(0, 0, 0) );
  155. handle_error( setresgid(0, 0, 0) );
  156. /* clear rt scheduling */
  157. handle_error( sched_setscheduler(0, SCHED_OTHER, &sp) );
  158. /* Run the command in a (non-interactive) login shell (i.e. read
  159. * /etc/profile) and send \004 after running the script if we are
  160. * using btee. */
  161. i = snprintf(command, sizeof(command), "%s%s",
  162. strchr(*argv, '/') ? "" : "/etc/rc.d/init.d/", *argv);
  163. argv++;
  164. while (*argv) /* copy args */
  165. i += snprintf(command+i, sizeof(command)-i, " %s", *argv++);
  166. i+=snprintf(command+i, sizeof(command)-i, " </dev/null 2>&1%s",
  167. use_btee ? "; echo -ne '\004'" : "");
  168. execle("/bin/bash", "-bash", "-l", "-c", command, NULL, clean_env);
  169. /* Oups! Can't exec the shell. */
  170. if ( use_btee ) write(1, "\004", 1);
  171. perror("rc: Can't execute shell for spawning init script");
  172. return 1;
  173. }