Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
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;
}