WHR | 1cf465e | 2015-03-03 16:37:02 +0800 | [diff] [blame] | 1 | /* service - toolbox |
| 2 | Copyright 2015 libdll.so |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
| 5 | |
| 6 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 7 | */ |
WHR | 1cf465e | 2015-03-03 16:37:02 +0800 | [diff] [blame] | 8 | |
WHR | 0796f35 | 2015-03-10 16:27:09 +0800 | [diff] [blame] | 9 | #ifdef _WIN32 |
| 10 | #ifdef _WIN32_WCE |
| 11 | #include "service.wce.c" |
| 12 | #else |
| 13 | #error "Not implemented" |
| 14 | #endif |
| 15 | |
| 16 | #else |
| 17 | |
WHR | 1cf465e | 2015-03-03 16:37:02 +0800 | [diff] [blame] | 18 | #include <unistd.h> |
| 19 | #include <limits.h> |
| 20 | #include <string.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include <assert.h> |
JohnnySun | d60b363 | 2015-03-04 17:45:53 +0800 | [diff] [blame] | 24 | #include <getopt.h> |
JohnnySun | c3aa9b6 | 2015-03-05 13:01:16 +0800 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <dirent.h> |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 29 | #include <sys/wait.h> |
WHR | 1cf465e | 2015-03-03 16:37:02 +0800 | [diff] [blame] | 30 | |
| 31 | #define INIT_D_PATH "/etc/init.d/" |
| 32 | |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 33 | static const char *env_names[] = { |
WHR | 1cf465e | 2015-03-03 16:37:02 +0800 | [diff] [blame] | 34 | "LANG", "PATH", "TERM" |
| 35 | }; |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 36 | |
| 37 | static const char *ignore_names[] = { |
| 38 | "skeleton", "README", "rc", "rcS", "single", "reboot", "bootclean.sh", "halt", "killall", "single", "linuxconf", "kudzu" |
| 39 | }; |
| 40 | |
| 41 | static char service_script[PATH_MAX+1] = INIT_D_PATH; |
| 42 | static size_t name_len; |
| 43 | static char *env[sizeof env_names / sizeof(char *) + 1]; |
WHR | 1cf465e | 2015-03-03 16:37:02 +0800 | [diff] [blame] | 44 | |
| 45 | static void print_usage(const char *name) { |
| 46 | fprintf(stderr, "Usage: %s <service> [<action>] [<options>]\n", name); |
| 47 | } |
| 48 | |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 49 | static int get_status(const char *pathname) { |
| 50 | pid_t pid = fork(); |
| 51 | if(pid < 0) return '?'; |
| 52 | if(pid) { |
| 53 | int status; |
| 54 | if(waitpid(pid, &status, 0) < 0) return '?'; |
| 55 | if(WIFSIGNALED(status)) return '?'; |
| 56 | //fprintf(stderr, "%d\n", WEXITSTATUS(status)); |
WHR | fa45a1e | 2015-03-08 22:20:36 +0800 | [diff] [blame] | 57 | int e = WEXITSTATUS(status); |
| 58 | if(e == 1) return '?'; |
| 59 | return e == 0 ? '+' : '-'; |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 60 | } else { |
| 61 | close(STDOUT_FILENO); |
| 62 | close(STDERR_FILENO); |
| 63 | execle(pathname, pathname, "status", NULL, env); |
WHR | fa45a1e | 2015-03-08 22:20:36 +0800 | [diff] [blame] | 64 | exit(1); |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 65 | } |
| 66 | } |
JohnnySun | c3aa9b6 | 2015-03-05 13:01:16 +0800 | [diff] [blame] | 67 | |
| 68 | static int status_all() { |
| 69 | struct dirent *de; |
JohnnySun | 1041877 | 2015-03-05 17:47:16 +0800 | [diff] [blame] | 70 | struct stat s; |
JohnnySun | c3aa9b6 | 2015-03-05 13:01:16 +0800 | [diff] [blame] | 71 | DIR *d = opendir(INIT_D_PATH); |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 72 | int i; |
WHR | 8d594de | 2015-03-24 23:35:01 +0800 | [diff] [blame] | 73 | if(!d) { |
| 74 | perror(INIT_D_PATH); |
| 75 | return 1; |
| 76 | } |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 77 | first_loop: |
| 78 | while((de = readdir(d))) { |
JohnnySun | c3aa9b6 | 2015-03-05 13:01:16 +0800 | [diff] [blame] | 79 | if((strcmp(de->d_name, ".") == 0) || (strcmp(de->d_name, "..") == 0)) continue; |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 80 | for(i = 0; i < sizeof ignore_names / sizeof(char *); i++) { |
| 81 | if(strcmp(de->d_name, ignore_names[i]) == 0) goto first_loop; |
| 82 | } |
| 83 | //snprintf(tmp, sizeof(tmp), INIT_D_PATH "%s", de->d_name); |
| 84 | //strcpy(service_script, INIT_D_PATH); |
| 85 | //strcat(service_script, de->d_name); |
JohnnySun | 1041877 | 2015-03-05 17:47:16 +0800 | [diff] [blame] | 86 | |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 87 | name_len = strlen(de->d_name); |
| 88 | if(sizeof INIT_D_PATH + name_len > sizeof service_script) { |
| 89 | continue; |
| 90 | } |
| 91 | memcpy(service_script + sizeof INIT_D_PATH - 1, de->d_name, name_len + 1); |
| 92 | |
| 93 | if(stat(service_script, &s) < 0) { |
| 94 | fprintf(stderr, "stat %s failed: %s", service_script, strerror(errno)); |
JohnnySun | 1041877 | 2015-03-05 17:47:16 +0800 | [diff] [blame] | 95 | return -1; |
| 96 | } |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 97 | if(!S_ISDIR(s.st_mode) && (s.st_mode & 0111)) { |
| 98 | //if(access(tmp, X_OK) == 0) |
| 99 | // fprintf(stdout, "%s\n", de->d_name); |
| 100 | printf(" [ %c ] %s\n", get_status(service_script), de->d_name); |
JohnnySun | 1041877 | 2015-03-05 17:47:16 +0800 | [diff] [blame] | 101 | } |
JohnnySun | c3aa9b6 | 2015-03-05 13:01:16 +0800 | [diff] [blame] | 102 | } |
| 103 | return 0; |
| 104 | } |
JohnnySun | f2af624 | 2015-03-04 23:44:11 +0800 | [diff] [blame] | 105 | |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 106 | static void init_env() { |
| 107 | int i; |
| 108 | char **p = env; |
| 109 | /* How many options the env_names have. */ |
| 110 | for(i = 0; i < sizeof env_names / sizeof(char *); i++) { |
| 111 | char *e = getenv(env_names[i]); |
| 112 | /* Copy HEAD to env[i] */ |
| 113 | if(e) *p++ = e; |
| 114 | } |
| 115 | *p = NULL; |
| 116 | } |
| 117 | |
WHR | 1cf465e | 2015-03-03 16:37:02 +0800 | [diff] [blame] | 118 | int service_main(int argc, char **argv) { |
JohnnySun | d60b363 | 2015-03-04 17:45:53 +0800 | [diff] [blame] | 119 | static struct option long_options[] = { |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 120 | { "help", no_argument, 0, 'h' }, |
| 121 | { "status-all", no_argument, 0, 0 }, |
| 122 | { 0, 0, 0, 0 } |
JohnnySun | d60b363 | 2015-03-04 17:45:53 +0800 | [diff] [blame] | 123 | }; |
| 124 | while(1) { |
| 125 | int option_index = 0; |
JohnnySun | 6242296 | 2015-03-04 18:44:48 +0800 | [diff] [blame] | 126 | int c = getopt_long(argc, argv, "h", long_options, &option_index); |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 127 | if(c == -1) break; |
JohnnySun | d60b363 | 2015-03-04 17:45:53 +0800 | [diff] [blame] | 128 | switch(c) { |
| 129 | case 'h': |
| 130 | print_usage(argv[0]); |
| 131 | return -1; |
JohnnySun | 6242296 | 2015-03-04 18:44:48 +0800 | [diff] [blame] | 132 | case 0: |
| 133 | //printf("%s\n", long_options[option_index].name); |
JohnnySun | 2d4e0b8 | 2015-03-09 17:01:14 +0800 | [diff] [blame] | 134 | if(option_index == 1) { |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 135 | init_env(); |
JohnnySun | c3aa9b6 | 2015-03-05 13:01:16 +0800 | [diff] [blame] | 136 | status_all(); |
JohnnySun | d60b363 | 2015-03-04 17:45:53 +0800 | [diff] [blame] | 137 | return 1; |
| 138 | } |
JohnnySun | 6242296 | 2015-03-04 18:44:48 +0800 | [diff] [blame] | 139 | break; |
JohnnySun | d60b363 | 2015-03-04 17:45:53 +0800 | [diff] [blame] | 140 | case '?': |
| 141 | return -1; |
| 142 | default: |
| 143 | print_usage(argv[0]); |
| 144 | return 0; |
| 145 | } |
WHR | 1cf465e | 2015-03-03 16:37:02 +0800 | [diff] [blame] | 146 | } |
JohnnySun | f2af624 | 2015-03-04 23:44:11 +0800 | [diff] [blame] | 147 | /* Check if no option, Avoid Segmentation fault */ |
JohnnySun | b091a0c | 2015-03-04 23:40:13 +0800 | [diff] [blame] | 148 | if(optind == argc) { |
| 149 | print_usage(argv[0]); |
| 150 | return 0; |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 151 | } |
| 152 | name_len = strlen(argv[1]); |
WHR | 1cf465e | 2015-03-03 16:37:02 +0800 | [diff] [blame] | 153 | if(sizeof INIT_D_PATH + name_len > sizeof service_script) { |
| 154 | fprintf(stderr, "%s: Service name too long\n", argv[0]); |
| 155 | return 1; |
| 156 | } |
| 157 | memcpy(service_script + sizeof INIT_D_PATH - 1, argv[1], name_len + 1); |
| 158 | if(access(service_script, X_OK) < 0) { |
| 159 | fprintf(stderr, "%s: %s: Unrecognized service\n", argv[0], argv[1]); |
| 160 | return 1; |
| 161 | } |
WHR | fe52b3b | 2015-03-07 20:51:28 +0800 | [diff] [blame] | 162 | init_env(); |
WHR | 1cf465e | 2015-03-03 16:37:02 +0800 | [diff] [blame] | 163 | //execle(service_script, argv[0], argv[2], NULL, env); |
| 164 | //perror(argv[1]); |
| 165 | argv++; |
| 166 | execve(service_script, argv, env); |
| 167 | perror(argv[0]); |
| 168 | return 1; |
| 169 | } |
WHR | 0796f35 | 2015-03-10 16:27:09 +0800 | [diff] [blame] | 170 | |
| 171 | #endif |