proc_sysctl.c 39 KB

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