int main(int argc, char **argv) { const char *ubus_socket = NULL; int ch; while ((ch = getopt(argc, argv, "cs:")) != -1) { switch (ch) { case 's': ubus_socket = optarg; break; default: break; } } argc -= optind; argv += optind; uloop_init(); //N1. Connect the process to ubus daemon ctx = ubus_connect(ubus_socket); if (!ctx) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } //N2. Add connected fd into epoll fd set ubus_add_uloop(ctx); //N4. Add notify object onto bus ubus_add_object(ctx, & test_object); //N5. Prepare notify type and arguments when actually an event happens …… event_ broadcast(event); ubus_free(ctx); uloop_done(); return 0; }
int main(int argc, char **argv) { while (isStaGetIP("apcli0") == 0) { sleep(1); } uloop_init(); signal(SIGPIPE, SIG_IGN); openlog("checkupgrade", 0, 0); ctx = ubus_connect(ubus_socket); if (!ctx) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } ubus_add_uloop(ctx); server_main(); ubus_free(ctx); uloop_done(); return 0; }
UBusThread::UBusThread(StatusBar* bar, HomeScreen* home, HawkScreen* hawk, StatScreen* stat, int fd) : Thread(), _bar(bar), _home(home), _hawk(hawk), _stat(stat) { ILOG_TRACE(UBUS_THREAD); uloop_init(); _ubus = ubus_connect(NULL); if (!_ubus) ILOG_THROW(UBUS_THREAD, "Failed to connect to ubus\n"); ubus_add_uloop(_ubus); _ubus_fd.cb = UBusThreadEvent; _ubus_fd.fd = fd; MGuiRil::Create(_ubus, _bar, _home); MGuiCharger::Create(_ubus, _bar); MGuiWifi::Create(_ubus, _bar, _home); MGuiStats::Create(_ubus, _bar, _home); MGuiHawk::Create(_ubus, _hawk); ILOG_DEBUG(UBUS_THREAD, "%s exit\n", __func__); }
int main(int argc, char **argv) { struct stat s; const char *hangup; const char *ubus_socket = NULL; int ch; while ((ch = getopt(argc, argv, "s:")) != -1) { switch (ch) { case 's': ubus_socket = optarg; break; default: break; } } if (stat("/var/run/rpcd", &s)) mkdir("/var/run/rpcd", 0700); umask(0077); signal(SIGPIPE, SIG_IGN); signal(SIGHUP, handle_signal); signal(SIGUSR1, handle_signal); uloop_init(); ctx = ubus_connect(ubus_socket); if (!ctx) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } ubus_add_uloop(ctx); rpc_session_api_init(ctx); rpc_uci_api_init(ctx); rpc_plugin_api_init(ctx); hangup = getenv("RPC_HANGUP"); if (!hangup || strcmp(hangup, "1")) rpc_uci_purge_savedirs(); else rpc_session_thaw(); uloop_run(); ubus_free(ctx); uloop_done(); if (respawn) exec_self(argc, argv); return 0; }
int ubus_init(void) { ctx = ubus_connect(config->local->ubus_socket); if (!ctx) return -1; ubus_add_uloop(ctx); if (ubus_add_object(ctx, &main_object)) return -1; return 0; }
int ubus_init(void) { ubus = ubus_connect(NULL); if (!ubus) return -1; ubus_add_uloop(ubus); if (ubus_add_object(ubus, &main_object)) return -1; return 0; }
int cloudc_ubus_init(void) { int ret = -1; const char *ubus_socket = NULL; cloudc_debug("%s[%d]: Enter ", __func__, __LINE__); uloop_init(); /* ubus init */ cloudc_ctx = ubus_connect(ubus_socket); if (!cloudc_ctx) { cloudc_error(stderr, "Failed to connect to ubus"); return -1; } else { cloudc_debug("%s[%d]: connect to ubus succeed, cloudc_ctx = %p", __func__, __LINE__, cloudc_ctx); } /* add connected fd into epoll fd set */ ubus_add_uloop(cloudc_ctx); /* add ubus object */ ret = ubus_add_object(cloudc_ctx, &cloudc_object); if (ret) { cloudc_error(stderr, "Failed to add object: %s", ubus_strerror(ret)); } else { cloudc_debug("%s[%d]: ubus add object successfully, ret = %d", __func__, __LINE__, ret); } cloudc_debug("%s[%d]: Exit ", __func__, __LINE__); 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) { 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; }
void app_run(struct app *self){ ubus_add_uloop(self->ctx); uloop_run(); }
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; }
static void loop_detect_ubus_add_fd(void) { ubus_add_uloop(ctx); }
/** * 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; }