proc_sysctl.c 39 KB

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