proc_sysctl.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. /*
  2. * /proc/sys support
  3. */
  4. #include <linux/init.h>
  5. #include <linux/sysctl.h>
  6. #include <linux/poll.h>
  7. #include <linux/proc_fs.h>
  8. #include <linux/security.h>
  9. #include <linux/namei.h>
  10. #include <linux/module.h>
  11. #include "internal.h"
  12. static const struct dentry_operations proc_sys_dentry_operations;
  13. static const struct file_operations proc_sys_file_operations;
  14. static const struct inode_operations proc_sys_inode_operations;
  15. static const struct file_operations proc_sys_dir_file_operations;
  16. static const struct inode_operations proc_sys_dir_operations;
  17. void proc_sys_poll_notify(struct ctl_table_poll *poll)
  18. {
  19. if (!poll)
  20. return;
  21. atomic_inc(&poll->event);
  22. wake_up_interruptible(&poll->wait);
  23. }
  24. static struct ctl_table root_table[] = {
  25. {
  26. .procname = "",
  27. .mode = S_IFDIR|S_IRUGO|S_IXUGO,
  28. },
  29. { }
  30. };
  31. static struct ctl_table_root sysctl_table_root = {
  32. .default_set.dir.header = {
  33. {{.count = 1,
  34. .nreg = 1,
  35. .ctl_table = root_table }},
  36. .ctl_table_arg = root_table,
  37. .root = &sysctl_table_root,
  38. .set = &sysctl_table_root.default_set,
  39. },
  40. };
  41. static DEFINE_SPINLOCK(sysctl_lock);
  42. static void drop_sysctl_table(struct ctl_table_header *header);
  43. static int sysctl_follow_link(struct ctl_table_header **phead,
  44. struct ctl_table **pentry, struct nsproxy *namespaces);
  45. static int insert_links(struct ctl_table_header *head);
  46. static void put_links(struct ctl_table_header *header);
  47. static void sysctl_print_dir(struct ctl_dir *dir)
  48. {
  49. if (dir->header.parent)
  50. sysctl_print_dir(dir->header.parent);
  51. printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname);
  52. }
  53. static int namecmp(const char *name1, int len1, const char *name2, int len2)
  54. {
  55. int minlen;
  56. int cmp;
  57. minlen = len1;
  58. if (minlen > len2)
  59. minlen = len2;
  60. cmp = memcmp(name1, name2, minlen);
  61. if (cmp == 0)
  62. cmp = len1 - len2;
  63. return cmp;
  64. }
  65. static struct ctl_table *find_entry(struct ctl_table_header **phead,
  66. struct ctl_dir *dir, const char *name, int namelen)
  67. {
  68. struct ctl_table_header *head;
  69. struct ctl_table *entry;
  70. struct rb_node *node = dir->root.rb_node;
  71. while (node)
  72. {
  73. struct ctl_node *ctl_node;
  74. const char *procname;
  75. int cmp;
  76. ctl_node = rb_entry(node, struct ctl_node, node);
  77. head = ctl_node->header;
  78. entry = &head->ctl_table[ctl_node - head->node];
  79. procname = entry->procname;
  80. cmp = namecmp(name, namelen, procname, strlen(procname));
  81. if (cmp < 0)
  82. node = node->rb_left;
  83. else if (cmp > 0)
  84. node = node->rb_right;
  85. else {
  86. *phead = head;
  87. return entry;
  88. }
  89. }
  90. return NULL;
  91. }
  92. static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
  93. {
  94. struct rb_node *node = &head->node[entry - head->ctl_table].node;
  95. struct rb_node **p = &head->parent->root.rb_node;
  96. struct rb_node *parent = NULL;
  97. const char *name = entry->procname;
  98. int namelen = strlen(name);
  99. while (*p) {
  100. struct ctl_table_header *parent_head;
  101. struct ctl_table *parent_entry;
  102. struct ctl_node *parent_node;
  103. const char *parent_name;
  104. int cmp;
  105. parent = *p;
  106. parent_node = rb_entry(parent, struct ctl_node, node);
  107. parent_head = parent_node->header;
  108. parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
  109. parent_name = parent_entry->procname;
  110. cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
  111. if (cmp < 0)
  112. p = &(*p)->rb_left;
  113. else if (cmp > 0)
  114. p = &(*p)->rb_right;
  115. else {
  116. printk(KERN_ERR "sysctl duplicate entry: ");
  117. sysctl_print_dir(head->parent);
  118. printk(KERN_CONT "/%s\n", entry->procname);
  119. return -EEXIST;
  120. }
  121. }
  122. rb_link_node(node, parent, p);
  123. return 0;
  124. }
  125. static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
  126. {
  127. struct rb_node *node = &head->node[entry - head->ctl_table].node;
  128. rb_erase(node, &head->parent->root);
  129. }
  130. static void init_header(struct ctl_table_header *head,
  131. struct ctl_table_root *root, struct ctl_table_set *set,
  132. struct ctl_node *node, struct ctl_table *table)
  133. {
  134. head->ctl_table = table;
  135. head->ctl_table_arg = table;
  136. head->used = 0;
  137. head->count = 1;
  138. head->nreg = 1;
  139. head->unregistering = NULL;
  140. head->root = root;
  141. head->set = set;
  142. head->parent = NULL;
  143. head->node = node;
  144. if (node) {
  145. struct ctl_table *entry;
  146. for (entry = table; entry->procname; entry++, node++) {
  147. rb_init_node(&node->node);
  148. node->header = head;
  149. }
  150. }
  151. }
  152. static void erase_header(struct ctl_table_header *head)
  153. {
  154. struct ctl_table *entry;
  155. for (entry = head->ctl_table; entry->procname; entry++)
  156. erase_entry(head, entry);
  157. }
  158. static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
  159. {
  160. struct ctl_table *entry;
  161. int err;
  162. dir->header.nreg++;
  163. header->parent = dir;
  164. err = insert_links(header);
  165. if (err)
  166. goto fail_links;
  167. for (entry = header->ctl_table; entry->procname; entry++) {
  168. err = insert_entry(header, entry);
  169. if (err)
  170. goto fail;
  171. }
  172. return 0;
  173. fail:
  174. erase_header(header);
  175. put_links(header);
  176. fail_links:
  177. header->parent = NULL;
  178. drop_sysctl_table(&dir->header);
  179. return err;
  180. }
  181. /* called under sysctl_lock */
  182. static int use_table(struct ctl_table_header *p)
  183. {
  184. if (unlikely(p->unregistering))
  185. return 0;
  186. p->used++;
  187. return 1;
  188. }
  189. /* called under sysctl_lock */
  190. static void unuse_table(struct ctl_table_header *p)
  191. {
  192. if (!--p->used)
  193. if (unlikely(p->unregistering))
  194. complete(p->unregistering);
  195. }
  196. /* called under sysctl_lock, will reacquire if has to wait */
  197. static void start_unregistering(struct ctl_table_header *p)
  198. {
  199. /*
  200. * if p->used is 0, nobody will ever touch that entry again;
  201. * we'll eliminate all paths to it before dropping sysctl_lock
  202. */
  203. if (unlikely(p->used)) {
  204. struct completion wait;
  205. init_completion(&wait);
  206. p->unregistering = &wait;
  207. spin_unlock(&sysctl_lock);
  208. wait_for_completion(&wait);
  209. spin_lock(&sysctl_lock);
  210. } else {
  211. /* anything non-NULL; we'll never dereference it */
  212. p->unregistering = ERR_PTR(-EINVAL);
  213. }
  214. /*
  215. * do not remove from the list until nobody holds it; walking the
  216. * list in do_sysctl() relies on that.
  217. */
  218. erase_header(p);
  219. }
  220. static void sysctl_head_get(struct ctl_table_header *head)
  221. {
  222. spin_lock(&sysctl_lock);
  223. head->count++;
  224. spin_unlock(&sysctl_lock);
  225. }
  226. void sysctl_head_put(struct ctl_table_header *head)
  227. {
  228. spin_lock(&sysctl_lock);
  229. if (!--head->count)
  230. kfree_rcu(head, rcu);
  231. spin_unlock(&sysctl_lock);
  232. }
  233. static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
  234. {
  235. if (!head)
  236. BUG();
  237. spin_lock(&sysctl_lock);
  238. if (!use_table(head))
  239. head = ERR_PTR(-ENOENT);
  240. spin_unlock(&sysctl_lock);
  241. return head;
  242. }
  243. static void sysctl_head_finish(struct ctl_table_header *head)
  244. {
  245. if (!head)
  246. return;
  247. spin_lock(&sysctl_lock);
  248. unuse_table(head);
  249. spin_unlock(&sysctl_lock);
  250. }
  251. static struct ctl_table_set *
  252. lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
  253. {
  254. struct ctl_table_set *set = &root->default_set;
  255. if (root->lookup)
  256. set = root->lookup(root, namespaces);
  257. return set;
  258. }
  259. static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
  260. struct ctl_dir *dir,
  261. const char *name, int namelen)
  262. {
  263. struct ctl_table_header *head;
  264. struct ctl_table *entry;
  265. spin_lock(&sysctl_lock);
  266. entry = find_entry(&head, dir, name, namelen);
  267. if (entry && use_table(head))
  268. *phead = head;
  269. else
  270. entry = NULL;
  271. spin_unlock(&sysctl_lock);
  272. return entry;
  273. }
  274. static struct ctl_node *first_usable_entry(struct rb_node *node)
  275. {
  276. struct ctl_node *ctl_node;
  277. for (;node; node = rb_next(node)) {
  278. ctl_node = rb_entry(node, struct ctl_node, node);
  279. if (use_table(ctl_node->header))
  280. return ctl_node;
  281. }
  282. return NULL;
  283. }
  284. static void first_entry(struct ctl_dir *dir,
  285. struct ctl_table_header **phead, struct ctl_table **pentry)
  286. {
  287. struct ctl_table_header *head = NULL;
  288. struct ctl_table *entry = NULL;
  289. struct ctl_node *ctl_node;
  290. spin_lock(&sysctl_lock);
  291. ctl_node = first_usable_entry(rb_first(&dir->root));
  292. spin_unlock(&sysctl_lock);
  293. if (ctl_node) {
  294. head = ctl_node->header;
  295. entry = &head->ctl_table[ctl_node - head->node];
  296. }
  297. *phead = head;
  298. *pentry = entry;
  299. }
  300. static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
  301. {
  302. struct ctl_table_header *head = *phead;
  303. struct ctl_table *entry = *pentry;
  304. struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
  305. spin_lock(&sysctl_lock);
  306. unuse_table(head);
  307. ctl_node = first_usable_entry(rb_next(&ctl_node->node));
  308. spin_unlock(&sysctl_lock);
  309. head = NULL;
  310. if (ctl_node) {
  311. head = ctl_node->header;
  312. entry = &head->ctl_table[ctl_node - head->node];
  313. }
  314. *phead = head;
  315. *pentry = entry;
  316. }
  317. void register_sysctl_root(struct ctl_table_root *root)
  318. {
  319. }
  320. /*
  321. * sysctl_perm does NOT grant the superuser all rights automatically, because
  322. * some sysctl variables are readonly even to root.
  323. */
  324. static int test_perm(int mode, int op)
  325. {
  326. if (!current_euid())
  327. mode >>= 6;
  328. else if (in_egroup_p(0))
  329. mode >>= 3;
  330. if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
  331. return 0;
  332. return -EACCES;
  333. }
  334. static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
  335. {
  336. int mode;
  337. if (root->permissions)
  338. mode = root->permissions(root, current->nsproxy, table);
  339. else
  340. mode = table->mode;
  341. return test_perm(mode, op);
  342. }
  343. static struct inode *proc_sys_make_inode(struct super_block *sb,
  344. struct ctl_table_header *head, struct ctl_table *table)
  345. {
  346. struct inode *inode;
  347. struct proc_inode *ei;
  348. inode = new_inode(sb);
  349. if (!inode)
  350. goto out;
  351. inode->i_ino = get_next_ino();
  352. sysctl_head_get(head);
  353. ei = PROC_I(inode);
  354. ei->sysctl = head;
  355. ei->sysctl_entry = table;
  356. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  357. inode->i_mode = table->mode;
  358. if (!S_ISDIR(table->mode)) {
  359. inode->i_mode |= S_IFREG;
  360. inode->i_op = &proc_sys_inode_operations;
  361. inode->i_fop = &proc_sys_file_operations;
  362. } else {
  363. inode->i_mode |= S_IFDIR;
  364. inode->i_op = &proc_sys_dir_operations;
  365. inode->i_fop = &proc_sys_dir_file_operations;
  366. }
  367. out:
  368. return inode;
  369. }
  370. static struct ctl_table_header *grab_header(struct inode *inode)
  371. {
  372. struct ctl_table_header *head = PROC_I(inode)->sysctl;
  373. if (!head)
  374. head = &sysctl_table_root.default_set.dir.header;
  375. return sysctl_head_grab(head);
  376. }
  377. static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
  378. struct nameidata *nd)
  379. {
  380. struct ctl_table_header *head = grab_header(dir);
  381. struct ctl_table_header *h = NULL;
  382. struct qstr *name = &dentry->d_name;
  383. struct ctl_table *p;
  384. struct inode *inode;
  385. struct dentry *err = ERR_PTR(-ENOENT);
  386. struct ctl_dir *ctl_dir;
  387. int ret;
  388. if (IS_ERR(head))
  389. return ERR_CAST(head);
  390. ctl_dir = container_of(head, struct ctl_dir, header);
  391. p = lookup_entry(&h, ctl_dir, name->name, name->len);
  392. if (!p)
  393. goto out;
  394. ret = sysctl_follow_link(&h, &p, current->nsproxy);
  395. err = ERR_PTR(ret);
  396. if (ret)
  397. goto out;
  398. err = ERR_PTR(-ENOMEM);
  399. inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
  400. if (h)
  401. sysctl_head_finish(h);
  402. if (!inode)
  403. goto out;
  404. err = NULL;
  405. d_set_d_op(dentry, &proc_sys_dentry_operations);
  406. d_add(dentry, inode);
  407. out:
  408. sysctl_head_finish(head);
  409. return err;
  410. }
  411. static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
  412. size_t count, loff_t *ppos, int write)
  413. {
  414. struct inode *inode = filp->f_path.dentry->d_inode;
  415. struct ctl_table_header *head = grab_header(inode);
  416. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  417. ssize_t error;
  418. size_t res;
  419. if (IS_ERR(head))
  420. return PTR_ERR(head);
  421. /*
  422. * At this point we know that the sysctl was not unregistered
  423. * and won't be until we finish.
  424. */
  425. error = -EPERM;
  426. if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
  427. goto out;
  428. /* if that can happen at all, it should be -EINVAL, not -EISDIR */
  429. error = -EINVAL;
  430. if (!table->proc_handler)
  431. goto out;
  432. /* careful: calling conventions are nasty here */
  433. res = count;
  434. error = table->proc_handler(table, write, buf, &res, ppos);
  435. if (!error)
  436. error = res;
  437. out:
  438. sysctl_head_finish(head);
  439. return error;
  440. }
  441. static ssize_t proc_sys_read(struct file *filp, char __user *buf,
  442. size_t count, loff_t *ppos)
  443. {
  444. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
  445. }
  446. static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
  447. size_t count, loff_t *ppos)
  448. {
  449. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
  450. }
  451. static int proc_sys_open(struct inode *inode, struct file *filp)
  452. {
  453. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  454. if (table->poll)
  455. filp->private_data = proc_sys_poll_event(table->poll);
  456. return 0;
  457. }
  458. static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
  459. {
  460. struct inode *inode = filp->f_path.dentry->d_inode;
  461. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  462. unsigned long event = (unsigned long)filp->private_data;
  463. unsigned int ret = DEFAULT_POLLMASK;
  464. if (!table->proc_handler)
  465. goto out;
  466. if (!table->poll)
  467. goto out;
  468. poll_wait(filp, &table->poll->wait, wait);
  469. if (event != atomic_read(&table->poll->event)) {
  470. filp->private_data = proc_sys_poll_event(table->poll);
  471. ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
  472. }
  473. out:
  474. return ret;
  475. }
  476. static int proc_sys_fill_cache(struct file *filp, void *dirent,
  477. filldir_t filldir,
  478. struct ctl_table_header *head,
  479. struct ctl_table *table)
  480. {
  481. struct dentry *child, *dir = filp->f_path.dentry;
  482. struct inode *inode;
  483. struct qstr qname;
  484. ino_t ino = 0;
  485. unsigned type = DT_UNKNOWN;
  486. qname.name = table->procname;
  487. qname.len = strlen(table->procname);
  488. qname.hash = full_name_hash(qname.name, qname.len);
  489. child = d_lookup(dir, &qname);
  490. if (!child) {
  491. child = d_alloc(dir, &qname);
  492. if (child) {
  493. inode = proc_sys_make_inode(dir->d_sb, head, table);
  494. if (!inode) {
  495. dput(child);
  496. return -ENOMEM;
  497. } else {
  498. d_set_d_op(child, &proc_sys_dentry_operations);
  499. d_add(child, inode);
  500. }
  501. } else {
  502. return -ENOMEM;
  503. }
  504. }
  505. inode = child->d_inode;
  506. ino = inode->i_ino;
  507. type = inode->i_mode >> 12;
  508. dput(child);
  509. return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
  510. }
  511. static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
  512. filldir_t filldir,
  513. struct ctl_table_header *head,
  514. struct ctl_table *table)
  515. {
  516. int err, ret = 0;
  517. head = sysctl_head_grab(head);
  518. /* It is not an error if we can not follow the link ignore it */
  519. err = sysctl_follow_link(&head, &table, current->nsproxy);
  520. if (err)
  521. goto out;
  522. ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
  523. out:
  524. sysctl_head_finish(head);
  525. return ret;
  526. }
  527. static int scan(struct ctl_table_header *head, ctl_table *table,
  528. unsigned long *pos, struct file *file,
  529. void *dirent, filldir_t filldir)
  530. {
  531. int res;
  532. if ((*pos)++ < file->f_pos)
  533. return 0;
  534. if (unlikely(S_ISLNK(table->mode)))
  535. res = proc_sys_link_fill_cache(file, dirent, filldir, head, table);
  536. else
  537. res = proc_sys_fill_cache(file, dirent, filldir, head, table);
  538. if (res == 0)
  539. file->f_pos = *pos;
  540. return res;
  541. }
  542. static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
  543. {
  544. struct dentry *dentry = filp->f_path.dentry;
  545. struct inode *inode = dentry->d_inode;
  546. struct ctl_table_header *head = grab_header(inode);
  547. struct ctl_table_header *h = NULL;
  548. struct ctl_table *entry;
  549. struct ctl_dir *ctl_dir;
  550. unsigned long pos;
  551. int ret = -EINVAL;
  552. if (IS_ERR(head))
  553. return PTR_ERR(head);
  554. ctl_dir = container_of(head, struct ctl_dir, header);
  555. ret = 0;
  556. /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
  557. if (filp->f_pos == 0) {
  558. if (filldir(dirent, ".", 1, filp->f_pos,
  559. inode->i_ino, DT_DIR) < 0)
  560. goto out;
  561. filp->f_pos++;
  562. }
  563. if (filp->f_pos == 1) {
  564. if (filldir(dirent, "..", 2, filp->f_pos,
  565. parent_ino(dentry), DT_DIR) < 0)
  566. goto out;
  567. filp->f_pos++;
  568. }
  569. pos = 2;
  570. for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
  571. ret = scan(h, entry, &pos, filp, dirent, filldir);
  572. if (ret) {
  573. sysctl_head_finish(h);
  574. break;
  575. }
  576. }
  577. ret = 1;
  578. out:
  579. sysctl_head_finish(head);
  580. return ret;
  581. }
  582. static int proc_sys_permission(struct inode *inode, int mask)
  583. {
  584. /*
  585. * sysctl entries that are not writeable,
  586. * are _NOT_ writeable, capabilities or not.
  587. */
  588. struct ctl_table_header *head;
  589. struct ctl_table *table;
  590. int error;
  591. /* Executable files are not allowed under /proc/sys/ */
  592. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
  593. return -EACCES;
  594. head = grab_header(inode);
  595. if (IS_ERR(head))
  596. return PTR_ERR(head);
  597. table = PROC_I(inode)->sysctl_entry;
  598. if (!table) /* global root - r-xr-xr-x */
  599. error = mask & MAY_WRITE ? -EACCES : 0;
  600. else /* Use the permissions on the sysctl table entry */
  601. error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
  602. sysctl_head_finish(head);
  603. return error;
  604. }
  605. static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
  606. {
  607. struct inode *inode = dentry->d_inode;
  608. int error;
  609. if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  610. return -EPERM;
  611. error = inode_change_ok(inode, attr);
  612. if (error)
  613. return error;
  614. if ((attr->ia_valid & ATTR_SIZE) &&
  615. attr->ia_size != i_size_read(inode)) {
  616. error = vmtruncate(inode, attr->ia_size);
  617. if (error)
  618. return error;
  619. }
  620. setattr_copy(inode, attr);
  621. mark_inode_dirty(inode);
  622. return 0;
  623. }
  624. static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  625. {
  626. struct inode *inode = dentry->d_inode;
  627. struct ctl_table_header *head = grab_header(inode);
  628. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  629. if (IS_ERR(head))
  630. return PTR_ERR(head);
  631. generic_fillattr(inode, stat);
  632. if (table)
  633. stat->mode = (stat->mode & S_IFMT) | table->mode;
  634. sysctl_head_finish(head);
  635. return 0;
  636. }
  637. static const struct file_operations proc_sys_file_operations = {
  638. .open = proc_sys_open,
  639. .poll = proc_sys_poll,
  640. .read = proc_sys_read,
  641. .write = proc_sys_write,
  642. .llseek = default_llseek,
  643. };
  644. static const struct file_operations proc_sys_dir_file_operations = {
  645. .read = generic_read_dir,
  646. .readdir = proc_sys_readdir,
  647. .llseek = generic_file_llseek,
  648. };
  649. static const struct inode_operations proc_sys_inode_operations = {
  650. .permission = proc_sys_permission,
  651. .setattr = proc_sys_setattr,
  652. .getattr = proc_sys_getattr,
  653. };
  654. static const struct inode_operations proc_sys_dir_operations = {
  655. .lookup = proc_sys_lookup,
  656. .permission = proc_sys_permission,
  657. .setattr = proc_sys_setattr,
  658. .getattr = proc_sys_getattr,
  659. };
  660. static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
  661. {
  662. if (nd->flags & LOOKUP_RCU)
  663. return -ECHILD;
  664. return !PROC_I(dentry->d_inode)->sysctl->unregistering;
  665. }
  666. static int proc_sys_delete(const struct dentry *dentry)
  667. {
  668. return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
  669. }
  670. static int sysctl_is_seen(struct ctl_table_header *p)
  671. {
  672. struct ctl_table_set *set = p->set;
  673. int res;
  674. spin_lock(&sysctl_lock);
  675. if (p->unregistering)
  676. res = 0;
  677. else if (!set->is_seen)
  678. res = 1;
  679. else
  680. res = set->is_seen(set);
  681. spin_unlock(&sysctl_lock);
  682. return res;
  683. }
  684. static int proc_sys_compare(const struct dentry *parent,
  685. const struct inode *pinode,
  686. const struct dentry *dentry, const struct inode *inode,
  687. unsigned int len, const char *str, const struct qstr *name)
  688. {
  689. struct ctl_table_header *head;
  690. /* Although proc doesn't have negative dentries, rcu-walk means
  691. * that inode here can be NULL */
  692. /* AV: can it, indeed? */
  693. if (!inode)
  694. return 1;
  695. if (name->len != len)
  696. return 1;
  697. if (memcmp(name->name, str, len))
  698. return 1;
  699. head = rcu_dereference(PROC_I(inode)->sysctl);
  700. return !head || !sysctl_is_seen(head);
  701. }
  702. static const struct dentry_operations proc_sys_dentry_operations = {
  703. .d_revalidate = proc_sys_revalidate,
  704. .d_delete = proc_sys_delete,
  705. .d_compare = proc_sys_compare,
  706. };
  707. static struct ctl_dir *find_subdir(struct ctl_dir *dir,
  708. const char *name, int namelen)
  709. {
  710. struct ctl_table_header *head;
  711. struct ctl_table *entry;
  712. entry = find_entry(&head, dir, name, namelen);
  713. if (!entry)
  714. return ERR_PTR(-ENOENT);
  715. if (S_ISDIR(entry->mode))
  716. return container_of(head, struct ctl_dir, header);
  717. return ERR_PTR(-ENOTDIR);
  718. }
  719. static struct ctl_dir *new_dir(struct ctl_table_set *set,
  720. const char *name, int namelen)
  721. {
  722. struct ctl_table *table;
  723. struct ctl_dir *new;
  724. struct ctl_node *node;
  725. char *new_name;
  726. new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
  727. sizeof(struct ctl_table)*2 + namelen + 1,
  728. GFP_KERNEL);
  729. if (!new)
  730. return NULL;
  731. node = (struct ctl_node *)(new + 1);
  732. table = (struct ctl_table *)(node + 1);
  733. new_name = (char *)(table + 2);
  734. memcpy(new_name, name, namelen);
  735. new_name[namelen] = '\0';
  736. table[0].procname = new_name;
  737. table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
  738. init_header(&new->header, set->dir.header.root, set, node, table);
  739. return new;
  740. }
  741. static struct ctl_dir *get_subdir(struct ctl_dir *dir,
  742. const char *name, int namelen)
  743. {
  744. struct ctl_table_set *set = dir->header.set;
  745. struct ctl_dir *subdir, *new = NULL;
  746. spin_lock(&sysctl_lock);
  747. subdir = find_subdir(dir, name, namelen);
  748. if (!IS_ERR(subdir))
  749. goto found;
  750. if (PTR_ERR(subdir) != -ENOENT)
  751. goto failed;
  752. spin_unlock(&sysctl_lock);
  753. new = new_dir(set, name, namelen);
  754. spin_lock(&sysctl_lock);
  755. subdir = ERR_PTR(-ENOMEM);
  756. if (!new)
  757. goto failed;
  758. subdir = find_subdir(dir, name, namelen);
  759. if (!IS_ERR(subdir))
  760. goto found;
  761. if (PTR_ERR(subdir) != -ENOENT)
  762. goto failed;
  763. if (insert_header(dir, &new->header))
  764. goto failed;
  765. subdir = new;
  766. found:
  767. subdir->header.nreg++;
  768. failed:
  769. if (unlikely(IS_ERR(subdir))) {
  770. printk(KERN_ERR "sysctl could not get directory: ");
  771. sysctl_print_dir(dir);
  772. printk(KERN_CONT "/%*.*s %ld\n",
  773. namelen, namelen, name, PTR_ERR(subdir));
  774. }
  775. drop_sysctl_table(&dir->header);
  776. if (new)
  777. drop_sysctl_table(&new->header);
  778. spin_unlock(&sysctl_lock);
  779. return subdir;
  780. }
  781. static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
  782. {
  783. struct ctl_dir *parent;
  784. const char *procname;
  785. if (!dir->header.parent)
  786. return &set->dir;
  787. parent = xlate_dir(set, dir->header.parent);
  788. if (IS_ERR(parent))
  789. return parent;
  790. procname = dir->header.ctl_table[0].procname;
  791. return find_subdir(parent, procname, strlen(procname));
  792. }
  793. static int sysctl_follow_link(struct ctl_table_header **phead,
  794. struct ctl_table **pentry, struct nsproxy *namespaces)
  795. {
  796. struct ctl_table_header *head;
  797. struct ctl_table_root *root;
  798. struct ctl_table_set *set;
  799. struct ctl_table *entry;
  800. struct ctl_dir *dir;
  801. int ret;
  802. /* Get out quickly if not a link */
  803. if (!S_ISLNK((*pentry)->mode))
  804. return 0;
  805. ret = 0;
  806. spin_lock(&sysctl_lock);
  807. root = (*pentry)->data;
  808. set = lookup_header_set(root, namespaces);
  809. dir = xlate_dir(set, (*phead)->parent);
  810. if (IS_ERR(dir))
  811. ret = PTR_ERR(dir);
  812. else {
  813. const char *procname = (*pentry)->procname;
  814. head = NULL;
  815. entry = find_entry(&head, dir, procname, strlen(procname));
  816. ret = -ENOENT;
  817. if (entry && use_table(head)) {
  818. unuse_table(*phead);
  819. *phead = head;
  820. *pentry = entry;
  821. ret = 0;
  822. }
  823. }
  824. spin_unlock(&sysctl_lock);
  825. return ret;
  826. }
  827. static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
  828. {
  829. struct va_format vaf;
  830. va_list args;
  831. va_start(args, fmt);
  832. vaf.fmt = fmt;
  833. vaf.va = &args;
  834. printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
  835. path, table->procname, &vaf);
  836. va_end(args);
  837. return -EINVAL;
  838. }
  839. static int sysctl_check_table(const char *path, struct ctl_table *table)
  840. {
  841. int err = 0;
  842. for (; table->procname; table++) {
  843. if (table->child)
  844. err = sysctl_err(path, table, "Not a file");
  845. if ((table->proc_handler == proc_dostring) ||
  846. (table->proc_handler == proc_dointvec) ||
  847. (table->proc_handler == proc_dointvec_minmax) ||
  848. (table->proc_handler == proc_dointvec_jiffies) ||
  849. (table->proc_handler == proc_dointvec_userhz_jiffies) ||
  850. (table->proc_handler == proc_dointvec_ms_jiffies) ||
  851. (table->proc_handler == proc_doulongvec_minmax) ||
  852. (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
  853. if (!table->data)
  854. err = sysctl_err(path, table, "No data");
  855. if (!table->maxlen)
  856. err = sysctl_err(path, table, "No maxlen");
  857. }
  858. if (!table->proc_handler)
  859. err = sysctl_err(path, table, "No proc_handler");
  860. if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
  861. err = sysctl_err(path, table, "bogus .mode 0%o",
  862. table->mode);
  863. }
  864. return err;
  865. }
  866. static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
  867. struct ctl_table_root *link_root)
  868. {
  869. struct ctl_table *link_table, *entry, *link;
  870. struct ctl_table_header *links;
  871. struct ctl_node *node;
  872. char *link_name;
  873. int nr_entries, name_bytes;
  874. name_bytes = 0;
  875. nr_entries = 0;
  876. for (entry = table; entry->procname; entry++) {
  877. nr_entries++;
  878. name_bytes += strlen(entry->procname) + 1;
  879. }
  880. links = kzalloc(sizeof(struct ctl_table_header) +
  881. sizeof(struct ctl_node)*nr_entries +
  882. sizeof(struct ctl_table)*(nr_entries + 1) +
  883. name_bytes,
  884. GFP_KERNEL);
  885. if (!links)
  886. return NULL;
  887. node = (struct ctl_node *)(links + 1);
  888. link_table = (struct ctl_table *)(node + nr_entries);
  889. link_name = (char *)&link_table[nr_entries + 1];
  890. for (link = link_table, entry = table; entry->procname; link++, entry++) {
  891. int len = strlen(entry->procname) + 1;
  892. memcpy(link_name, entry->procname, len);
  893. link->procname = link_name;
  894. link->mode = S_IFLNK|S_IRWXUGO;
  895. link->data = link_root;
  896. link_name += len;
  897. }
  898. init_header(links, dir->header.root, dir->header.set, node, link_table);
  899. links->nreg = nr_entries;
  900. return links;
  901. }
  902. static bool get_links(struct ctl_dir *dir,
  903. struct ctl_table *table, struct ctl_table_root *link_root)
  904. {
  905. struct ctl_table_header *head;
  906. struct ctl_table *entry, *link;
  907. /* Are there links available for every entry in table? */
  908. for (entry = table; entry->procname; entry++) {
  909. const char *procname = entry->procname;
  910. link = find_entry(&head, dir, procname, strlen(procname));
  911. if (!link)
  912. return false;
  913. if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
  914. continue;
  915. if (S_ISLNK(link->mode) && (link->data == link_root))
  916. continue;
  917. return false;
  918. }
  919. /* The checks passed. Increase the registration count on the links */
  920. for (entry = table; entry->procname; entry++) {
  921. const char *procname = entry->procname;
  922. link = find_entry(&head, dir, procname, strlen(procname));
  923. head->nreg++;
  924. }
  925. return true;
  926. }
  927. static int insert_links(struct ctl_table_header *head)
  928. {
  929. struct ctl_table_set *root_set = &sysctl_table_root.default_set;
  930. struct ctl_dir *core_parent = NULL;
  931. struct ctl_table_header *links;
  932. int err;
  933. if (head->set == root_set)
  934. return 0;
  935. core_parent = xlate_dir(root_set, head->parent);
  936. if (IS_ERR(core_parent))
  937. return 0;
  938. if (get_links(core_parent, head->ctl_table, head->root))
  939. return 0;
  940. core_parent->header.nreg++;
  941. spin_unlock(&sysctl_lock);
  942. links = new_links(core_parent, head->ctl_table, head->root);
  943. spin_lock(&sysctl_lock);
  944. err = -ENOMEM;
  945. if (!links)
  946. goto out;
  947. err = 0;
  948. if (get_links(core_parent, head->ctl_table, head->root)) {
  949. kfree(links);
  950. goto out;
  951. }
  952. err = insert_header(core_parent, links);
  953. if (err)
  954. kfree(links);
  955. out:
  956. drop_sysctl_table(&core_parent->header);
  957. return err;
  958. }
  959. /**
  960. * __register_sysctl_table - register a leaf sysctl table
  961. * @set: Sysctl tree to register on
  962. * @path: The path to the directory the sysctl table is in.
  963. * @table: the top-level table structure
  964. *
  965. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  966. * array. A completely 0 filled entry terminates the table.
  967. *
  968. * The members of the &struct ctl_table structure are used as follows:
  969. *
  970. * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
  971. * enter a sysctl file
  972. *
  973. * data - a pointer to data for use by proc_handler
  974. *
  975. * maxlen - the maximum size in bytes of the data
  976. *
  977. * mode - the file permissions for the /proc/sys file
  978. *
  979. * child - must be %NULL.
  980. *
  981. * proc_handler - the text handler routine (described below)
  982. *
  983. * extra1, extra2 - extra pointers usable by the proc handler routines
  984. *
  985. * Leaf nodes in the sysctl tree will be represented by a single file
  986. * under /proc; non-leaf nodes will be represented by directories.
  987. *
  988. * There must be a proc_handler routine for any terminal nodes.
  989. * Several default handlers are available to cover common cases -
  990. *
  991. * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
  992. * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
  993. * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
  994. *
  995. * It is the handler's job to read the input buffer from user memory
  996. * and process it. The handler should return 0 on success.
  997. *
  998. * This routine returns %NULL on a failure to register, and a pointer
  999. * to the table header on success.
  1000. */
  1001. struct ctl_table_header *__register_sysctl_table(
  1002. struct ctl_table_set *set,
  1003. const char *path, struct ctl_table *table)
  1004. {
  1005. struct ctl_table_root *root = set->dir.header.root;
  1006. struct ctl_table_header *header;
  1007. const char *name, *nextname;
  1008. struct ctl_dir *dir;
  1009. struct ctl_table *entry;
  1010. struct ctl_node *node;
  1011. int nr_entries = 0;
  1012. for (entry = table; entry->procname; entry++)
  1013. nr_entries++;
  1014. header = kzalloc(sizeof(struct ctl_table_header) +
  1015. sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
  1016. if (!header)
  1017. return NULL;
  1018. node = (struct ctl_node *)(header + 1);
  1019. init_header(header, root, set, node, table);
  1020. if (sysctl_check_table(path, table))
  1021. goto fail;
  1022. spin_lock(&sysctl_lock);
  1023. dir = &set->dir;
  1024. dir->header.nreg++;
  1025. spin_unlock(&sysctl_lock);
  1026. /* Find the directory for the ctl_table */
  1027. for (name = path; name; name = nextname) {
  1028. int namelen;
  1029. nextname = strchr(name, '/');
  1030. if (nextname) {
  1031. namelen = nextname - name;
  1032. nextname++;
  1033. } else {
  1034. namelen = strlen(name);
  1035. }
  1036. if (namelen == 0)
  1037. continue;
  1038. dir = get_subdir(dir, name, namelen);
  1039. if (IS_ERR(dir))
  1040. goto fail;
  1041. }
  1042. spin_lock(&sysctl_lock);
  1043. if (insert_header(dir, header))
  1044. goto fail_put_dir_locked;
  1045. drop_sysctl_table(&dir->header);
  1046. spin_unlock(&sysctl_lock);
  1047. return header;
  1048. fail_put_dir_locked:
  1049. drop_sysctl_table(&dir->header);
  1050. spin_unlock(&sysctl_lock);
  1051. fail:
  1052. kfree(header);
  1053. dump_stack();
  1054. return NULL;
  1055. }
  1056. /**
  1057. * register_sysctl - register a sysctl table
  1058. * @path: The path to the directory the sysctl table is in.
  1059. * @table: the table structure
  1060. *
  1061. * Register a sysctl table. @table should be a filled in ctl_table
  1062. * array. A completely 0 filled entry terminates the table.
  1063. *
  1064. * See __register_sysctl_table for more details.
  1065. */
  1066. struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
  1067. {
  1068. return __register_sysctl_table(&sysctl_table_root.default_set,
  1069. path, table);
  1070. }
  1071. EXPORT_SYMBOL(register_sysctl);
  1072. static char *append_path(const char *path, char *pos, const char *name)
  1073. {
  1074. int namelen;
  1075. namelen = strlen(name);
  1076. if (((pos - path) + namelen + 2) >= PATH_MAX)
  1077. return NULL;
  1078. memcpy(pos, name, namelen);
  1079. pos[namelen] = '/';
  1080. pos[namelen + 1] = '\0';
  1081. pos += namelen + 1;
  1082. return pos;
  1083. }
  1084. static int count_subheaders(struct ctl_table *table)
  1085. {
  1086. int has_files = 0;
  1087. int nr_subheaders = 0;
  1088. struct ctl_table *entry;
  1089. /* special case: no directory and empty directory */
  1090. if (!table || !table->procname)
  1091. return 1;
  1092. for (entry = table; entry->procname; entry++) {
  1093. if (entry->child)
  1094. nr_subheaders += count_subheaders(entry->child);
  1095. else
  1096. has_files = 1;
  1097. }
  1098. return nr_subheaders + has_files;
  1099. }
  1100. static int register_leaf_sysctl_tables(const char *path, char *pos,
  1101. struct ctl_table_header ***subheader, struct ctl_table_set *set,
  1102. struct ctl_table *table)
  1103. {
  1104. struct ctl_table *ctl_table_arg = NULL;
  1105. struct ctl_table *entry, *files;
  1106. int nr_files = 0;
  1107. int nr_dirs = 0;
  1108. int err = -ENOMEM;
  1109. for (entry = table; entry->procname; entry++) {
  1110. if (entry->child)
  1111. nr_dirs++;
  1112. else
  1113. nr_files++;
  1114. }
  1115. files = table;
  1116. /* If there are mixed files and directories we need a new table */
  1117. if (nr_dirs && nr_files) {
  1118. struct ctl_table *new;
  1119. files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
  1120. GFP_KERNEL);
  1121. if (!files)
  1122. goto out;
  1123. ctl_table_arg = files;
  1124. for (new = files, entry = table; entry->procname; entry++) {
  1125. if (entry->child)
  1126. continue;
  1127. *new = *entry;
  1128. new++;
  1129. }
  1130. }
  1131. /* Register everything except a directory full of subdirectories */
  1132. if (nr_files || !nr_dirs) {
  1133. struct ctl_table_header *header;
  1134. header = __register_sysctl_table(set, path, files);
  1135. if (!header) {
  1136. kfree(ctl_table_arg);
  1137. goto out;
  1138. }
  1139. /* Remember if we need to free the file table */
  1140. header->ctl_table_arg = ctl_table_arg;
  1141. **subheader = header;
  1142. (*subheader)++;
  1143. }
  1144. /* Recurse into the subdirectories. */
  1145. for (entry = table; entry->procname; entry++) {
  1146. char *child_pos;
  1147. if (!entry->child)
  1148. continue;
  1149. err = -ENAMETOOLONG;
  1150. child_pos = append_path(path, pos, entry->procname);
  1151. if (!child_pos)
  1152. goto out;
  1153. err = register_leaf_sysctl_tables(path, child_pos, subheader,
  1154. set, entry->child);
  1155. pos[0] = '\0';
  1156. if (err)
  1157. goto out;
  1158. }
  1159. err = 0;
  1160. out:
  1161. /* On failure our caller will unregister all registered subheaders */
  1162. return err;
  1163. }
  1164. /**
  1165. * __register_sysctl_paths - register a sysctl table hierarchy
  1166. * @set: Sysctl tree to register on
  1167. * @path: The path to the directory the sysctl table is in.
  1168. * @table: the top-level table structure
  1169. *
  1170. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1171. * array. A completely 0 filled entry terminates the table.
  1172. *
  1173. * See __register_sysctl_table for more details.
  1174. */
  1175. struct ctl_table_header *__register_sysctl_paths(
  1176. struct ctl_table_set *set,
  1177. const struct ctl_path *path, struct ctl_table *table)
  1178. {
  1179. struct ctl_table *ctl_table_arg = table;
  1180. int nr_subheaders = count_subheaders(table);
  1181. struct ctl_table_header *header = NULL, **subheaders, **subheader;
  1182. const struct ctl_path *component;
  1183. char *new_path, *pos;
  1184. pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
  1185. if (!new_path)
  1186. return NULL;
  1187. pos[0] = '\0';
  1188. for (component = path; component->procname; component++) {
  1189. pos = append_path(new_path, pos, component->procname);
  1190. if (!pos)
  1191. goto out;
  1192. }
  1193. while (table->procname && table->child && !table[1].procname) {
  1194. pos = append_path(new_path, pos, table->procname);
  1195. if (!pos)
  1196. goto out;
  1197. table = table->child;
  1198. }
  1199. if (nr_subheaders == 1) {
  1200. header = __register_sysctl_table(set, new_path, table);
  1201. if (header)
  1202. header->ctl_table_arg = ctl_table_arg;
  1203. } else {
  1204. header = kzalloc(sizeof(*header) +
  1205. sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
  1206. if (!header)
  1207. goto out;
  1208. subheaders = (struct ctl_table_header **) (header + 1);
  1209. subheader = subheaders;
  1210. header->ctl_table_arg = ctl_table_arg;
  1211. if (register_leaf_sysctl_tables(new_path, pos, &subheader,
  1212. set, table))
  1213. goto err_register_leaves;
  1214. }
  1215. out:
  1216. kfree(new_path);
  1217. return header;
  1218. err_register_leaves:
  1219. while (subheader > subheaders) {
  1220. struct ctl_table_header *subh = *(--subheader);
  1221. struct ctl_table *table = subh->ctl_table_arg;
  1222. unregister_sysctl_table(subh);
  1223. kfree(table);
  1224. }
  1225. kfree(header);
  1226. header = NULL;
  1227. goto out;
  1228. }
  1229. /**
  1230. * register_sysctl_table_path - register a sysctl table hierarchy
  1231. * @path: The path to the directory the sysctl table is in.
  1232. * @table: the top-level table structure
  1233. *
  1234. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1235. * array. A completely 0 filled entry terminates the table.
  1236. *
  1237. * See __register_sysctl_paths for more details.
  1238. */
  1239. struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
  1240. struct ctl_table *table)
  1241. {
  1242. return __register_sysctl_paths(&sysctl_table_root.default_set,
  1243. path, table);
  1244. }
  1245. EXPORT_SYMBOL(register_sysctl_paths);
  1246. /**
  1247. * register_sysctl_table - register a sysctl table hierarchy
  1248. * @table: the top-level table structure
  1249. *
  1250. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1251. * array. A completely 0 filled entry terminates the table.
  1252. *
  1253. * See register_sysctl_paths for more details.
  1254. */
  1255. struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
  1256. {
  1257. static const struct ctl_path null_path[] = { {} };
  1258. return register_sysctl_paths(null_path, table);
  1259. }
  1260. EXPORT_SYMBOL(register_sysctl_table);
  1261. static void put_links(struct ctl_table_header *header)
  1262. {
  1263. struct ctl_table_set *root_set = &sysctl_table_root.default_set;
  1264. struct ctl_table_root *root = header->root;
  1265. struct ctl_dir *parent = header->parent;
  1266. struct ctl_dir *core_parent;
  1267. struct ctl_table *entry;
  1268. if (header->set == root_set)
  1269. return;
  1270. core_parent = xlate_dir(root_set, parent);
  1271. if (IS_ERR(core_parent))
  1272. return;
  1273. for (entry = header->ctl_table; entry->procname; entry++) {
  1274. struct ctl_table_header *link_head;
  1275. struct ctl_table *link;
  1276. const char *name = entry->procname;
  1277. link = find_entry(&link_head, core_parent, name, strlen(name));
  1278. if (link &&
  1279. ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
  1280. (S_ISLNK(link->mode) && (link->data == root)))) {
  1281. drop_sysctl_table(link_head);
  1282. }
  1283. else {
  1284. printk(KERN_ERR "sysctl link missing during unregister: ");
  1285. sysctl_print_dir(parent);
  1286. printk(KERN_CONT "/%s\n", name);
  1287. }
  1288. }
  1289. }
  1290. static void drop_sysctl_table(struct ctl_table_header *header)
  1291. {
  1292. struct ctl_dir *parent = header->parent;
  1293. if (--header->nreg)
  1294. return;
  1295. put_links(header);
  1296. start_unregistering(header);
  1297. if (!--header->count)
  1298. kfree_rcu(header, rcu);
  1299. if (parent)
  1300. drop_sysctl_table(&parent->header);
  1301. }
  1302. /**
  1303. * unregister_sysctl_table - unregister a sysctl table hierarchy
  1304. * @header: the header returned from register_sysctl_table
  1305. *
  1306. * Unregisters the sysctl table and all children. proc entries may not
  1307. * actually be removed until they are no longer used by anyone.
  1308. */
  1309. void unregister_sysctl_table(struct ctl_table_header * header)
  1310. {
  1311. int nr_subheaders;
  1312. might_sleep();
  1313. if (header == NULL)
  1314. return;
  1315. nr_subheaders = count_subheaders(header->ctl_table_arg);
  1316. if (unlikely(nr_subheaders > 1)) {
  1317. struct ctl_table_header **subheaders;
  1318. int i;
  1319. subheaders = (struct ctl_table_header **)(header + 1);
  1320. for (i = nr_subheaders -1; i >= 0; i--) {
  1321. struct ctl_table_header *subh = subheaders[i];
  1322. struct ctl_table *table = subh->ctl_table_arg;
  1323. unregister_sysctl_table(subh);
  1324. kfree(table);
  1325. }
  1326. kfree(header);
  1327. return;
  1328. }
  1329. spin_lock(&sysctl_lock);
  1330. drop_sysctl_table(header);
  1331. spin_unlock(&sysctl_lock);
  1332. }
  1333. EXPORT_SYMBOL(unregister_sysctl_table);
  1334. void setup_sysctl_set(struct ctl_table_set *set,
  1335. struct ctl_table_root *root,
  1336. int (*is_seen)(struct ctl_table_set *))
  1337. {
  1338. memset(set, sizeof(*set), 0);
  1339. set->is_seen = is_seen;
  1340. init_header(&set->dir.header, root, set, NULL, root_table);
  1341. }
  1342. void retire_sysctl_set(struct ctl_table_set *set)
  1343. {
  1344. WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
  1345. }
  1346. int __init proc_sys_init(void)
  1347. {
  1348. struct proc_dir_entry *proc_sys_root;
  1349. proc_sys_root = proc_mkdir("sys", NULL);
  1350. proc_sys_root->proc_iops = &proc_sys_dir_operations;
  1351. proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
  1352. proc_sys_root->nlink = 0;
  1353. return sysctl_init();
  1354. }