/**
 * init_node
 *
 * @param node
 * @returns 0 on success, !0 otherwise
 */
static int
init_node(struct dr_node *node)
{
	struct dirent **de_list, *de;
	char *newpath;
	int count;
	int rc, i;

	if (node->is_owned)
		find_ofdt_dname(node, node->ofdt_path);

	count = scandir(node->ofdt_path, &de_list, 0, alphasort);
	for (i = 0; i < count; i++) {
		de = de_list[i];
		if ((de->d_type != DT_DIR) || is_dot_dir(de->d_name))
			continue;

		newpath = zalloc(strlen(node->ofdt_path) +
				 strlen(de->d_name) + 2);
		if (newpath == NULL) {
			say(ERROR, "Could not allocate path for node at "
			    "%s/%s\n", node->ofdt_path, de->d_name);
			return 1;
		}

		sprintf(newpath, "%s/%s", node->ofdt_path, de->d_name);
		rc = examine_child(node, newpath);
		if (rc)
			return rc;
	}

	return 0;
}
Beispiel #2
0
/**
 * init_node
 *
 * @param node
 * @returns 0 on success, !0 otherwise
 */
static int
init_node(struct dr_node *node)
{
	DIR *d;
	struct dirent *de;
	char child_path[DR_PATH_MAX];
	uint32_t my_drc_index;
	int rc;

	if (node->is_owned)
		find_ofdt_dname(node, node->ofdt_path);

	d = opendir(node->ofdt_path);
	if (!d)
		return -1;

	rc = 0;
	while ((de = readdir(d)) != NULL) {
		if ((de->d_type != DT_DIR) || is_dot_dir(de->d_name))
			continue;

		sprintf(child_path, "%s/%s", node->ofdt_path, de->d_name);

		if (get_my_drc_index(child_path, &my_drc_index))
			continue;

		if (node->dev_type == PCI_HP_DEV) {
			if (node->drc_index == my_drc_index) {
				/* Add hotplug children */
				add_child_node(node, child_path);
			}
		} else {
			if (!node->is_owned) {
				if (node->drc_index == my_drc_index) {
					/* Update node path */
					snprintf(node->ofdt_path, DR_PATH_MAX,
						 "%s", child_path);
					node->is_owned = 1;

					/* Populate w/ children */
					rc = init_node(node);
					if (rc)
						break;
				}
			} else {
				/* Add all DR-capable children */
				add_child_node(node, child_path);
			}
		}
	}

	closedir(d);
	return rc;
}
/**
 * find_ofdt_dname
 *
 * Find "name" of the device_node that is a child
 * of given node and its "ibm,loc-code" is the same
 * as node->name.
 *
 * @param node
 * @param path
 * @param ofdt_dname
 */
static int
find_ofdt_dname(struct dr_node *node, char *path)
{
	DIR *d = NULL;
	struct dirent *de;
	struct stat sb;
	char new_path[DR_PATH_MAX];
	char loc_code[DR_BUF_SZ];
	char *q;
	int found = 0;
	int rc;

	rc = get_property(path, "ibm,loc-code", &loc_code, DR_BUF_SZ);
	if ((rc == 0) && (strstr(loc_code, node->drc_name))) {
		rc = get_property(path, "name", node->ofdt_dname,
				  sizeof(node->ofdt_dname));
		if (rc == 0)
			return 1;
	}

	/* First look for a ofdt node that has ibm,loc-code property
	 * with a value that matches node->name.
	 *
	 * Set node->node->ofdt_dname to "name" of the ofdt node.
	 */
	d = opendir(path);
	if (d == NULL) {
		say(ERROR, "Could not open dir %s\n%s\n", path,
		    strerror(errno));
		return 0;
	}

	strcpy(new_path, path);
	q = new_path + strlen(new_path);
	*q++ = '/';

	while(((de = readdir(d)) != NULL) && (! found)) {
		/* skip all dot files */
		if (de->d_name[0] == '.')
			continue;

		strcpy(q, de->d_name);
		if (lstat(new_path, &sb) < 0)
			continue;

		if (S_ISLNK(sb.st_mode))
			continue;

		if (S_ISDIR(sb.st_mode)) {
			rc = get_property(path, "ibm,loc-code", &loc_code,
					  DR_BUF_SZ);
			if ((rc != 0) || (! strstr(loc_code, node->drc_name))) {
				found = find_ofdt_dname(node, new_path);
				continue;
			}

			rc = get_property(path, "name", node->ofdt_dname,
					  sizeof(node->ofdt_dname));
			if (rc == 0) {
				found = 1;
				break;
			}
		}
	}

	if (d != NULL)
		closedir(d);

	return found;
}