示例#1
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;
}
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;
} 
示例#3
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__);
}
示例#4
0
int main(int argc, char **argv)//内核发生热插拔时会执行一次/proc/sys/kernel/hotplug。
{///sbin/mdev > /proc/sys/kernel/hotplug,本质上内核发生一次热插拔时执行一次/sbin/mdev
	int ch;
	char *dbglvl = getenv("DBGLVL");

	if (dbglvl) {
		debug = atoi(dbglvl);
		unsetenv("DBGLVL");
	}

	while ((ch = getopt(argc, argv, "d:s:h:")) != -1) {
		switch (ch) {
		case 'h':
			return hotplug_run(optarg);//执行hotplug_run(/etc/hotplug-preinit.json):
		case 's':
			ubus_socket = optarg;
			break;
		case 'd':
			debug = atoi(optarg);
			break;
		default:
			return usage(argv[0]);
		}
	}
	uloop_init();
	procd_signal();
	trigger_init();
	if (getpid() != 1)
		procd_connect_ubus();
	else
		procd_state_next();//进程pid=1才执行procd_state_next,这才是开机启动流程中的procd,负责解析/etc/inittab文件并据此依次启动系统。
	uloop_run();

	return 0;
}
示例#5
0
文件: main.c 项目: wangshawn/uhttpd2
static int run_server(void)
{
	uloop_init();
	uh_setup_listeners();
	uh_plugin_post_init();
	uloop_run();

	return 0;
}
int main(int argc, char **argv)
{
  struct dummy_struct ds;
  int c;
  int type = kDNSServiceType_ANY;
  int flags = 0;
  int ifindex = 0;

  memset (&ds, 0, sizeof(ds));
  while ((c = getopt(argc, argv, "f:t:i:")) != -1)
    switch (c)
      {
      case 't':
        type = atoi(optarg);
        break;
      case 'i':
        ifindex = if_nametoindex(optarg);
        break;
      case 'f':
        flags = atoi(optarg);
        break;
      }
  if (argc <= 1)
    {
      fprintf(stderr, "Usage: %s [-f <flags>] [-i <if>] [-t <type>] <name>\n",
              argv[0]);
      exit(1);
    }
  if (DNSServiceQueryRecord(&ds.service, flags, ifindex,
                            argv[1],
                            type,
                            kDNSServiceClass_IN,
                            dummy_callback,
                            NULL) != kDNSServiceErr_NoError)
    {
      fprintf(stderr, "Error initializing DNSServiceQueryRecord\n");
      exit(1);
    }

  if (uloop_init() < 0)
    {
      fprintf(stderr, "Error in uloop_init\n");
      exit(1);
    }
  ds.ufd.fd = DNSServiceRefSockFD(ds.service);
  printf("FD:%d\n", ds.ufd.fd);
  ds.ufd.cb = uloop_fd_callback;
  if (uloop_fd_add(&ds.ufd, ULOOP_READ) < 0)
    {
      fprintf(stderr, "Error in uloop_fd_add\n");
      exit(1);
    }
  printf("Entering event loop\n");
  uloop_run();
  return 0;
}
示例#7
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;
}
示例#8
0
文件: main.c 项目: OnionIoT/ogps
int
main(int argc, char ** argv)
{
	int ch;
	char *device = NULL;
	char *dbglvl = getenv("DBGLVL");
	int ulog_channels = ULOG_KMSG;

	signal(SIGPIPE, SIG_IGN);

	if (dbglvl) {
		debug = atoi(dbglvl);
		unsetenv("DBGLVL");
	}

	while ((ch = getopt(argc, argv, "ad:s:S")) != -1) {
		switch (ch) {
		case 'a':
			adjust_clock = -1;
			break;
		case 's':
			ubus_socket = optarg;
			break;
		case 'd':
			debug = atoi(optarg);
			break;
		case 'S':
			ulog_channels = ULOG_STDIO;
			break;
		default:
			return usage(argv[0]);
		}
	}

	if (argc - optind < 1) {
		fprintf(stderr, "ERROR: missing device parameter\n");
		return usage(argv[0]);
	}

	device = argv[optind];
	ulog_open(ulog_channels, LOG_DAEMON, "ugps");

	uloop_init();
	conn.path = ubus_socket;
	conn.cb = ubus_connect_handler;
	ubus_auto_connect(&conn);

	if (nmea_open(device, &stream, B4800) < 0)
		return -1;

	uloop_run();
	uloop_done();

	return 0;
}
示例#9
0
int
main(int argc, char **argv)
{
	pid_t pid;

	ulog_open(ULOG_KMSG, LOG_DAEMON, "init");

	sigaction(SIGTERM, &sa_shutdown, NULL);
	sigaction(SIGUSR1, &sa_shutdown, NULL);
	sigaction(SIGUSR2, &sa_shutdown, NULL);

	early();
	cmdline();
	watchdog_init(1);

	pid = fork();
	if (!pid) {
		char *kmod[] = { "/sbin/kmodloader", "/etc/modules-boot.d/", NULL };

		if (debug < 3) {
			int fd = open("/dev/null", O_RDWR);

			if (fd > -1) {
				dup2(fd, STDIN_FILENO);
				dup2(fd, STDOUT_FILENO);
				dup2(fd, STDERR_FILENO);
				if (fd > STDERR_FILENO)
					close(fd);
			}
		}
		execvp(kmod[0], kmod);
		ERROR("Failed to start kmodloader\n");
		exit(-1);
	}
	if (pid <= 0) {
		ERROR("Failed to start kmodloader instance\n");
	} else {
		int i;

		for (i = 0; i < 120; i++) {
			if (waitpid(pid, NULL, WNOHANG) > 0)
				break;
			sleep(1);
			watchdog_ping();
		}
	}
	uloop_init();
	preinit();
	uloop_run();

	return 0;
}
示例#10
0
int main(int argc, char *const argv[])
{
	const char *prog = argv[0];
	int c, i;

	openlog("ohybridproxy", LOG_PERROR | LOG_PID, LOG_DAEMON);
	uloop_init();
	while ((c = getopt(argc, argv, "46a:p:h")) != -1) {
		switch (c) {
		case '4':
		case '6':
			/* Ignored for now */
			break;
		case 'a':
			bindaddr = optarg;
			break;

		case 'p':
			bindport = atoi(optarg);
			break;

		default:
                  goto help;
		}
	}

	argc -= optind;
	argv += optind;

	if (argc == 0) {
help:
		show_help(prog);
		return 1;
	}
	for (i = 0 ; i < argc ; i++) {
		char *ifname = argv[i];
		char *domain = strchr(ifname, '=');
		if (!domain) {
			fprintf(stderr, "Invalid domain specification #%d (no =): %s",
				i, ifname);
			return 1;
		}
		*domain++ = 0;
		/* Now we can do stuff with ifname+domain. */
		if (!d2m_add_interface(ifname, domain)) {
			L_ERR("Failed to add interface %s: %s", ifname, strerror(errno));
			return 2;
		}
	}

	return io_run(bindaddr, bindport, MAXIMUM_REQUEST_DURATION_IN_MS);
}
示例#11
0
int main()
{
	openlog("hnetd", LOG_PERROR | LOG_PID, LOG_DAEMON);
	uloop_init();
	to.pending = 0;
	to.cb = _t1;
	uloop_timeout_set(&to, 0);
	end.pending = 0;
	end.cb = _end_to;
	uloop_timeout_set(&end, 1000);
	uloop_run();
	return 0;
}
示例#12
0
文件: main.c 项目: eckyecky/rt-n56u
int main(int argc, char **argv)
{
	static struct qmi_dev dev;
	int ch;

	while ((ch = getopt_long(argc, argv, "d:k:", uqmi_getopt, NULL)) != -1) {
		int cmd_opt = CMD_OPT(ch);

		if (ch < 0 && cmd_opt >= 0 && cmd_opt < __UQMI_COMMAND_LAST) {
			uqmi_add_command(optarg, cmd_opt);
			continue;
		}

		switch(ch) {
		case 'r':
			release_client_id(&dev, optarg);
			break;
		case 'k':
			keep_client_id(&dev, optarg);
			break;
		case 'd':
			device = optarg;
			break;
		default:
			return usage(argv[0]);
		}
	}

	if (!device) {
		fprintf(stderr, "No device given\n");
		return usage(argv[0]);
	}

	uloop_init();
	signal(SIGINT, handle_exit_signal);
	signal(SIGTERM, handle_exit_signal);

	if (qmi_device_open(&dev, device)) {
		fprintf(stderr, "Failed to open device\n");
		uloop_done();
		return 2;
	}

	uqmi_run_commands(&dev);

	qmi_device_close(&dev);

	uloop_done();

	return 0;
}
示例#13
0
文件: server.c 项目: yubo/libubox
int main(void)
{

	jrpc_server_init(&my_server, HOST, PORT);
	jrpc_register_procedure(&my_server, say_hello, "sayHello", NULL);
	jrpc_register_procedure(&my_server, exit_server, "exit", NULL);
	jrpc_register_procedure(&my_server, foo, "foo", NULL);

	uloop_init();
	uloop_fd_add(&my_server.sock, ULOOP_READ);
	uloop_run();

	jrpc_server_destroy(&my_server);

	return 0;
}
示例#14
0
文件: ubus.c 项目: sevennothing/lros
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;
}
示例#15
0
int
main(int argc, char **argv)
{
	int rc = 0;

	rc = config_load();
	if (rc) {
		ERROR("configuration loading failed\n");
		exit(EXIT_FAILURE);
	}

	rc = uloop_init();
	if (rc) {
		ERROR("uloop init failed\n");
		exit(EXIT_FAILURE);
	}

	rc = ssh_netconf_init();
	if (rc) {
		ERROR("ssh init failed\n");
		exit(EXIT_FAILURE);
	}

	rc = ubus_init();
	if (rc) {
		ERROR("ubus init failed\n");
		exit(EXIT_FAILURE);
	}

	LOG("freenetconfd is listening on address:'%s', port:'%s'\n", config.addr, config.port);

	/* main loop */
	uloop_run();

	ssh_netconf_exit();

	uloop_done();

	ubus_exit();
	config_exit();

	return EXIT_SUCCESS;
}
示例#16
0
int main(int argc, char **argv)
{
	// uloop initialize
	uloop_init();

	// setTimeout
	struct uloop_timeout *timeout;
	initialize_timer(timeout, SET_TIMEOUT);
	
	// setInternal
	struct uloop_timeout *interval;
	initialize_timer(interval, SET_INTERVAL);
	
	// loop start
	uloop_run();
	uloop_done();

	return 0;
}
示例#17
0
文件: ubus.c 项目: gaojing2016/backup
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;

}
示例#18
0
文件: ubusd.c 项目: sevennothing/lros
int main(int argc, char **argv)
{
	const char *ubus_socket = UBUS_UNIX_SOCKET;
	int ret = 0;
	int ch;

	signal(SIGPIPE, SIG_IGN);
	signal(SIGHUP, sighup_handler);

	openlog("ubusd", LOG_PID, LOG_DAEMON);
	uloop_init();

	while ((ch = getopt(argc, argv, "s:")) != -1) {
		switch (ch) {
		case 's':
			ubus_socket = optarg;
			break;
		default:
			return usage(argv[0]);
		}
	}

	unlink(ubus_socket);
	umask(0111);
	server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
	if (server_fd.fd < 0) {
		perror("usock");
		ret = -1;
		goto out;
	}
	uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
	ubusd_acl_load();

	uloop_run();
	unlink(ubus_socket);

out:
	uloop_done();
	return ret;
}
示例#19
0
文件: unitd.c 项目: orb-os/unitd
int main()
{
	static char unitd[] = "unitd";

	if (getpid() != 1) {
		fprintf(stderr, "error: must run as PID 1\n");
		return 1;
	}

	program_invocation_short_name = unitd;
	prctl(PR_SET_NAME, unitd);

	ulog_open(ULOG_KMSG, LOG_DAEMON, "unitd");

	setsid();
	uloop_init();
	unitd_signal();
	unitd_state_next();
	uloop_run();
	uloop_done();

	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;
}
示例#21
0
int main(int argc, char **argv)
{
	const char *ubus_socket = UBUS_UNIX_SOCKET;//定义在cmake中,UBUS_UNIX_SOCKET="/var/run/ubus.sock"
	int ret = 0;
	int ch;

	signal(SIGPIPE, SIG_IGN);//入参:信号类型;处理函数handler。

	uloop_init();

	while ((ch = getopt(argc, argv, "s:")) != -1) {
		switch (ch) {
		case 's':
			ubus_socket = optarg;
			break;
		default:
			return usage(argv[0]);
		}
	}

	unlink(ubus_socket);//unlink,删除/var/run/ubus.sock文件
	umask(0177);//umask,设置用户创建新文件时文件的默认权限。
	server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
	if (server_fd.fd < 0) {//usock,首参为套接字类型,ubus_socket可为"127.0.0.1"或var/run/ubus.sock,最后一个为服务端口号"22"或NULL
		perror("usock");
		ret = -1;
		goto out;
	}
	uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);

	uloop_run();//调用uloop_run(),ubusd进程不会再退出--->ubusd通过select方法监听套接字/var/run/ubus.sock,接收ubus发出的服务查询/请求,并将服务请求转发给netifd等提供服务的进程。
	unlink(ubus_socket);

