3 * Copyright © 2010 - 2013 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
18 #include "procobjecttree.h"
20 #define MYDRVNAME "procobjecttree"
24 /** This is context info that we stash in each /proc file entry, which we
25 * need in order to call the callback function that supplies the /proc read
29 void (*show_property)(struct seq_file *, void *, int);
30 MYPROCOBJECT *procObject;
33 } PROCDIRENTRYCONTEXT;
35 /** This describes the attributes of a tree rooted at
36 * <procDirRoot>/<name[0]>/<name[1]>/...
37 * Properties for each object of this type will be located under
38 * <procDirRoot>/<name[0]>/<name[1]>/.../<objectName>/<propertyName>.
40 struct MYPROCTYPE_Tag {
41 const char **name; /**< node names for this type, ending with NULL */
42 int nNames; /**< num of node names in <name> */
44 /** root dir for this type tree in /proc */
45 struct proc_dir_entry *procDirRoot;
47 struct proc_dir_entry **procDirs; /**< for each node in <name> */
49 /** bottom dir where objects will be rooted; i.e., this is
50 * <procDirRoot>/<name[0]>/<name[1]>/.../, which is the same as the
51 * last entry in the <procDirs> array. */
52 struct proc_dir_entry *procDir;
54 /** name for each property that objects of this type can have */
55 const char **propertyNames;
57 int nProperties; /**< num of names in <propertyNames> */
59 /** Call this, passing MYPROCOBJECT.context and the property index
60 * whenever someone reads the proc entry */
61 void (*show_property)(struct seq_file *, void *, int);
66 struct MYPROCOBJECT_Tag {
69 /** This is the name of the dir node in /proc under which the
70 * properties of this object will appear as files. */
73 int namesize; /**< number of bytes allocated for name */
74 void *context; /**< passed to MYPROCTYPE.show_property */
76 /** <type.procDirRoot>/<type.name[0]>/<type.name[1]>/.../<name> */
77 struct proc_dir_entry *procDir;
79 /** a proc dir entry for each of the properties of the object;
80 * properties are identified in MYPROCTYPE.propertyNames, so each of
81 * the <procDirProperties> describes a single file like
82 * <type.procDirRoot>/<type.name[0]>/<type.name[1]>/...
83 * /<name>/<propertyName>
85 struct proc_dir_entry **procDirProperties;
87 /** this is a holding area for the context information that is needed
88 * to run the /proc callback function */
89 PROCDIRENTRYCONTEXT *procDirPropertyContexts;
94 static struct proc_dir_entry *
95 createProcDir(const char *name, struct proc_dir_entry *parent)
97 struct proc_dir_entry *p = proc_mkdir_mode(name, S_IFDIR, parent);
99 ERRDRV("failed to create /proc directory %s", name);
103 static struct proc_dir_entry *
104 createProcFile(const char *name, struct proc_dir_entry *parent,
105 const struct file_operations *fops, void *data)
107 struct proc_dir_entry *p = proc_create_data(name, 0, parent,
110 ERRDRV("failed to create /proc file %s", name);
114 static int seq_show(struct seq_file *seq, void *offset);
115 static int proc_open(struct inode *inode, struct file *file)
117 return single_open(file, seq_show, PDE_DATA(inode));
120 static const struct file_operations proc_fops = {
124 .release = single_release,
129 MYPROCTYPE *visor_proc_CreateType(struct proc_dir_entry *procDirRoot,
131 const char **propertyNames,
132 void (*show_property)(struct seq_file *,
136 MYPROCTYPE *rc = NULL, *type = NULL;
137 struct proc_dir_entry *parent = NULL;
139 if (procDirRoot == NULL)
140 FAIL("procDirRoot cannot be NULL!", 0);
141 if (name == NULL || name[0] == NULL)
142 FAIL("name must contain at least 1 node name!", 0);
143 type = kzalloc(sizeof(MYPROCTYPE), GFP_KERNEL | __GFP_NORETRY);
145 FAIL("out of memory", 0);
147 type->propertyNames = propertyNames;
148 type->nProperties = 0;
150 type->show_property = show_property;
151 type->procDirRoot = procDirRoot;
152 if (type->propertyNames != 0)
153 while (type->propertyNames[type->nProperties] != NULL)
155 while (type->name[type->nNames] != NULL)
157 type->procDirs = kzalloc((type->nNames + 1) *
158 sizeof(struct proc_dir_entry *),
159 GFP_KERNEL | __GFP_NORETRY);
160 if (type->procDirs == NULL)
161 FAIL("out of memory", 0);
162 parent = procDirRoot;
163 for (i = 0; i < type->nNames; i++) {
164 type->procDirs[i] = createProcDir(type->name[i], parent);
165 if (type->procDirs[i] == NULL)
167 parent = type->procDirs[i];
169 type->procDir = type->procDirs[type->nNames-1];
174 visor_proc_DestroyType(type);
180 EXPORT_SYMBOL_GPL(visor_proc_CreateType);
184 void visor_proc_DestroyType(MYPROCTYPE *type)
188 if (type->procDirs != NULL) {
189 int i = type->nNames-1;
191 if (type->procDirs[i] != NULL) {
192 struct proc_dir_entry *parent = NULL;
194 parent = type->procDirRoot;
196 parent = type->procDirs[i-1];
197 remove_proc_entry(type->name[i], parent);
201 kfree(type->procDirs);
202 type->procDirs = NULL;
206 EXPORT_SYMBOL_GPL(visor_proc_DestroyType);
210 MYPROCOBJECT *visor_proc_CreateObject(MYPROCTYPE *type,
211 const char *name, void *context)
213 MYPROCOBJECT *obj = NULL, *rc = NULL;
217 FAIL("type cannot be NULL", 0);
218 obj = kzalloc(sizeof(MYPROCOBJECT), GFP_KERNEL | __GFP_NORETRY);
220 FAIL("out of memory", 0);
222 obj->context = context;
225 obj->procDir = type->procDir;
227 obj->namesize = strlen(name)+1;
228 obj->name = kmalloc(obj->namesize, GFP_KERNEL | __GFP_NORETRY);
229 if (obj->name == NULL) {
231 FAIL("out of memory", 0);
233 strcpy(obj->name, name);
234 obj->procDir = createProcDir(obj->name, type->procDir);
235 if (obj->procDir == NULL)
238 obj->procDirPropertyContexts =
239 kzalloc((type->nProperties + 1) * sizeof(PROCDIRENTRYCONTEXT),
240 GFP_KERNEL | __GFP_NORETRY);
241 if (obj->procDirPropertyContexts == NULL)
242 FAIL("out of memory", 0);
243 obj->procDirProperties =
244 kzalloc((type->nProperties + 1) * sizeof(struct proc_dir_entry *),
245 GFP_KERNEL | __GFP_NORETRY);
246 if (obj->procDirProperties == NULL)
247 FAIL("out of memory", 0);
248 for (i = 0; i < type->nProperties; i++) {
249 obj->procDirPropertyContexts[i].procObject = obj;
250 obj->procDirPropertyContexts[i].propertyIndex = i;
251 obj->procDirPropertyContexts[i].show_property =
253 if (type->propertyNames[i][0] != '\0') {
254 /* only create properties that have names */
255 obj->procDirProperties[i] =
256 createProcFile(type->propertyNames[i],
257 obj->procDir, &proc_fops,
258 &obj->procDirPropertyContexts[i]);
259 if (obj->procDirProperties[i] == NULL)
267 visor_proc_DestroyObject(obj);
273 EXPORT_SYMBOL_GPL(visor_proc_CreateObject);
277 void visor_proc_DestroyObject(MYPROCOBJECT *obj)
279 MYPROCTYPE *type = NULL;
285 if (obj->procDirProperties != NULL) {
287 for (i = 0; i < type->nProperties; i++) {
288 if (obj->procDirProperties[i] != NULL) {
289 remove_proc_entry(type->propertyNames[i],
291 obj->procDirProperties[i] = NULL;
294 kfree(obj->procDirProperties);
295 obj->procDirProperties = NULL;
297 if (obj->procDirPropertyContexts != NULL) {
298 kfree(obj->procDirPropertyContexts);
299 obj->procDirPropertyContexts = NULL;
301 if (obj->procDir != NULL) {
302 if (obj->name != NULL)
303 remove_proc_entry(obj->name, type->procDir);
306 if (obj->name != NULL) {
312 EXPORT_SYMBOL_GPL(visor_proc_DestroyObject);
316 static int seq_show(struct seq_file *seq, void *offset)
318 PROCDIRENTRYCONTEXT *ctx = (PROCDIRENTRYCONTEXT *)(seq->private);
320 ERRDRV("I don't have a freakin' clue...");
323 (*ctx->show_property)(seq, ctx->procObject->context,