int main(void) { jrpc_server_init(&my_server, HOST, PORT); jrpc_register_procedure(&my_server, say_hello, "sayHello", NULL); jrpc_register_procedure(&my_server, exit_server, "exit", NULL); jrpc_register_procedure(&my_server, foo, "foo", NULL); uloop_init(); uloop_fd_add(&my_server.sock, ULOOP_READ); uloop_run(); jrpc_server_destroy(&my_server); return 0; }
int main(int argc, char **argv) { pid_t pid; ulog_open(ULOG_KMSG, LOG_DAEMON, "init"); sigaction(SIGTERM, &sa_shutdown, NULL); sigaction(SIGUSR1, &sa_shutdown, NULL); sigaction(SIGUSR2, &sa_shutdown, NULL); early(); cmdline(); watchdog_init(1); pid = fork(); if (!pid) { char *kmod[] = { "/sbin/kmodloader", "/etc/modules-boot.d/", NULL }; if (debug < 3) patch_stdio("/dev/null"); execvp(kmod[0], kmod); ERROR("Failed to start kmodloader\n"); exit(-1); } if (pid <= 0) { ERROR("Failed to start kmodloader instance\n"); } else { int i; for (i = 0; i < 1200; i++) { if (waitpid(pid, NULL, WNOHANG) > 0) break; usleep(10 * 1000); watchdog_ping(); } } uloop_init(); preinit(); uloop_run(); return 0; }
int main(int argc, char **argv) { int rc = 0; rc = config_load(); if (rc) { ERROR("configuration loading failed\n"); exit(EXIT_FAILURE); } rc = uloop_init(); if (rc) { ERROR("uloop init failed\n"); exit(EXIT_FAILURE); } rc = ssh_netconf_init(); if (rc) { ERROR("ssh init failed\n"); exit(EXIT_FAILURE); } rc = ubus_init(); if (rc) { ERROR("ubus init failed\n"); exit(EXIT_FAILURE); } LOG("freenetconfd is listening on address:'%s', port:'%s'\n", config.addr, config.port); /* main loop */ uloop_run(); ssh_netconf_exit(); uloop_done(); ubus_exit(); config_exit(); return EXIT_SUCCESS; }
int main(int argc, char **argv) { // uloop initialize uloop_init(); // setTimeout struct uloop_timeout *timeout; initialize_timer(timeout, SET_TIMEOUT); // setInternal struct uloop_timeout *interval; initialize_timer(interval, SET_INTERVAL); // loop start uloop_run(); uloop_done(); return 0; }
int main(int argc, char **argv) { const char *ubus_socket = UBUS_UNIX_SOCKET; int ret = 0; int ch; signal(SIGPIPE, SIG_IGN); signal(SIGHUP, sighup_handler); openlog("ubusd", LOG_PID, LOG_DAEMON); uloop_init(); while ((ch = getopt(argc, argv, "s:")) != -1) { switch (ch) { case 's': ubus_socket = optarg; break; default: return usage(argv[0]); } } unlink(ubus_socket); umask(0111); server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL); if (server_fd.fd < 0) { perror("usock"); ret = -1; goto out; } uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER); ubusd_acl_load(); uloop_run(); unlink(ubus_socket); out: uloop_done(); return ret; }
int main() { static char unitd[] = "unitd"; if (getpid() != 1) { fprintf(stderr, "error: must run as PID 1\n"); return 1; } program_invocation_short_name = unitd; prctl(PR_SET_NAME, unitd); ulog_open(ULOG_KMSG, LOG_DAEMON, "unitd"); setsid(); uloop_init(); unitd_signal(); unitd_state_next(); uloop_run(); uloop_done(); return 0; }
static int ubus_cli_listen(struct ubus_context *ctx, char *event) { static struct ubus_event_handler listener; int ret = 0; memset(&listener, 0, sizeof(listener)); listener.cb = receive_event; ret = ubus_register_event_handler(ctx, &listener, event); if (ret) { fprintf(stderr, "Error while registering for event '%s': %s\n", event, ubus_strerror(ret)); return -1; } uloop_init(); ubus_add_uloop(ctx); uloop_run(); uloop_done(); return 0; }
int main(int argc, char **argv) { const char *ubus_socket = UBUS_UNIX_SOCKET;//定义在cmake中,UBUS_UNIX_SOCKET="/var/run/ubus.sock" int ret = 0; int ch; signal(SIGPIPE, SIG_IGN);//入参:信号类型;处理函数handler。 uloop_init(); while ((ch = getopt(argc, argv, "s:")) != -1) { switch (ch) { case 's': ubus_socket = optarg; break; default: return usage(argv[0]); } } unlink(ubus_socket);//unlink,删除/var/run/ubus.sock文件 umask(0177);//umask,设置用户创建新文件时文件的默认权限。 server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL); if (server_fd.fd < 0) {//usock,首参为套接字类型,ubus_socket可为"127.0.0.1"或var/run/ubus.sock,最后一个为服务端口号"22"或NULL perror("usock"); ret = -1; goto out; } uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER); uloop_run();//调用uloop_run(),ubusd进程不会再退出--->ubusd通过select方法监听套接字/var/run/ubus.sock,接收ubus发出的服务查询/请求,并将服务请求转发给netifd等提供服务的进程。 unlink(ubus_socket); out: uloop_done(); return ret; }
int main(int argc, char *argv[]) { config_init(argc, argv); if (glue_init() != 0) { iotc_error("glue init failed!"); return -1; } if (0 != connection_init()) { iotc_error("socket_init failed!"); return -1; } uloop_init(); uloop_fd_add(&iotc_monitor_uloop_fd, ULOOP_READ); uloop_timeout_set(&g_sendDevOnlineTm, 2000); uloop_run(); uloop_done(); return 0; }
int main(int argc, char **argv) { int opt, rc = 0; while ((opt = getopt(argc, argv, "hv")) != -1) { switch (opt) { case 'v': conf.verbosity++; break; case 'h': default: return usage(argv[0]); } } conf.flx_ufd.fd = open(FLX_DEV, O_RDWR); if (conf.flx_ufd.fd < 0) { perror(FLX_DEV); rc = 1; goto finish; } if (!configure_tty(conf.flx_ufd.fd)) { fprintf(stderr, "%s: Failed to configure tty params\n", FLX_DEV); rc = 2; goto finish; } if (!config_init()) { rc = 3; goto oom; } if (!config_load_all()) { rc = 4; goto finish; } conf.ubus_ctx = ubus_connect(NULL); if (!conf.ubus_ctx) { fprintf(stderr, "Failed to connect to ubus\n"); rc = 5; goto finish; } #ifdef WITH_YKW conf.ykw = ykw_new(YKW_DEFAULT_THETA); if (conf.ykw == NULL) { rc = 6; goto oom; } #endif mosquitto_lib_init(); snprintf(conf.mqtt.id, MQTT_ID_LEN, MQTT_ID_TPL, getpid()); conf.mosq = mosquitto_new(conf.mqtt.id, conf.mqtt.clean_session, &conf); if (!conf.mosq) { switch (errno) { case ENOMEM: rc = 7; goto oom; case EINVAL: fprintf(stderr, "mosq_new: Invalid id and/or clean_session.\n"); rc = 8; goto finish; } } rc = mosquitto_loop_start(conf.mosq); switch (rc) { case MOSQ_ERR_INVAL: fprintf(stderr, "mosq_loop_start: Invalid input parameters.\n"); goto finish; case MOSQ_ERR_NOT_SUPPORTED: fprintf(stderr, "mosq_loop_start: No threading support.\n"); goto finish; }; rc = mosquitto_connect_async(conf.mosq, conf.mqtt.host, conf.mqtt.port, conf.mqtt.keepalive); switch (rc) { case MOSQ_ERR_INVAL: fprintf(stderr, "mosq_connect_async: Invalid input parameters.\n"); goto finish; case MOSQ_ERR_ERRNO: perror("mosq_connect_async"); goto finish; } uloop_init(); uloop_fd_add(&conf.flx_ufd, ULOOP_READ); uloop_timeout_set(&conf.timeout, CONFIG_ULOOP_TIMEOUT); ubus_add_uloop(conf.ubus_ctx); ubus_register_event_handler(conf.ubus_ctx, &conf.ubus_ev_sighup, CONFIG_UBUS_EV_SIGHUP); ubus_register_event_handler(conf.ubus_ctx, &conf.ubus_ev_shift_calc, CONFIG_UBUS_EV_SHIFT_CALC); uloop_run(); uloop_done(); goto finish; oom: fprintf(stderr, "error: Out of memory.\n"); finish: mosquitto_disconnect(conf.mosq); mosquitto_loop_stop(conf.mosq, false); mosquitto_destroy(conf.mosq); mosquitto_lib_cleanup(); ykw_free(conf.ykw); if (conf.ubus_ctx != NULL) { ubus_free(conf.ubus_ctx); } close(conf.flx_ufd.fd); uci_free_context(conf.uci_ctx); return rc; }
static void dncp_io_basic_2() { hncp_s h1, h2; dncp_s d1, d2; bool r; struct in6_addr a; char *msg = "foo"; char *ifname = LOOPBACK_NAME; (void)uloop_init(); memset(&h1, 0, sizeof(h1)); memset(&h2, 0, sizeof(h2)); memset(&d1, 0, sizeof(d1)); memset(&d2, 0, sizeof(d2)); h1.udp_port = 62000; h2.udp_port = 62001; h1.dncp = &d1; h2.dncp = &d2; d1.ext = &h1.ext; d2.ext = &h2.ext; r = hncp_io_init(&h1); sput_fail_unless(r, "dncp_io_init h1"); r = hncp_io_init(&h2); sput_fail_unless(r, "dncp_io_init h2"); /* Send a packet to ourselves */ (void)inet_pton(AF_INET6, "::1", &a); struct sockaddr_in6 src = { .sin6_family = AF_INET6, .sin6_port = htons(h1.udp_port), .sin6_addr = a #ifdef __APPLE__ , .sin6_len = sizeof(struct sockaddr_in6) #endif /* __APPLE__ */ }; struct sockaddr_in6 dst = { .sin6_family = AF_INET6, .sin6_port = htons(h2.udp_port), .sin6_addr = a #ifdef __APPLE__ , .sin6_len = sizeof(struct sockaddr_in6) #endif /* __APPLE__ */ }; smock_push_int("dncp_poll_io_recvfrom", 3); smock_push_int("dncp_poll_io_recvfrom_src", &src); smock_push_int("dncp_poll_io_recvfrom_dst", &dst); smock_push_int("dncp_poll_io_recvfrom_buf", msg); smock_push_int("dncp_poll_io_recvfrom_ifname", ifname); h1.ext.cb.send(&h1.ext, dncp_find_ep_by_name(h1.dncp, "lo"), NULL, &dst, msg, strlen(msg)); pending_packets++; uloop_run(); hncp_io_uninit(&h1); hncp_io_uninit(&h2); } int main(int argc, char **argv) { setbuf(stdout, NULL); /* so that it's in sync with stderr when redirected */ openlog("test_hncp_io", LOG_CONS | LOG_PERROR, LOG_DAEMON); sput_start_testing(); sput_enter_suite("hncp_io"); /* optional */ argc -= 1; argv += 1; sput_maybe_run_test(dncp_io_basic_2, do {} while(0)); sput_leave_suite(); /* optional */ sput_finish_testing(); return sput_get_return_value(); }
void iface_test_new_managed(void) { struct in_addr v4source = {INADDR_LOOPBACK}; iface_register_user(&user_mock); struct prefix p = {IN6ADDR_LOOPBACK_INIT, 0}; char test[] = "test"; struct iface *iface00 = iface_create("test00", "test00", 0); iface_update_ipv4_uplink(iface00); iface_set_ipv4_uplink(iface00, &v4source, 24); iface_commit_ipv4_uplink(iface00); /* smock_pull_bool_is("test00", false); */ /* this was removed in the commit a5293745c235a057b6a477ffbd817937eaa9bc12 at 5/2015(!); */ struct iface *iface = iface_create("test0", "test0", 0); iface->carrier = true; iface_discover_border(iface); sput_fail_unless(!!iface, "alloc managed"); struct iface *iface2 = iface_get("test0"); sput_fail_unless(iface == iface2, "get after create"); struct iface *iface3 = iface_create("test0", "test0", 0); sput_fail_unless(iface == iface3, "create after create"); /* smock_pull_bool_is("test0", false); */ /* this was removed in the commit a5293745c235a057b6a477ffbd817937eaa9bc12 at 5/2015(!); */ uloop_cancelled = false; uloop_run(); smock_pull_bool_is("test0", true); iface_update_ipv4_uplink(iface); iface_set_ipv4_uplink(iface, &v4source, 24); iface_commit_ipv4_uplink(iface); smock_pull_bool_is("test0", false); iface_update_ipv4_uplink(iface); iface_commit_ipv4_uplink(iface); uloop_cancelled = false; uloop_run(); smock_pull_bool_is("test0", true); iface_update_ipv6_uplink(iface); iface_add_delegated(iface, &p, NULL, HNETD_TIME_MAX, 0, test, sizeof(test)); iface_commit_ipv6_uplink(iface); smock_pull_bool_is("test0", false); sput_fail_unless(!prefix_cmp(&p, (struct prefix *)smock_pull("prefix_prefix")), "prefix address"); smock_pull_int64_is("prefix_valid", HNETD_TIME_MAX); smock_pull_int64_is("prefix_preferred", 0); sput_fail_unless(!strcmp(smock_pull("dhcpv6_data"), "test"), "dhcpv6_data"); smock_pull_int_is("dhcpv6_len", sizeof(test)); iface_update_ipv4_uplink(iface); iface_set_ipv4_uplink(iface, &v4source, 24); iface_commit_ipv4_uplink(iface); iface_update_ipv6_uplink(iface); iface_commit_ipv6_uplink(iface); smock_pull_bool_is("prefix_remove", true); iface_update_ipv4_uplink(iface); iface_commit_ipv4_uplink(iface); uloop_cancelled = false; uloop_run(); smock_pull_bool_is("test0", true); iface_remove(iface); sput_fail_unless(!iface_get("test0"), "delete"); smock_pull_bool_is("test0", false); smock_is_empty(); iface_unregister_user(&user_mock); }
int main(int argc, char **argv) { const char *socket = NULL; int ch; global_argv = argv; while ((ch = getopt(argc, argv, "d:s:p:c:h:r:l:S")) != -1) { switch(ch) { case 'd': debug_mask = strtoul(optarg, NULL, 0); break; case 's': socket = optarg; break; case 'p': main_path = optarg; break; case 'c': config_path = optarg; break; case 'h': hotplug_cmd_path = optarg; break; case 'r': resolv_conf = optarg; break; case 'l': log_level = atoi(optarg); if (log_level >= ARRAY_SIZE(log_class)) log_level = ARRAY_SIZE(log_class) - 1; break; #ifndef DUMMY_MODE case 'S': use_syslog = false; break; #endif default: return usage(argv[0]); } } if (use_syslog) openlog("netifd", 0, LOG_DAEMON); netifd_setup_signals(); if (netifd_ubus_init(socket) < 0) { fprintf(stderr, "Failed to connect to ubus\n"); return 1; } proto_shell_init(); wireless_init(); if (system_init()) { fprintf(stderr, "Failed to initialize system control\n"); return 1; } config_init_all(); uloop_run(); netifd_kill_processes(); netifd_ubus_done(); if (use_syslog) closelog(); return 0; }
void app_run(struct app *self){ ubus_add_uloop(self->ctx); uloop_run(); }
int main(int argc, char *argv[]) { int rc, c; int keep = 0; char *pid_filename = NULL; /* zero out main struct */ memset(&cshark, 0, sizeof(cshark)); /* preconfigure defaults */ cshark.interface = "any"; cshark.filename = NULL; cshark.snaplen = 65535; cshark.filter = NULL; cshark.packets = 0; cshark.limit_packets = 0; cshark.caplen = 0; cshark.limit_caplen = 0; openlog(PROJECT_NAME, LOG_PERROR | LOG_PID, LOG_DAEMON); while ((c = getopt(argc, argv, "i:w:s:T:P:S:p:kvh")) != -1) { switch (c) { case 'i': cshark.interface = optarg; break; case 'w': cshark.filename = strdup(optarg); if (!cshark.filename) { ERROR("not enough memory\n"); rc = EXIT_FAILURE; goto exit; } break; case 's': cshark.snaplen = atoi(optarg); if (!cshark.snaplen) cshark.snaplen = 65535; break; case 'T': { struct uloop_timeout dump_timeout = { .cb = dump_timeout_callback }; int dump_timeout_s = atoi(optarg); if (dump_timeout_s > 0) uloop_timeout_set(&dump_timeout, dump_timeout_s * 1000); break; } case 'P': cshark.limit_packets = atoi(optarg); break; case 'S': cshark.limit_caplen = atoi(optarg); break; case 'p': { pid_t pid = getpid(); pid_filename = optarg; FILE *f = fopen(pid_filename, "w"); if (!f) { fprintf(stderr, "Failed writing PID to '%s'\n", optarg); return EXIT_FAILURE; } fprintf(f, "%d\n", pid); fclose(f); sync(); break; } case 'k': keep = 1; break; case 'v': printf("%s version %s\n", PROJECT_NAME, PROJECT_VERSION); return EXIT_FAILURE; case 'h': show_help(); return EXIT_FAILURE; default: break; } } while (optind < argc) { asprintf(&cshark.filter, "%s %s", \ cshark.filter ? cshark.filter : "", argv[optind]); optind++; } rc = config_load(); if (rc) { ERROR("unable to load configuration\n"); rc = EXIT_FAILURE; goto exit; } if (!cshark.filename) { int len = 0; len = snprintf(cshark.filename, 0, "%s/cshark.pcap-XXXXXX", config.dir); cshark.filename = calloc(len + 1, sizeof(char)); if (!cshark.filename) { ERROR("not enough memory\n"); rc = EXIT_FAILURE; goto exit; } snprintf(cshark.filename, len + 1, "%s/cshark.pcap-XXXXXX", config.dir); int fd = mkstemp(cshark.filename); if (fd == -1) { ERROR("unable to create dump file\n"); rc = EXIT_FAILURE; goto exit; } } uloop_init(); rc = cshark_pcap_init(&cshark); if (rc) { rc = EXIT_FAILURE; goto exit; } printf("capturing traffic to file: '%s' ...\n", cshark.filename); uloop_run(); cshark_pcap_done(&cshark); printf("\n%lu packets captured\n", (long unsigned int) cshark.packets); rc = cshark_uclient_init(&cshark); if (rc) { rc = EXIT_FAILURE; goto exit; } printf("uploading capture ...\n"); uloop_run(); uloop_done(); rc = EXIT_SUCCESS; exit: cshark_pcap_done(&cshark); cshark_uclient_done(&cshark); if (!keep) remove(cshark.filename); free(cshark.filename); if (pid_filename) remove(pid_filename); return rc; }
int main(int argc, char **argv, char **envp) { char *json = NULL; int status, ch, policy = EPERM; pid_t child; while ((ch = getopt(argc, argv, "f:p:")) != -1) { switch (ch) { case 'f': json = optarg; break; case 'p': policy = atoi(optarg); break; } } argc -= optind; argv += optind; if (!argc) return -1; if (getenv("TRACE_DEBUG")) debug = 1; unsetenv("TRACE_DEBUG"); child = fork(); if (child == 0) { char **_argv = calloc(argc + 1, sizeof(char *)); char **_envp; char preload[] = "LD_PRELOAD=/lib/libpreload-trace.so"; int envc = 1; int ret; memcpy(_argv, argv, argc * sizeof(char *)); while (envp[envc++]) ; _envp = calloc(envc, sizeof(char *)); memcpy(&_envp[1], _envp, envc * sizeof(char *)); *envp = preload; ret = execve(_argv[0], _argv, envp); ERROR("failed to exec %s: %s\n", _argv[0], strerror(errno)); return ret; } if (child < 0) return -1; syscall_max = ARRAY_SIZE(syscall_names); syscall_count = calloc(syscall_max, sizeof(int)); waitpid(child, &status, 0); if (!WIFSTOPPED(status)) { ERROR("failed to start %s\n", *argv); return -1; } ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACESYSGOOD); ptrace(PTRACE_SYSCALL, child, 0, 0); uloop_init(); tracer.pid = child; tracer.cb = tracer_cb; uloop_process_add(&tracer); uloop_run(); uloop_done(); if (!json) if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0) ERROR("failed to allocate output path: %s\n", strerror(errno)); print_syscalls(policy, json); return 0; }
int main() { uloop_init(); uloop_timeout_set(&server_time, 2000); uloop_run(); return 0; }
int main(int argc, char **argv) { static struct ubus_request req; struct ubus_context *ctx; uint32_t id; const char *ubus_socket = NULL; int ch, ret, lines = 0; static struct blob_buf b; int tries = 5; signal(SIGPIPE, SIG_IGN); while ((ch = getopt(argc, argv, "u0fcs:l:r:F:p:S:P:h:")) != -1) { switch (ch) { case 'u': log_udp = 1; break; case '0': log_trailer_null = 1; break; case 's': ubus_socket = optarg; break; case 'r': log_ip = optarg++; log_port = argv[optind++]; break; case 'F': log_file = optarg; break; case 'p': pid_file = optarg; break; case 'P': log_prefix = optarg; break; case 'f': log_follow = 1; break; case 'l': lines = atoi(optarg); break; case 'S': log_size = atoi(optarg); if (log_size < 1) log_size = 1; log_size *= 1024; break; case 'h': hostname = optarg; break; default: return usage(*argv); } } uloop_init(); ctx = ubus_connect(ubus_socket); if (!ctx) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } ubus_add_uloop(ctx); /* ugly ugly ugly ... we need a real reconnect logic */ do { ret = ubus_lookup_id(ctx, "log", &id); if (ret) { fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret)); sleep(1); continue; } blob_buf_init(&b, 0); if (lines) blobmsg_add_u32(&b, "lines", lines); else if (log_follow) blobmsg_add_u32(&b, "lines", 0); if (log_follow) { if (pid_file) { FILE *fp = fopen(pid_file, "w+"); if (fp) { fprintf(fp, "%d", getpid()); fclose(fp); } } } if (log_ip && log_port) { openlog("logread", LOG_PID, LOG_DAEMON); log_type = LOG_NET; sender.cb = log_handle_fd; retry.cb = log_handle_reconnect; uloop_timeout_set(&retry, 1000); } else if (log_file) { log_type = LOG_FILE; sender.fd = open(log_file, O_CREAT | O_WRONLY| O_APPEND, 0600); if (sender.fd < 0) { fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno)); exit(-1); } } else { sender.fd = STDOUT_FILENO; } ubus_invoke_async(ctx, id, "read", b.head, &req); req.fd_cb = logread_fd_cb; ubus_complete_request_async(ctx, &req); uloop_run(); ubus_free(ctx); uloop_done(); } while (ret && tries--); return ret; }
int main (int argc, char **argv) { int c; int start_event = 0; bool foreground = false; while (1) { c = getopt_long(argc, argv, "fhbgv", long_opts, NULL); if (c == EOF) break; switch (c) { case 'b': start_event |= START_BOOT; break; case 'f': foreground = true; break; case 'g': start_event |= START_GET_RPC_METHOD; break; case 'h': print_help(); exit(EXIT_SUCCESS); case 'v': print_version(); exit(EXIT_SUCCESS); default: print_help(); exit(EXIT_FAILURE); } } log_message(NAME, L_DEBUG, "daemon started\n"); setlocale(LC_CTYPE, ""); umask(0037); if (getuid() != 0) { D("run %s as root\n", NAME); exit(EXIT_FAILURE); } /* run early cwmp initialization */ cwmp = calloc(1, sizeof(struct cwmp_internal)); if (!cwmp) return -1; INIT_LIST_HEAD(&cwmp->events); INIT_LIST_HEAD(&cwmp->notifications); INIT_LIST_HEAD(&cwmp->downloads); INIT_LIST_HEAD(&cwmp->scheduled_informs); uloop_init(); backup_init(); config_load(); cwmp_init_deviceid(); if (start_event & START_BOOT) { cwmp_add_event(EVENT_BOOT, NULL, 0, EVENT_BACKUP); cwmp_add_inform_timer(); } if (start_event & START_GET_RPC_METHOD) { cwmp->get_rpc_methods = true; cwmp_add_event(EVENT_PERIODIC, NULL, 0, EVENT_BACKUP); cwmp_add_inform_timer(); } if (netlink_init()) { D("netlink initialization failed\n"); exit(EXIT_FAILURE); } if (ubus_init()) D("ubus initialization failed\n"); http_server_init(); pid_t pid, sid; if (!foreground) { pid = fork(); if (pid < 0) exit(EXIT_FAILURE); if (pid > 0) exit(EXIT_SUCCESS); sid = setsid(); if (sid < 0) { D("setsid() returned error\n"); exit(EXIT_FAILURE); } char *directory = "/"; if ((chdir(directory)) < 0) { D("chdir() returned error\n"); exit(EXIT_FAILURE); } } log_message(NAME, L_DEBUG, "entering main loop\n"); uloop_run(); ubus_exit(); uloop_done(); http_client_exit(); xml_exit(); config_exit(); cwmp_free_deviceid(); free(cwmp); closelog(); log_message(NAME, L_DEBUG, "exiting\n"); return 0; }
/** * Nodewatcher agent entry point. */ int main(int argc, char **argv) { struct stat s; const char *ubus_socket = NULL; int log_option = 0; int c; while ((c = getopt(argc, argv, "s:")) != -1) { switch (c) { case 's': ubus_socket = optarg; break; case 'f': log_option |= LOG_PERROR; break; default: break; } } /* Open the syslog facility */ openlog("nw-agent", log_option, LOG_DAEMON); /* Create directory for temporary run files */ if (stat("/var/run/nodewatcher-agent", &s)) mkdir("/var/run/nodewatcher-agent", 0700); umask(0077); /* Setup signal handlers */ signal(SIGPIPE, SIG_IGN); /* TODO: Handle SIGHUP to reload? */ /* Seed random generator */ unsigned int seed; int rc = nw_read_random_bytes(&seed, sizeof(seed)); if (rc < 0) { fprintf(stderr, "ERROR: Failed to seed random generator!\n"); return -1; } srandom(seed); /* Initialize event loop */ uloop_init(); /* Attempt to establish connection to ubus daemon */ for (;;) { ubus = ubus_connect(ubus_socket); if (!ubus) { syslog(LOG_WARNING, "Failed to connect to ubus!"); sleep(10); continue; } break; } ubus_add_uloop(ubus); /* Initialize UCI context */ uci = uci_alloc_context(); if (!uci) { syslog(LOG_ERR, "Failed to initialize UCI!"); return -1; } /* Discover and initialize modules */ if (nw_module_init(ubus, uci) != 0) { syslog(LOG_ERR, "Unable to initialize modules!"); return -1; } /* Initialize the scheduler */ if (nw_scheduler_init() != 0) { syslog(LOG_ERR, "Unable to initialize scheduler!"); return -1; } /* Initialize the output exporter */ if (nw_output_init(uci) != 0) { syslog(LOG_ERR, "Unable to initialize output exporter!"); return -1; } /* Enter the event loop */ uloop_run(); ubus_free(ubus); uci_free_context(uci); uloop_done(); return 0; }