proc_sysctl.c 39 KB

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