Beispiel #1
0
/*
 * Read pid from pidfile
 */
pid_t
ni_pidfile_read(const char *pidfile)
{
	char buffer[128];
	FILE *fp;
	pid_t pid = 0;

	if (!ni_file_exists(pidfile))
		return 0;

	if (!(fp = fopen(pidfile, "r"))) {
		/* pidfile exists but we can't read it. bad. */
		ni_error("cannot open pidfile %s for reading: %m", pidfile);
		return -1;
	}

	if (fgets(buffer, sizeof(buffer), fp) != NULL) {
		char *s;

		pid = strtoul(buffer, &s, 0);
		if (*s && !isspace(*s)) {
			ni_error("cannot parse pidfile %s", pidfile);
			pid = -1;
		}
	}

	fclose(fp);
	return pid;
}
Beispiel #2
0
/*
 * Remove a lease file
 */
static void
__ni_addrconf_lease_file_remove(const char *dir, const char *ifname,
				int type, int family)
{
	char *filename = NULL;

	if (!__ni_addrconf_lease_file_path(&filename, dir, ifname, type, family))
		return;

	if (ni_file_exists(filename) && unlink(filename) == 0)
		ni_debug_dhcp("removed %s", filename);
	ni_string_free(&filename);
}
Beispiel #3
0
int main(void)
{
	ni_ibft_nic_array_t nics = NI_IBFT_NIC_ARRAY_INIT;
	const char *root = NULL;
	unsigned int i;

	/* Use local directory */
	if (!ni_file_exists("/sys/firmware/ibft"))
		root = "./ibft";

	if(ni_sysfs_ibft_scan_nics(&nics, root) <= 0)
		return 0;

	for(i = 0; i < nics.count; ++i) {
		ni_ibft_nic_t *nic = nics.data[i];

		printf("node: %s\n", nic->node);
		printf("  devpath  : %s\n", nic->devpath);
		printf("  ifname   :  %s\n", nic->ifname);
		printf("  ifindex  : %u\n", nic->ifindex);

		printf("  index    : %u\n",  nic->index);
		printf("  flags    : %u\n",  nic->flags);
		printf("  origin   : %u\n", nic->origin);
		printf("  vlan     : %u\n",   nic->vlan);

		printf("  hwaddr   : %s\n", ni_link_address_print(&nic->hwaddr));
		printf("  ipaddr   : %s/%u\n", ni_sockaddr_print(&nic->ipaddr),
						nic->prefix_len);

		printf("  dhcp     : %s\n", ni_sockaddr_print(&nic->dhcp));
		printf("  gateway  : %s\n", ni_sockaddr_print(&nic->gateway));
		printf("  pri_dns  : %s\n", ni_sockaddr_print(&nic->primary_dns));
		printf("  sec_dns  : %s\n", ni_sockaddr_print(&nic->secondary_dns));

		printf("  hostname : %s\n", nic->hostname);

		printf("\n");
	}

	ni_ibft_nic_array_destroy(&nics);

	return 0;
}
Beispiel #4
0
/*
 * background the current process
 */
int
ni_daemonize(const char *pidfile, unsigned int permissions)
{
	pid_t pid;

	if (pidfile) {
		/* check if service is already running */

		pid = ni_pidfile_check(pidfile);
		if (pid < 0)
			return -1;

		if (pid > 0) {
			ni_error("cannot create pidfile %s: service already running", pidfile);
			return -1;
		}

		if (ni_file_exists(pidfile)) {
			if (unlink(pidfile) < 0) {
				ni_error("cannot remove stale pidfile %s: %m", pidfile);
				return -1;
			}
			ni_warn("removing stale pidfile %s", pidfile);
		}
	}

	if (pidfile && ni_pidfile_write(pidfile, permissions, getpid()) < 0)
		return -1;

	/* fork, chdir to root and close fds */
	if (daemon(0, 0) < 0)
		ni_fatal("unable to background process! daemon() failed: %m");

	if (pidfile)
		__ni_pidfile_write(pidfile, permissions, getpid(), 0);

	return 0;
}
Beispiel #5
0
int
ni_init_ex(const char *appname, ni_init_appdata_callback_t *cb, void *appdata)
{
	int explicit_config = 1;

	if (ni_global.initialized) {
		ni_error("ni_init called twice");
		return -1;
	}

	if (__ni_init_gcrypt() < 0)
		return -1;

	if (ni_global.config_path == NULL) {
		if (appname == NULL) {
			/* Backward compatible - for now.
			 * The server will load config.xml
			 */
			appname = "config";
		}

		if (asprintf(&ni_global.config_path, "%s/%s.xml",
					ni_get_global_config_dir(), appname) < 0) {
			ni_global.config_path = NULL;
			return -1;
		}

		/* If the application-specific config file does not exist, fall
		 * back to common.xml */
		if (!ni_file_exists(ni_global.config_path)) {
			ni_string_free(&ni_global.config_path);
			if (asprintf(&ni_global.config_path, "%s/common.xml",
						ni_get_global_config_dir()) < 0) {
				ni_global.config_path = NULL;
				return -1;
			}
		}

		explicit_config = 0;
	}

	if (ni_file_exists(ni_global.config_path)) {
		ni_global.config = ni_config_parse(ni_global.config_path, cb, appdata);
		if (!ni_global.config) {
			ni_error("Unable to parse netinfo configuration file");
			return -1;
		}
	} else {
		if (explicit_config) {
			ni_error("Configuration file %s does not exist",
					ni_global.config_path);
			return -1;
		}
		/* Create empty default configuration */
		ni_global.config = ni_config_new();
	}

	/* Our socket code relies on us ignoring this */
	signal(SIGPIPE, SIG_IGN);

	ni_global.initialized = 1;
	return 0;
}