⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kobject.c

📁 Lib files of linux kernel
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * kobject.c - library routines for handling generic kernel objects * * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org> * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com> * Copyright (c) 2006-2007 Novell Inc. * * This file is released under the GPLv2. * * * Please see the file Documentation/kobject.txt for critical information * about using the kobject interface. */#include <linux/kobject.h>#include <linux/string.h>#include <linux/module.h>#include <linux/stat.h>#include <linux/slab.h>/* * populate_dir - populate directory with attributes. * @kobj: object we're working on. * * Most subsystems have a set of default attributes that are associated * with an object that registers with them.  This is a helper called during * object registration that loops through the default attributes of the * subsystem and creates attributes files for them in sysfs. */static int populate_dir(struct kobject *kobj){	struct kobj_type *t = get_ktype(kobj);	struct attribute *attr;	int error = 0;	int i;	if (t && t->default_attrs) {		for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {			error = sysfs_create_file(kobj, attr);			if (error)				break;		}	}	return error;}static int create_dir(struct kobject *kobj){	int error = 0;	if (kobject_name(kobj)) {		error = sysfs_create_dir(kobj);		if (!error) {			error = populate_dir(kobj);			if (error)				sysfs_remove_dir(kobj);		}	}	return error;}static int get_kobj_path_length(struct kobject *kobj){	int length = 1;	struct kobject *parent = kobj;	/* walk up the ancestors until we hit the one pointing to the	 * root.	 * Add 1 to strlen for leading '/' of each level.	 */	do {		if (kobject_name(parent) == NULL)			return 0;		length += strlen(kobject_name(parent)) + 1;		parent = parent->parent;	} while (parent);	return length;}static void fill_kobj_path(struct kobject *kobj, char *path, int length){	struct kobject *parent;	--length;	for (parent = kobj; parent; parent = parent->parent) {		int cur = strlen(kobject_name(parent));		/* back up enough to print this name with '/' */		length -= cur;		strncpy(path + length, kobject_name(parent), cur);		*(path + --length) = '/';	}	pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),		 kobj, __func__, path);}/** * kobject_get_path - generate and return the path associated with a given kobj and kset pair. * * @kobj:	kobject in question, with which to build the path * @gfp_mask:	the allocation type used to allocate the path * * The result must be freed by the caller with kfree(). */char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask){	char *path;	int len;	len = get_kobj_path_length(kobj);	if (len == 0)		return NULL;	path = kzalloc(len, gfp_mask);	if (!path)		return NULL;	fill_kobj_path(kobj, path, len);	return path;}EXPORT_SYMBOL_GPL(kobject_get_path);/* add the kobject to its kset's list */static void kobj_kset_join(struct kobject *kobj){	if (!kobj->kset)		return;	kset_get(kobj->kset);	spin_lock(&kobj->kset->list_lock);	list_add_tail(&kobj->entry, &kobj->kset->list);	spin_unlock(&kobj->kset->list_lock);}/* remove the kobject from its kset's list */static void kobj_kset_leave(struct kobject *kobj){	if (!kobj->kset)		return;	spin_lock(&kobj->kset->list_lock);	list_del_init(&kobj->entry);	spin_unlock(&kobj->kset->list_lock);	kset_put(kobj->kset);}static void kobject_init_internal(struct kobject *kobj){	if (!kobj)		return;	kref_init(&kobj->kref);	INIT_LIST_HEAD(&kobj->entry);	kobj->state_in_sysfs = 0;	kobj->state_add_uevent_sent = 0;	kobj->state_remove_uevent_sent = 0;	kobj->state_initialized = 1;}static int kobject_add_internal(struct kobject *kobj){	int error = 0;	struct kobject *parent;	if (!kobj)		return -ENOENT;	if (!kobj->name || !kobj->name[0]) {		WARN(1, "kobject: (%p): attempted to be registered with empty "			 "name!\n", kobj);		return -EINVAL;	}	parent = kobject_get(kobj->parent);	/* join kset if set, use it as parent if we do not already have one */	if (kobj->kset) {		if (!parent)			parent = kobject_get(&kobj->kset->kobj);		kobj_kset_join(kobj);		kobj->parent = parent;	}	pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",		 kobject_name(kobj), kobj, __func__,		 parent ? kobject_name(parent) : "<NULL>",		 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");	error = create_dir(kobj);	if (error) {		kobj_kset_leave(kobj);		kobject_put(parent);		kobj->parent = NULL;		/* be noisy on error issues */		if (error == -EEXIST)			printk(KERN_ERR "%s failed for %s with "			       "-EEXIST, don't try to register things with "			       "the same name in the same directory.\n",			       __func__, kobject_name(kobj));		else			printk(KERN_ERR "%s failed for %s (%d)\n",			       __func__, kobject_name(kobj), error);		dump_stack();	} else		kobj->state_in_sysfs = 1;	return error;}/** * kobject_set_name_vargs - Set the name of an kobject * @kobj: struct kobject to set the name of * @fmt: format string used to build the name * @vargs: vargs to format the string. */static int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,				  va_list vargs){	const char *old_name = kobj->name;	char *s;	kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);	if (!kobj->name)		return -ENOMEM;	/* ewww... some of these buggers have '/' in the name ... */	while ((s = strchr(kobj->name, '/')))		s[0] = '!';	kfree(old_name);	return 0;}/** * kobject_set_name - Set the name of a kobject * @kobj: struct kobject to set the name of * @fmt: format string used to build the name * * This sets the name of the kobject.  If you have already added the * kobject to the system, you must call kobject_rename() in order to * change the name of the kobject. */int kobject_set_name(struct kobject *kobj, const char *fmt, ...){	va_list vargs;	int retval;	va_start(vargs, fmt);	retval = kobject_set_name_vargs(kobj, fmt, vargs);	va_end(vargs);	return retval;}EXPORT_SYMBOL(kobject_set_name);/** * kobject_init - initialize a kobject structure * @kobj: pointer to the kobject to initialize * @ktype: pointer to the ktype for this kobject. * * This function will properly initialize a kobject such that it can then * be passed to the kobject_add() call. * * After this function is called, the kobject MUST be cleaned up by a call * to kobject_put(), not by a call to kfree directly to ensure that all of * the memory is cleaned up properly. */void kobject_init(struct kobject *kobj, struct kobj_type *ktype){	char *err_str;	if (!kobj) {		err_str = "invalid kobject pointer!";		goto error;	}	if (!ktype) {		err_str = "must have a ktype to be initialized properly!\n";		goto error;	}	if (kobj->state_initialized) {		/* do not error out as sometimes we can recover */		printk(KERN_ERR "kobject (%p): tried to init an initialized "		       "object, something is seriously wrong.\n", kobj);		dump_stack();	}	kobject_init_internal(kobj);	kobj->ktype = ktype;	return;error:	printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);	dump_stack();}EXPORT_SYMBOL(kobject_init);static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,			    const char *fmt, va_list vargs){	int retval;	retval = kobject_set_name_vargs(kobj, fmt, vargs);	if (retval) {		printk(KERN_ERR "kobject: can not set name properly!\n");		return retval;	}	kobj->parent = parent;	return kobject_add_internal(kobj);}/** * kobject_add - the main kobject add function * @kobj: the kobject to add * @parent: pointer to the parent of the kobject. * @fmt: format to name the kobject with. * * The kobject name is set and added to the kobject hierarchy in this * function. * * If @parent is set, then the parent of the @kobj will be set to it. * If @parent is NULL, then the parent of the @kobj will be set to the * kobject associted with the kset assigned to this kobject.  If no kset * is assigned to the kobject, then the kobject will be located in the * root of the sysfs tree. * * If this function returns an error, kobject_put() must be called to * properly clean up the memory associated with the object. * Under no instance should the kobject that is passed to this function * be directly freed with a call to kfree(), that can leak memory. * * Note, no "add" uevent will be created with this call, the caller should set * up all of the necessary sysfs files for the object and then call * kobject_uevent() with the UEVENT_ADD parameter to ensure that * userspace is properly notified of this kobject's creation. */int kobject_add(struct kobject *kobj, struct kobject *parent,		const char *fmt, ...){	va_list args;	int retval;	if (!kobj)		return -EINVAL;	if (!kobj->state_initialized) {		printk(KERN_ERR "kobject '%s' (%p): tried to add an "		       "uninitialized object, something is seriously wrong.\n",		       kobject_name(kobj), kobj);		dump_stack();		return -EINVAL;	}	va_start(args, fmt);	retval = kobject_add_varg(kobj, parent, fmt, args);	va_end(args);	return retval;}EXPORT_SYMBOL(kobject_add);/** * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy * @kobj: pointer to the kobject to initialize * @ktype: pointer to the ktype for this kobject. * @parent: pointer to the parent of this kobject. * @fmt: the name of the kobject. * * This function combines the call to kobject_init() and * kobject_add().  The same type of error handling after a call to * kobject_add() and kobject lifetime rules are the same here. */int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,			 struct kobject *parent, const char *fmt, ...){	va_list args;	int retval;	kobject_init(kobj, ktype);	va_start(args, fmt);	retval = kobject_add_varg(kobj, parent, fmt, args);	va_end(args);	return retval;}EXPORT_SYMBOL_GPL(kobject_init_and_add);/** * kobject_rename - change the name of an object * @kobj: object in question. * @new_name: object's new name */int kobject_rename(struct kobject *kobj, const char *new_name){	int error = 0;	const char *devpath = NULL;	char *devpath_string = NULL;	char *envp[2];	kobj = kobject_get(kobj);	if (!kobj)		return -EINVAL;	if (!kobj->parent)		return -EINVAL;	/* see if this name is already in use */	if (kobj->kset) {		struct kobject *temp_kobj;		temp_kobj = kset_find_obj(kobj->kset, new_name);		if (temp_kobj) {			printk(KERN_WARNING "kobject '%s' cannot be renamed "			       "to '%s' as '%s' is already in existence.\n",			       kobject_name(kobj), new_name, new_name);			kobject_put(temp_kobj);			return -EINVAL;		}	}	devpath = kobject_get_path(kobj, GFP_KERNEL);	if (!devpath) {		error = -ENOMEM;		goto out;	}	devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);	if (!devpath_string) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -