void ubus_init() { struct ubus_context *ubus = NULL; int ret = 0; ubus = ubus_connect(NULL); if (!ubus) { if (!error_logged) { my_syslog(LOG_ERR, _("Cannot initialize UBus: connection failed")); error_logged = 1; } ubus_destroy(ubus); return; } ret = ubus_add_object(ubus, &ubus_object); if (ret) { if (!error_logged) { my_syslog(LOG_ERR, _("Cannot add object to UBus: %s"), ubus_strerror(ret)); error_logged = 1; } return; } ubus->connection_lost = ubus_disconnect_cb; daemon->ubus = ubus; error_logged = 0; my_syslog(LOG_INFO, _("Connected to system UBus")); }
int UBusRequest::InvokeAsync(const char *method, blob_attr *b) { ubus_request *req; int ret = 0; ILOG_TRACE(UBUS_REQ); ILOG_DEBUG(UBUS_REQ, "%s enter\n", __func__); req = (ubus_request *)malloc(sizeof(*req)); if (!req) { ILOG_ERROR(UBUS_REQ,"memory allocation failed\n"); return -1; } ret = ubus_invoke_async(_ubus, _id, method, b, req); if (ret) { ILOG_ERROR(UBUS_REQ, "ubus_invoke failed [id=%d, method=%s, err=%s]\n", _id, method, ubus_strerror(ret)); free(req); return ret; } ILOG_DEBUG(UBUS_REQ,"ubus_invoke_async done [id=%d, method=%s]\n", _id, method); req->data_cb = data_cb; req->complete_cb = complete_cb; req->priv = this; ubus_complete_request_async(_ubus, req); ILOG_DEBUG(UBUS_REQ, "ubus_complete_request_async done\n"); return 0; }
void vlan_add_to_ubus(void) { int ret; ret = ubus_add_object(ctx, &vlan_object); if (ret) ERROR(GLOBAL_OUT_GROUP,"Failed to add port object: %s\n", ubus_strerror(ret)); };
static void server_main(void) { int ret; ret = ubus_add_object(ctx, &apClient_object); if (ret) fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret)); uloop_run(); }
static void ubus_connect_handler(struct ubus_context *ctx) { int ret; ret = ubus_add_object(ctx, &gps_object); if (ret) fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret)); }
void lowlevel_add_to_ubus(void) { int ret; ret = ubus_add_object(ctx, &lowlevel_object); if (ret) ERROR(GLOBAL_OUT_GROUP,"Failed to add port authorized object: %s\n", ubus_strerror(ret)); };
int UBusRequest::Invoke(const char *method, blob_attr *b) { int ret = 0; ILOG_TRACE(UBUS_REQ); ILOG_DEBUG(UBUS_REQ, "%s enter\n", __func__); ret = ubus_invoke(_ubus, _id, method, b , data_cb, this, 0); if (ret) { ILOG_ERROR(UBUS_REQ,"ubus_invoke failed [id=%d, method=%s, err=%s]\n", _id, method, ubus_strerror(ret)); return ret; } ILOG_DEBUG(UBUS_REQ,"ubus_invoke done [id=%d, method=%s]\n", _id, method, ubus_strerror(ret)); ILOG_DEBUG(UBUS_REQ, "%s exit\n", __func__); return 0; }
static void ubus_disconnect_cb(struct ubus_context *ubus) { int ret; ret = ubus_reconnect(ubus, NULL); if (ret) { my_syslog(LOG_ERR, _("Cannot reconnect to UBus: %s"), ubus_strerror(ret)); ubus_destroy(ubus); } }
void procd_bcast_event(char *event, struct blob_attr *msg) { int ret; if (!notify) return; ret = ubus_notify(_ctx, &system_object, event, msg, -1); if (ret) fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret)); }
void ubus_init_system(struct ubus_context *ctx) { struct stat s; int ret; if (stat("/sbin/upgraded", &s)) system_object.n_methods -= 1; _ctx = ctx; ret = ubus_add_object(ctx, &system_object); if (ret) ERROR("Failed to add object: %s\n", ubus_strerror(ret)); }
int main(int argc, char **argv) { const char *ubus_socket = NULL; static struct ubus_context *ctx; char ubus_event[128] = ""; const char * ubusevent = NULL; char *cmd; int ret = 0; int i, ch; while ((ch = getopt(argc, argv, "r:vs:t:S")) != -1) { switch (ch) { case 's': ubus_socket = optarg; break; case 't': timeout = atoi(optarg); break; case 'S': simple_output = true; break; case 'r': strncpy(ubus_event, optarg, sizeof ubus_event); ubus_event[(sizeof ubus_event) - 1] = '\0'; ubusevent = ubus_event; break; case 'v': verbose++; break; default: return usage("ubus-linknx"); } } ctx = ubus_connect(ubus_socket); if (!ctx) { if (!simple_output) fprintf(stderr, "Failed to connect to ubus\n"); return -1; } ret = -2; ret = ubus_cli_listen(ctx,ubusevent); if (ret > 0) fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret)); ubus_free(ctx); 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; }
void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface) { struct ubus_context *ubus = (struct ubus_context *)daemon->ubus; int ret; if (!ubus || !notify) return; blob_buf_init(&b, BLOBMSG_TYPE_TABLE); if (mac) blobmsg_add_string(&b, "mac", mac); if (ip) blobmsg_add_string(&b, "ip", ip); if (name) blobmsg_add_string(&b, "name", name); if (interface) blobmsg_add_string(&b, "interface", interface); ret = ubus_notify(ubus, &ubus_object, type, b.head, -1); if (!ret) my_syslog(LOG_ERR, _("Failed to send UBus event: %s"), ubus_strerror(ret)); }
int UBusSubscriber::UnSubscribe(std::string &event) { uint32_t id; int ret; ILOG_TRACE(UBUS_SUBSCRIBER); if (ubus_lookup_id(_ubus, event.c_str(), &id)) { ILOG_ERROR(UBUS_SUBSCRIBER, "ubus lookup id %s failed\n", event.c_str()); return -1; } ret = ubus_unsubscribe(_ubus, &_subscriber, id); if (ret) { ILOG_ERROR(UBUS_SUBSCRIBER,"event %s unsubscribe failed (err: %s)\n", event.c_str(), ubus_strerror(ret)); return ret; } ILOG_DEBUG(UBUS_SUBSCRIBER, "unsubscribe event %s success\n", event.c_str()); 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) { 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; }