proc_sysctl.c 39 KB

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