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 main(int argc, char ** argv){ if(argc<2){ fprintf(stderr,"usage: ubus-signal /path/to/echo.method\n"); exit(1); } ubus_t * service=ubus_create(resolved_bus_path(argv[1])); if(service==0){ perror("ubus_create"); exit(errno); } fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK); fd_set rfds; for(;;) { FD_ZERO (&rfds); FD_SET (0, &rfds); int maxfd=ubus_select_all(service,&rfds); if (select(maxfd+2, &rfds, NULL, NULL, NULL) < 0){ perror("select"); exit(1); } ubus_activate_all(service,&rfds,0); ubus_chan_t * chan=0; while((chan=ubus_ready_chan(service))){ char buff [200]; int len=ubus_read(chan,&buff,200); if(len<1){ ubus_disconnect(chan); }else{ write(1,&buff,len); } } if(FD_ISSET (0, &rfds)){ char buff [100]; int n=read(0,&buff,100); if(n==0){ ubus_destroy(service); exit(0); } else if(n<1){ perror("read"); ubus_destroy(service); exit(errno); } if (ubus_broadcast(service, &buff, n) < 1) { perror("send"); exit(errno); } } } ubus_destroy(service); return 0; }
void check_ubus_listeners() { struct ubus_context *ubus = (struct ubus_context *)daemon->ubus; if (!ubus) { if (!error_logged) { my_syslog(LOG_ERR, _("Cannot poll UBus listeners: no connection")); error_logged = 1; } return; } error_logged = 0; if (poll_check(ubus->sock.fd, POLLIN)) ubus_handle_event(ubus); if (poll_check(ubus->sock.fd, POLLHUP | POLLERR)) { my_syslog(LOG_INFO, _("Disconnecting from UBus")); ubus_destroy(ubus); } }
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); } }
int main(int argc, char ** argv){ signal(SIGPIPE, SIG_IGN); if (argc<2) { fprintf(stderr, "usage: clock /path/to/clock.signal\n"); exit(1); } ubus_t *service = ubus_create(argv[1]); if (service == 0){ perror("ubus_create"); exit(0); } fd_set rfds; char buff [2000]; int len = 0;; for (;;) { struct timeval tv; bzero(&tv, sizeof(struct timeval)); tv.tv_sec = 60; FD_ZERO (&rfds); int maxfd = ubus_select_all(service, &rfds); if (select(maxfd + 2, &rfds, NULL, NULL, &tv) < 0) { perror("select"); exit(1); } ubus_activate_all(service, &rfds, UBUS_IGNORE_INBOUND); ubus_chan_t *chan=0; while ((chan = ubus_fresh_chan(service))) { ubus_write(chan, &buff, len); } FILE *f = popen("date +%H:%M","r"); char c = fgetc(f); while(c != EOF){ ubus_broadcast(service, &c, 1); c = fgetc(f); } pclose(f); } ubus_destroy(service); return 0; }