out:
	uloop_done();
	return ret;
}
示例#22
0
文件: main.c 项目: gjz22cn/openwrt
int main(int argc, char *argv[])
{
    config_init(argc, argv);

    if (glue_init() != 0)
    {
        iotc_error("glue init failed!");
        return -1;
    }

    if (0 != connection_init())
    {
        iotc_error("socket_init failed!");
        return -1;
    }

    uloop_init();
    uloop_fd_add(&iotc_monitor_uloop_fd, ULOOP_READ);
    uloop_timeout_set(&g_sendDevOnlineTm, 2000);
    uloop_run();
    uloop_done();

    return 0;
}
示例#23
0
static void dncp_io_basic_2()
{
  hncp_s h1, h2;
  dncp_s d1, d2;
  bool r;
  struct in6_addr a;
  char *msg = "foo";
  char *ifname = LOOPBACK_NAME;

  (void)uloop_init();
  memset(&h1, 0, sizeof(h1));
  memset(&h2, 0, sizeof(h2));
  memset(&d1, 0, sizeof(d1));
  memset(&d2, 0, sizeof(d2));
  h1.udp_port = 62000;
  h2.udp_port = 62001;
  h1.dncp = &d1;
  h2.dncp = &d2;
  d1.ext = &h1.ext;
  d2.ext = &h2.ext;
  r = hncp_io_init(&h1);
  sput_fail_unless(r, "dncp_io_init h1");
  r = hncp_io_init(&h2);
  sput_fail_unless(r, "dncp_io_init h2");

  /* Send a packet to ourselves */
  (void)inet_pton(AF_INET6, "::1", &a);
  struct sockaddr_in6 src = {
    .sin6_family = AF_INET6,
    .sin6_port = htons(h1.udp_port),
    .sin6_addr = a
#ifdef __APPLE__
    , .sin6_len = sizeof(struct sockaddr_in6)
#endif /* __APPLE__ */
  };
  struct sockaddr_in6 dst = {
    .sin6_family = AF_INET6,
    .sin6_port = htons(h2.udp_port),
    .sin6_addr = a
#ifdef __APPLE__
    , .sin6_len = sizeof(struct sockaddr_in6)
#endif /* __APPLE__ */
  };
  smock_push_int("dncp_poll_io_recvfrom", 3);
  smock_push_int("dncp_poll_io_recvfrom_src", &src);
  smock_push_int("dncp_poll_io_recvfrom_dst", &dst);
  smock_push_int("dncp_poll_io_recvfrom_buf", msg);
  smock_push_int("dncp_poll_io_recvfrom_ifname", ifname);
  h1.ext.cb.send(&h1.ext, dncp_find_ep_by_name(h1.dncp, "lo"),
                 NULL, &dst, msg, strlen(msg));
  pending_packets++;

  uloop_run();

  hncp_io_uninit(&h1);
  hncp_io_uninit(&h2);
}

