3 * Copyright (C) 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);
100 ERRDRV("failed to create /proc directory %s", name);
104 static struct proc_dir_entry *
105 createProcFile(const char *name, struct proc_dir_entry *parent,
106 const struct file_operations *fops, void *data)
108 struct proc_dir_entry *p = proc_create_data(name, 0, parent,
111 ERRDRV("failed to create /proc file %s", name);
115 static int seq_show(struct seq_file *seq, void *offset);
116 static int proc_open(struct inode *inode, struct file *file)
118 return single_open(file, seq_show, PDE_DATA(inode));
121 static const struct file_operations proc_fops = {
125 .release = single_release,
130 MYPROCTYPE *visor_proc_CreateType(struct proc_dir_entry *procDirRoot,
132 const char **propertyNames,
133 void (*show_property)(struct seq_file *,
137 MYPROCTYPE *rc = NULL, *type = NULL;
138 struct proc_dir_entry *parent = NULL;
140 if (procDirRoot == NULL) {
141 ERRDRV("procDirRoot cannot be NULL!\n");
144 if (name == NULL || name[0] == NULL) {
145 ERRDRV("name must contain at least 1 node name!\n");
148 type = kzalloc(sizeof(MYPROCTYPE), GFP_KERNEL | __GFP_NORETRY);
150 ERRDRV("out of memory\n");
154 type->propertyNames = propertyNames;
155 type->nProperties = 0;
157 type->show_property = show_property;
158 type->procDirRoot = procDirRoot;
159 if (type->propertyNames != NULL)
160 while (type->propertyNames[type->nProperties] != NULL)
162 while (type->name[type->nNames] != NULL)
164 type->procDirs = kzalloc((type->nNames + 1) *
165 sizeof(struct proc_dir_entry *),
166 GFP_KERNEL | __GFP_NORETRY);
167 if (type->procDirs == NULL) {
168 ERRDRV("out of memory\n");
171 parent = procDirRoot;
172 for (i = 0; i < type->nNames; i++) {
173 type->procDirs[i] = createProcDir(type->name[i], parent);
174 if (type->procDirs[i] == NULL) {
178 parent = type->procDirs[i];
180 type->procDir = type->procDirs[type->nNames-1];
185 visor_proc_DestroyType(type);
191 EXPORT_SYMBOL_GPL(visor_proc_CreateType);
195 void visor_proc_DestroyType(MYPROCTYPE *type)
199 if (type->procDirs != NULL) {
200 int i = type->nNames-1;
203 if (type->procDirs[i] != NULL) {
204 struct proc_dir_entry *parent = NULL;
207 parent = type->procDirRoot;
209 parent = type->procDirs[i-1];
210 remove_proc_entry(type->name[i], parent);
214 kfree(type->procDirs);
215 type->procDirs = NULL;
219 EXPORT_SYMBOL_GPL(visor_proc_DestroyType);
223 MYPROCOBJECT *visor_proc_CreateObject(MYPROCTYPE *type,
224 const char *name, void *context)
226 MYPROCOBJECT *obj = NULL, *rc = NULL;
230 ERRDRV("type cannot be NULL\n");
233 obj = kzalloc(sizeof(MYPROCOBJECT), GFP_KERNEL | __GFP_NORETRY);
235 ERRDRV("out of memory\n");
239 obj->context = context;
242 obj->procDir = type->procDir;
244 obj->namesize = strlen(name)+1;
245 obj->name = kmalloc(obj->namesize, GFP_KERNEL | __GFP_NORETRY);
246 if (obj->name == NULL) {
248 ERRDRV("out of memory\n");
251 strcpy(obj->name, name);
252 obj->procDir = createProcDir(obj->name, type->procDir);
253 if (obj->procDir == NULL)
256 obj->procDirPropertyContexts =
257 kzalloc((type->nProperties + 1) * sizeof(PROCDIRENTRYCONTEXT),
258 GFP_KERNEL | __GFP_NORETRY);
259 if (obj->procDirPropertyContexts == NULL) {
260 ERRDRV("out of memory\n");
263 obj->procDirProperties =
264 kzalloc((type->nProperties + 1) * sizeof(struct proc_dir_entry *),
265 GFP_KERNEL | __GFP_NORETRY);
266 if (obj->procDirProperties == NULL) {
267 ERRDRV("out of memory\n");
270 for (i = 0; i < type->nProperties; i++) {
271 obj->procDirPropertyContexts[i].procObject = obj;
272 obj->procDirPropertyContexts[i].propertyIndex = i;
273 obj->procDirPropertyContexts[i].show_property =
275 if (type->propertyNames[i][0] != '\0') {
276 /* only create properties that have names */
277 obj->procDirProperties[i] =
278 createProcFile(type->propertyNames[i],
279 obj->procDir, &proc_fops,
280 &obj->procDirPropertyContexts[i]);
281 if (obj->procDirProperties[i] == NULL) {
291 visor_proc_DestroyObject(obj);
297 EXPORT_SYMBOL_GPL(visor_proc_CreateObject);
301 void visor_proc_DestroyObject(MYPROCOBJECT *obj)
303 MYPROCTYPE *type = NULL;
310 if (obj->procDirProperties != NULL) {
313 for (i = 0; i < type->nProperties; i++) {
314 if (obj->procDirProperties[i] != NULL) {
315 remove_proc_entry(type->propertyNames[i],
317 obj->procDirProperties[i] = NULL;
320 kfree(obj->procDirProperties);
321 obj->procDirProperties = NULL;
324 kfree(obj->procDirPropertyContexts);
325 obj->procDirPropertyContexts = NULL;
327 if (obj->procDir != NULL) {
328 if (obj->name != NULL)
329 remove_proc_entry(obj->name, type->procDir);
337 EXPORT_SYMBOL_GPL(visor_proc_DestroyObject);
341 static int seq_show(struct seq_file *seq, void *offset)
343 PROCDIRENTRYCONTEXT *ctx = (PROCDIRENTRYCONTEXT *)(seq->private);
346 ERRDRV("I don't have a freakin' clue...");
349 (*ctx->show_property)(seq, ctx->procObject->context,