proc_sysctl.c 39 KB

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