int main(int argc, char **argv)
{
  setbuf(stdout, NULL); /* so that it's in sync with stderr when redirected */
  openlog("test_hncp_io", LOG_CONS | LOG_PERROR, LOG_DAEMON);
  sput_start_testing();
  sput_enter_suite("hncp_io"); /* optional */
  argc -= 1;
  argv += 1;

  sput_maybe_run_test(dncp_io_basic_2, do {} while(0));
  sput_leave_suite(); /* optional */
  sput_finish_testing();
  return sput_get_return_value();
}
示例#24
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;
}
示例#25
0
int main (int argc, char **argv)
{
	int c;
	int start_event = 0;
	bool foreground = false;

	while (1) {
		c = getopt_long(argc, argv, "fhbgv", long_opts, NULL);
		if (c == EOF)
			break;
		switch (c) {
			case 'b':
				start_event |= START_BOOT;
				break;
			case 'f':
				foreground = true;
				break;
			case 'g':
				start_event |= START_GET_RPC_METHOD;
				break;
			case 'h':
				print_help();
				exit(EXIT_SUCCESS);
			case 'v':
				print_version();
				exit(EXIT_SUCCESS);
			default:
				print_help();
				exit(EXIT_FAILURE);
		}
	}

	log_message(NAME, L_DEBUG, "daemon started\n");

	setlocale(LC_CTYPE, "");
	umask(0037);

	if (getuid() != 0) {
		D("run %s as root\n", NAME);
		exit(EXIT_FAILURE);
	}

	/* run early cwmp initialization */
	cwmp = calloc(1, sizeof(struct cwmp_internal));
	if (!cwmp) return -1;

	INIT_LIST_HEAD(&cwmp->events);
	INIT_LIST_HEAD(&cwmp->notifications);
	INIT_LIST_HEAD(&cwmp->downloads);
	INIT_LIST_HEAD(&cwmp->scheduled_informs);
	uloop_init();
	backup_init();
	config_load();
	cwmp_init_deviceid();
	if (start_event & START_BOOT) {
		cwmp_add_event(EVENT_BOOT, NULL, 0, EVENT_BACKUP);
		cwmp_add_inform_timer();
	}
	if (start_event & START_GET_RPC_METHOD) {
		cwmp->get_rpc_methods = true;
		cwmp_add_event(EVENT_PERIODIC, NULL, 0, EVENT_BACKUP);
		cwmp_add_inform_timer();
	}

	if (netlink_init()) {
		D("netlink initialization failed\n");
		exit(EXIT_FAILURE);
	}

	if (ubus_init()) D("ubus initialization failed\n");

	http_server_init();

	pid_t pid, sid;
	if (!foreground) {
		pid = fork();
		if (pid < 0)
			exit(EXIT_FAILURE);
		if (pid > 0)
			exit(EXIT_SUCCESS);

		sid = setsid();
		if (sid < 0) {
			D("setsid() returned error\n");
			exit(EXIT_FAILURE);
		}

		char *directory = "/";

		if ((chdir(directory)) < 0) {
			D("chdir() returned error\n");
			exit(EXIT_FAILURE);
		}
	}

	log_message(NAME, L_DEBUG, "entering main loop\n");
	uloop_run();

	ubus_exit();
	uloop_done();

	http_client_exit();
	xml_exit();
	config_exit();
	cwmp_free_deviceid();
	free(cwmp);

	closelog();

	log_message(NAME, L_DEBUG, "exiting\n");
	return 0;
}
示例#26
0
文件: cshark.c 项目: bngoalie/cshark
int main(int argc, char *argv[])
{
	int rc, c;
	int keep = 0;
	char *pid_filename = NULL;

	/* zero out main struct */
	memset(&cshark, 0, sizeof(cshark));

	/* preconfigure defaults */
	cshark.interface = "any";
	cshark.filename = NULL;
	cshark.snaplen = 65535;
	cshark.filter = NULL;
	cshark.packets = 0;
	cshark.limit_packets = 0;
	cshark.caplen = 0;
	cshark.limit_caplen = 0;

	openlog(PROJECT_NAME, LOG_PERROR | LOG_PID, LOG_DAEMON);

	while ((c = getopt(argc, argv, "i:w:s:T:P:S:p:kvh")) != -1) {
		switch (c) {
			case 'i':
				cshark.interface = optarg;
				break;

			case 'w':
				cshark.filename = strdup(optarg);
				if (!cshark.filename) {
					ERROR("not enough memory\n");
					rc = EXIT_FAILURE;
					goto exit;
				}
				break;

			case 's':
				cshark.snaplen = atoi(optarg);
				if (!cshark.snaplen) cshark.snaplen = 65535;
				break;

			case 'T':
			{
				struct uloop_timeout dump_timeout = {
					.cb = dump_timeout_callback
				};

				int dump_timeout_s = atoi(optarg);
				if (dump_timeout_s > 0)
					uloop_timeout_set(&dump_timeout, dump_timeout_s * 1000);

				break;
			}

			case 'P':
				cshark.limit_packets = atoi(optarg);
				break;

			case 'S':
				cshark.limit_caplen = atoi(optarg);
				break;

			case 'p':
			{
				pid_t pid = getpid();

				pid_filename = optarg;
				FILE *f = fopen(pid_filename, "w");
				if (!f) {
					fprintf(stderr, "Failed writing PID to '%s'\n", optarg);
					return EXIT_FAILURE;
				}

				fprintf(f, "%d\n", pid);

				fclose(f);
				sync();

				break;
			}

			case 'k':
				keep = 1;
				break;

			case 'v':
				printf("%s version %s\n", PROJECT_NAME, PROJECT_VERSION);
				return EXIT_FAILURE;

			case 'h':
				show_help();
				return EXIT_FAILURE;

			default:
				break;
		}
	}

	while (optind < argc) {
		asprintf(&cshark.filter, "%s %s", \
			cshark.filter ? cshark.filter : "", argv[optind]);
		optind++;
	}

	rc = config_load();
	if (rc) {
		ERROR("unable to load configuration\n");
		rc = EXIT_FAILURE;
		goto exit;
	}

	if (!cshark.filename) {
		int len = 0;
		len = snprintf(cshark.filename, 0, "%s/cshark.pcap-XXXXXX", config.dir);

		cshark.filename = calloc(len + 1, sizeof(char));
		if (!cshark.filename) {
			ERROR("not enough memory\n");
			rc = EXIT_FAILURE;
			goto exit;
		}
		snprintf(cshark.filename, len + 1, "%s/cshark.pcap-XXXXXX", config.dir);

		int fd = mkstemp(cshark.filename);
		if (fd == -1) {
			ERROR("unable to create dump file\n");
			rc = EXIT_FAILURE;
			goto exit;
		}
	}

	uloop_init();

	rc = cshark_pcap_init(&cshark);
	if (rc) {
		rc = EXIT_FAILURE;
		goto exit;
	}

	printf("capturing traffic to file: '%s' ...\n", cshark.filename);
	uloop_run();

	cshark_pcap_done(&cshark);
	printf("\n%lu packets captured\n", (long unsigned int) cshark.packets);

	rc = cshark_uclient_init(&cshark);
	if (rc) {
		rc = EXIT_FAILURE;
		goto exit;
	}

	printf("uploading capture ...\n");
	uloop_run();

	uloop_done();

	rc = EXIT_SUCCESS;

exit:
	cshark_pcap_done(&cshark);
	cshark_uclient_done(&cshark);
	if (!keep) remove(cshark.filename);
	free(cshark.filename);
	if (pid_filename) remove(pid_filename);

	return rc;
}
示例#27
0
文件: trace.c 项目: asac/procd
int main(int argc, char **argv, char **envp)
{
	char *json = NULL;
	int status, ch, policy = EPERM;
	pid_t child;

	while ((ch = getopt(argc, argv, "f:p:")) != -1) {
		switch (ch) {
		case 'f':
			json = optarg;
			break;
		case 'p':
			policy = atoi(optarg);
			break;
		}
	}

	argc -= optind;
	argv += optind;

	if (!argc)
		return -1;

	if (getenv("TRACE_DEBUG"))
		debug = 1;
	unsetenv("TRACE_DEBUG");

	child = fork();

	if (child == 0) {
		char **_argv = calloc(argc + 1, sizeof(char *));
		char **_envp;
		char preload[] = "LD_PRELOAD=/lib/libpreload-trace.so";
		int envc = 1;
		int ret;

		memcpy(_argv, argv, argc * sizeof(char *));

		while (envp[envc++])
			;

		_envp = calloc(envc, sizeof(char *));
		memcpy(&_envp[1], _envp, envc * sizeof(char *));
		*envp = preload;

		ret = execve(_argv[0], _argv, envp);
		ERROR("failed to exec %s: %s\n", _argv[0], strerror(errno));
		return ret;
	}

	if (child < 0)
		return -1;

	syscall_max = ARRAY_SIZE(syscall_names);
	syscall_count = calloc(syscall_max, sizeof(int));
	waitpid(child, &status, 0);
	if (!WIFSTOPPED(status)) {
		ERROR("failed to start %s\n", *argv);
		return -1;
	}

	ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACESYSGOOD);
	ptrace(PTRACE_SYSCALL, child, 0, 0);

	uloop_init();
	tracer.pid = child;
	tracer.cb = tracer_cb;
	uloop_process_add(&tracer);
	uloop_run();
	uloop_done();

	if (!json)
		if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
			ERROR("failed to allocate output path: %s\n", strerror(errno));

	print_syscalls(policy, json);

	return 0;
}
示例#28
0
int main() {
    uloop_init();
    uloop_timeout_set(&server_time, 2000);
    uloop_run();
    return 0;
}
示例#29
0
文件: main.c 项目: Arielis35/flxd
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;
}
示例#30
0
void app_init(struct app *self){
	uloop_init(); 
}