Ejemplo n.º 1
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;
}
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;
} 
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
0
static void ubus_destroy(struct ubus_context *ubus)
{
  // Forces re-initialization when we're reusing the same definitions later on.
  ubus_object.id = 0;
  ubus_object_type.id = 0;

  ubus_free(ubus);
  daemon->ubus = NULL;
}
Ejemplo n.º 6
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;
}
Ejemplo n.º 7
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;
}
Ejemplo n.º 8
0
UBusThread::~UBusThread()
{
    ILOG_TRACE(UBUS_THREAD);

    ubus_free(_ubus);
    uloop_done();

    MGuiRil::Destroy();
    MGuiCharger::Destroy();
    MGuiWifi::Destroy();
    MGuiStats::Destroy();
    MGuiHawk::Destroy();

    ILOG_DEBUG(UBUS_THREAD, "%s exit\n", __func__);
}
Ejemplo n.º 9
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;
}
Ejemplo n.º 10
0
void
ubus_exit(void)
{
	if (ctx) ubus_free(ctx);
}
Ejemplo n.º 11
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;
}
Ejemplo n.º 12
0
void loop_detect_ubus_done(void)
{
	ubus_free(ctx);
	uloop_done();
}
Ejemplo n.º 13
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;
}
Ejemplo n.º 14
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
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;
}
Ejemplo n.º 16
0
void app_destroy(struct app *self){
	ubus_free(self->ctx); 
}
Ejemplo n.º 17
0
void
ubus_exit(void)
{
	if (ubus) ubus_free(ubus);
}