Beispiel #1
1
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"));
}
Beispiel #2
0
void* client_thread(void *args){
	struct ubus_context *ctx = ubus_new("client", NULL, NULL); 
		
	if(ubus_connect(ctx, "test.sock", NULL) < 0){
		printf("Error connecting to ubus socket!\n"); 
		return 0; 
	}

	struct ubus_object *obj = ubus_object_new("test"); 
	struct ubus_method *method = ubus_method_new("my.object.test", test_method); 
	ubus_method_add_param(method, "name_int", "i"); 
	ubus_method_add_param(method, "name_string", "ai"); 
	ubus_method_add_param(method, "name_table", "a{sv}"); 
	ubus_method_add_return(method, "some_return", "i"); 
	ubus_method_add_return(method, "some_table", "a{sv}"); // returns a dictionary
	
	ubus_object_add_method(obj, &method); 
	//ubus_add_object(ctx, &obj); 

	while(true){
		ubus_handle_events(ctx); 
	}

	ubus_delete(&ctx); 
}
Beispiel #3
0
int main(int argc, char **argv) {
	const char *ubus_socket = "/var/run/ubus.sock";
	struct ubus_context *ctx = ubus_connect(ubus_socket);
	if (!ctx) {
		fprintf(stderr, "Failed to connect to ubus\n");
		return -1;
	}

	memset(&listener, 0, sizeof(listener));
	listener.cb = receive_event;
	int ret = ubus_register_event_handler(ctx, &listener, "foo");
	if (ret)
		return ret;

	struct event_base *evloop = event_base_new();
	struct event *signal_int = evsignal_new(evloop, SIGINT, signal_cb, evloop);
	event_add(signal_int, NULL);

	struct event *e = event_new(evloop, ctx->sock.fd, EV_READ | EV_PERSIST, cb, ctx);
	event_add(e, NULL);

	event_base_dispatch(evloop);

	event_free(signal_int);
	event_free(e);
	event_base_free(evloop);

	ubus_free(ctx);
	return 0;
}
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;
}
Beispiel #6
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__);
}
Beispiel #7
0
bool
fw3_ubus_connect(void)
{
	bool status = false;
	uint32_t id;
	struct ubus_context *ctx = ubus_connect(NULL);
	struct blob_buf b = { };

	if (!ctx)
		goto out;

	if (ubus_lookup_id(ctx, "network.interface", &id))
		goto out;

	if (ubus_invoke(ctx, id, "dump", NULL, dump_cb, NULL, 2000))
		goto out;

	status = true;

	if (ubus_lookup_id(ctx, "service", &id))
		goto out;

	blob_buf_init(&b, 0);
	blobmsg_add_string(&b, "type", "firewall");
	ubus_invoke(ctx, id, "get_data", b.head, procd_data_cb, NULL, 2000);
	blob_buf_free(&b);

out:
	if (ctx)
		ubus_free(ctx);
	return status;
}
Beispiel #8
0
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;
}
Beispiel #9
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;
}
Beispiel #10
0
int app_connect_to_ubus(struct app *self, const char *path){
	self->ctx = ubus_connect(path); 
	if(!self->ctx) {
		perror("could not connect to ubus!\n"); 
		return -EIO; 
	}

	DEBUG("connected as %x\n", self->ctx->local_id); 
	
	self->ctx->connection_lost = on_ubus_connection_lost; 	
	return 0; 
}
Beispiel #11
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 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;
}
Beispiel #13
0
int loop_detect_ubus_init(const char *path)
{
	uloop_init();
	ubus_path = path; 
	
	ctx = ubus_connect(ubus_path);
	if (!ctx) 
		return -EIO;

	INFO(LR_OUT_GROUP1,"connected as %08x(%s)\n",ctx->local_id,ubus_path);
	ctx->connection_lost = loop_detect_ubus_connection_lost;
	loop_detect_ubus_add_fd();

	//TODO: add object
    loop_detect_add_to_ubus(ctx);

	return 0;
}
Beispiel #14
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;

}
Beispiel #15
0
static int client_ubus_process(char *ubus_object, char *ubus_method, char *argv)
{
  static struct ubus_request req;
  uint32_t id;
  int ret, ret_ubus_invoke;
  const char *ubus_socket = NULL;
  struct ubus_context *ctx_local;

  ctx_local = ubus_connect(ubus_socket);
  if (!ctx_local) {
    printf("Failed to connect to ubus\n");
    return -1;
  }

  if (ubus_lookup_id(ctx_local, ubus_object, &id)) {
    printf("Failed to look up test object\n");
    return -1;
  }

  blob_buf_init(&b_local, 0);

  blobmsg_add_string(&b_local, "cmd", argv);

  ret_ubus_invoke = ubus_invoke(ctx_local, id, ubus_method, b_local.head, receive_call_result_data, 0, 20000);
 
  if(ret_ubus_invoke == 0 || ret_ubus_invoke == 7)
  {
    ret = 0;
  }
  else
  {
    ret = -1;
  }

  ubus_free(ctx_local);
  return ret;
}
Beispiel #16
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;
}
Beispiel #17
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;
}
int main(int argc, char **argv)
{
	struct lws_context_creation_info info;
	char interface_name[128] = "";
	//unsigned int ms, oldms = 0;
	const char *iface = NULL;
	const char *ssl_cert = NULL;
	const char *ssl_private_key = NULL;
	char cert_path[1024];
	char key_path[1024];
	int use_ssl = 0;
	int opts = 0;
	int n = 0;
	//int daemonize = 0;

	/*
	 * take care to zero down the info struct, he contains random garbaage
	 * from the stack otherwise
	 */
	memset(&info, 0, sizeof info);
	info.port = 7681;

	/*
	 * define ubus struct
	 */
	const char *ubus_socket = NULL;
	struct ubus_context *ctx;
	
	while (n >= 0) {
		n = getopt_long(argc, argv, "r:x:i:u:hsc:k:p:", options, NULL);
		if (n < 0)
			continue;
		switch (n) {
		//case 'd':
		//	debug_level = atoi(optarg);
		//	break;
		case 's':
			use_ssl = 1;
			break;
		case 'c':
			ssl_cert = optarg;
			printf("Setting ssl_cert path to \"%s\"\n", ssl_cert);
			break;
		case 'k':
			ssl_private_key = optarg;
			printf("Setting ssl_private_key path to \"%s\"\n", ssl_private_key);
			break;
		case 'p':
			info.port = atoi(optarg);
			break;
		case 'i':
			strncpy(interface_name, optarg, sizeof interface_name);
			interface_name[(sizeof interface_name) - 1] = '\0';
			iface = interface_name;
			break;
		case 'x':
			strncpy(ubus_xevent, optarg, sizeof ubus_xevent);
			ubus_xevent[(sizeof ubus_xevent) - 1] = '\0';
			ubusxevent = ubus_xevent;
			break;
		case 'h':
			fprintf(stderr, "Usage: ws-mirror-server "
						"-x <x> -[--port=<p>] [--ssl]\n"
						"-c <c> ssl_cert path -k <k> ssl_private_key path\n"
						"-x <x> ubus send path from ws e.g. \"linknxws\" or \"ws\"\n");
			exit(1);
		}
	}
	
	signal(SIGINT, sighandler);
	
	/* Create ubus conection */
	ctx = ubus_connect(ubus_socket);
	if (!ctx) {
		lwsl_err("Failed to connect to ubus\n");
		return -1;
	}

	info.iface = iface;
	info.protocols = protocols;

	if (!use_ssl) {
		info.ssl_cert_filepath = NULL;
		info.ssl_private_key_filepath = NULL;
	} else {
		if (strlen(ssl_cert) > sizeof(cert_path) - 32) {
			lwsl_err("resource path too long\n");
			return -1;
		}
		sprintf(cert_path, "%s",ssl_cert);
		if (strlen(ssl_private_key) > sizeof(key_path) - 32) {
			lwsl_err("resource path too long\n");
			return -1;
		}
		sprintf(key_path, "%s",ssl_private_key);

		info.ssl_cert_filepath = cert_path;
		info.ssl_private_key_filepath = key_path;
	}	
	info.gid = -1;
	info.uid = -1;
	info.max_http_header_pool = 1;
	info.options = opts;
	info.extensions = exts;
	context = lws_create_context(&info);
	if (context == NULL) {
		lwsl_err("libwebsocket init failed\n");
		return -1;
	}

	n = 0;
	while (n >= 0 && !force_exit) {
		struct timeval tv;
		gettimeofday(&tv, NULL);
		n = lws_service(context, 50);
		if (n < 0) {
		    lwsl_err("Unable to fork service loop %d\n", n);
		    return -1;
		}
	}

	lws_context_destroy(context);
	lwsl_notice("libwebsockets-test-server exited cleanly\n");
	return 0;
}
int
callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
			void *user, void *in, size_t len)
{
	struct per_session_data__lws_mirror *pss =
			(struct per_session_data__lws_mirror *)user;
	int m,n;

	switch (reason) {

	case LWS_CALLBACK_ESTABLISHED:
		lwsl_info("callback_lws_mirror: LWS_CALLBACK_ESTABLISHED\n", __func__);
		pss->ringbuffer_tail = ringbuffer_head;
		pss->wsi = wsi;
		break;

	case LWS_CALLBACK_PROTOCOL_DESTROY:
		lwsl_notice("mirror protocol cleaning up\n");
		for (n = 0; n < sizeof ringbuffer / sizeof ringbuffer[0]; n++)
			if (ringbuffer[n].payload)
				free(ringbuffer[n].payload);
		break;

	case LWS_CALLBACK_SERVER_WRITEABLE:
		if (close_testing)
			break;
		while (pss->ringbuffer_tail != ringbuffer_head) {
			m = ringbuffer[pss->ringbuffer_tail].len;
			n = lws_write(wsi, (unsigned char *)
					ringbuffer[pss->ringbuffer_tail].payload +
					LWS_PRE, m, LWS_WRITE_TEXT);
			if (n < 0) {
				lwsl_err("ERROR %d writing to mirror socket\n", n);
				return -1;
			}
			if (n < m)
				lwsl_err("mirror partial write %d vs %d\n", n, m);

			if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
				pss->ringbuffer_tail = 0;
			else
				pss->ringbuffer_tail++;

			if (((ringbuffer_head - pss->ringbuffer_tail) &
				  (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15))
				lws_rx_flow_allow_all_protocol(lws_get_context(wsi),
						lws_get_protocol(wsi));

			if (lws_send_pipe_choked(wsi)) {
				lws_callback_on_writable(wsi);
				break;
			}
			/*
			 * for tests with chrome on same machine as client and
			 * server, this is needed to stop chrome choking
			 */
			usleep(1);
		}
		break;

	case LWS_CALLBACK_RECEIVE:

		if (((ringbuffer_head - pss->ringbuffer_tail) &
				  (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 1)) {
			lwsl_err("dropping!\n");
			goto choke;
		}

		if (ringbuffer[ringbuffer_head].payload)
			free(ringbuffer[ringbuffer_head].payload);

		ringbuffer[ringbuffer_head].payload =
				malloc(LWS_SEND_BUFFER_PRE_PADDING + len +
						  LWS_SEND_BUFFER_POST_PADDING);
		ringbuffer[ringbuffer_head].len = len;
		memcpy((char *)ringbuffer[ringbuffer_head].payload +
					  LWS_SEND_BUFFER_PRE_PADDING, in, len);
		if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
			ringbuffer_head = 0;
		else
			ringbuffer_head++;

		if (((ringbuffer_head - pss->ringbuffer_tail) &
				  (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2))
			goto done;

choke:
		lwsl_debug("LWS_CALLBACK_RECEIVE: throttling %p\n", wsi);
		lws_rx_flow_control(wsi, 0);

done:
		lws_callback_on_writable_all_protocol(lws_get_context(wsi),
						lws_get_protocol(wsi));

		const char *ubus_socket = NULL;
		struct ubus_context *ctx;
		static struct blob_buf b;
		ctx = ubus_connect(ubus_socket);
		if (!ctx) {
			fprintf(stderr, "Failed to connect to ubus\n");
			return -1;
		}		
		blob_buf_init(&b, 0);
		blobmsg_add_json_from_string(&b, (char *)in);
		ubus_send_event(ctx, ubusxevent, b.head);
		ubus_free(ctx);

		break;
	/*
	 * this just demonstrates how to use the protocol filter. If you won't
	 * study and reject connections based on header content, you don't need
	 * to handle this callback
	 */

	case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
		dump_handshake_info(wsi);
		/* you could return non-zero here and kill the connection */
		break;

	default:
		break;
	}

	return 0;
}
Beispiel #20
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;
}