proc_sysctl.c 40 KB

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