proc_sysctl.c 41 KB

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