1 /* 2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org> 3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU Lesser General Public License version 2.1 7 * as published by the Free Software Foundation 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #include <sys/wait.h> 16 #include <sys/types.h> 17 #include <sys/stat.h> 18 #include <sys/reboot.h> 19 20 #include <unistd.h> 21 #include <getopt.h> 22 #include <libgen.h> 23 24 #include "procd.h" 25 #include "watchdog.h" 26 #include "plug/hotplug.h" 27 28 unsigned int debug; 29 30 static struct udebug ud; 31 static struct udebug_buf udb; 32 static bool udebug_enabled; 33 34 static void procd_udebug_vprintf(const char *format, va_list ap) 35 { 36 if (!udebug_enabled) 37 return; 38 39 udebug_entry_init(&udb); 40 udebug_entry_vprintf(&udb, format, ap); 41 udebug_entry_add(&udb); 42 } 43 44 void procd_udebug_printf(const char *format, ...) 45 { 46 va_list ap; 47 48 va_start(ap, format); 49 procd_udebug_vprintf(format, ap); 50 va_end(ap); 51 } 52 53 void procd_udebug_set_enabled(bool val) 54 { 55 static const struct udebug_buf_meta meta = { 56 .name = "procd_log", 57 .format = UDEBUG_FORMAT_STRING, 58 }; 59 60 if (udebug_enabled == val) 61 return; 62 63 udebug_enabled = val; 64 if (!val) { 65 ulog_udebug(NULL); 66 udebug_buf_free(&udb); 67 udebug_free(&ud); 68 return; 69 } 70 71 udebug_init(&ud); 72 udebug_auto_connect(&ud, NULL); 73 udebug_buf_init(&udb, 1024, 64 * 1024); 74 udebug_buf_add(&ud, &udb, &meta); 75 ulog_udebug(&udb); 76 } 77 78 79 static int usage(const char *prog) 80 { 81 fprintf(stderr, "Usage: %s [options]\n" 82 "Options:\n" 83 " -s <path> Path to ubus socket\n" 84 " -h <path> run as hotplug daemon\n" 85 " -d <level> Enable debug messages\n" 86 " -I <path> Path to init.d directory\n" 87 " -R <path> Path to rc.d directory\n" 88 " -S Print messages to stdout\n" 89 "\n", prog); 90 return 1; 91 } 92 93 int main(int argc, char **argv) 94 { 95 int ch; 96 char *dbglvl = getenv("DBGLVL"); 97 int ulog_channels = ULOG_KMSG; 98 99 if (dbglvl) { 100 debug = atoi(dbglvl); 101 unsetenv("DBGLVL"); 102 } 103 104 while ((ch = getopt(argc, argv, "d:s:h:I:R:S")) != -1) { 105 switch (ch) { 106 case 'h': 107 return hotplug_run(optarg); 108 case 's': 109 ubus_socket = optarg; 110 break; 111 case 'd': 112 debug = atoi(optarg); 113 break; 114 case 'I': 115 init_d_path = optarg; 116 break; 117 case 'R': 118 rc_d_path = optarg; 119 break; 120 case 'S': 121 ulog_channels = ULOG_STDIO; 122 break; 123 default: 124 return usage(argv[0]); 125 } 126 } 127 128 ulog_open(ulog_channels, LOG_DAEMON, "procd"); 129 ulog_threshold(LOG_DEBUG + 1); 130 131 setsid(); 132 uloop_init(); 133 procd_signal(); 134 procd_udebug_set_enabled(true); 135 if (getpid() != 1) 136 procd_connect_ubus(); 137 else 138 procd_state_next(); 139 uloop_run(); 140 uloop_done(); 141 142 return 0; 143 } 144
This page was automatically generated by LXR 0.3.1. • OpenWrt