proc_sysctl.c 39 KB

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