proc_sysctl.c 40 KB

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