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/cred.h>
  12. #include <linux/namei.h>
  13. #include <linux/mm.h>
  14. #include <linux/module.h>
  15. #include "internal.h"
  16. static const struct dentry_operations proc_sys_dentry_operations;
  17. static const struct file_operations proc_sys_file_operations;
  18. static const struct inode_operations proc_sys_inode_operations;
  19. static const struct file_operations proc_sys_dir_file_operations;
  20. static const struct inode_operations proc_sys_dir_operations;
  21. /* Support for permanently empty directories */
  22. struct ctl_table sysctl_mount_point[] = {
  23. { }
  24. };
  25. static bool is_empty_dir(struct ctl_table_header *head)
  26. {
  27. return head->ctl_table[0].child == sysctl_mount_point;
  28. }
  29. static void set_empty_dir(struct ctl_dir *dir)
  30. {
  31. dir->header.ctl_table[0].child = sysctl_mount_point;
  32. }
  33. static void clear_empty_dir(struct ctl_dir *dir)
  34. {
  35. dir->header.ctl_table[0].child = NULL;
  36. }
  37. void proc_sys_poll_notify(struct ctl_table_poll *poll)
  38. {
  39. if (!poll)
  40. return;
  41. atomic_inc(&poll->event);
  42. wake_up_interruptible(&poll->wait);
  43. }
  44. static struct ctl_table root_table[] = {
  45. {
  46. .procname = "",
  47. .mode = S_IFDIR|S_IRUGO|S_IXUGO,
  48. },
  49. { }
  50. };
  51. static struct ctl_table_root sysctl_table_root = {
  52. .default_set.dir.header = {
  53. {{.count = 1,
  54. .nreg = 1,
  55. .ctl_table = root_table }},
  56. .ctl_table_arg = root_table,
  57. .root = &sysctl_table_root,
  58. .set = &sysctl_table_root.default_set,
  59. },
  60. };
  61. static DEFINE_SPINLOCK(sysctl_lock);
  62. static void drop_sysctl_table(struct ctl_table_header *header);
  63. static int sysctl_follow_link(struct ctl_table_header **phead,
  64. struct ctl_table **pentry);
  65. static int insert_links(struct ctl_table_header *head);
  66. static void put_links(struct ctl_table_header *header);
  67. static void sysctl_print_dir(struct ctl_dir *dir)
  68. {
  69. if (dir->header.parent)
  70. sysctl_print_dir(dir->header.parent);
  71. pr_cont("%s/", dir->header.ctl_table[0].procname);
  72. }
  73. static int namecmp(const char *name1, int len1, const char *name2, int len2)
  74. {
  75. int minlen;
  76. int cmp;
  77. minlen = len1;
  78. if (minlen > len2)
  79. minlen = len2;
  80. cmp = memcmp(name1, name2, minlen);
  81. if (cmp == 0)
  82. cmp = len1 - len2;
  83. return cmp;
  84. }
  85. /* Called under sysctl_lock */
  86. static struct ctl_table *find_entry(struct ctl_table_header **phead,
  87. struct ctl_dir *dir, const char *name, int namelen)
  88. {
  89. struct ctl_table_header *head;
  90. struct ctl_table *entry;
  91. struct rb_node *node = dir->root.rb_node;
  92. while (node)
  93. {
  94. struct ctl_node *ctl_node;
  95. const char *procname;
  96. int cmp;
  97. ctl_node = rb_entry(node, struct ctl_node, node);
  98. head = ctl_node->header;
  99. entry = &head->ctl_table[ctl_node - head->node];
  100. procname = entry->procname;
  101. cmp = namecmp(name, namelen, procname, strlen(procname));
  102. if (cmp < 0)
  103. node = node->rb_left;
  104. else if (cmp > 0)
  105. node = node->rb_right;
  106. else {
  107. *phead = head;
  108. return entry;
  109. }
  110. }
  111. return NULL;
  112. }
  113. static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
  114. {
  115. struct rb_node *node = &head->node[entry - head->ctl_table].node;
  116. struct rb_node **p = &head->parent->root.rb_node;
  117. struct rb_node *parent = NULL;
  118. const char *name = entry->procname;
  119. int namelen = strlen(name);
  120. while (*p) {
  121. struct ctl_table_header *parent_head;
  122. struct ctl_table *parent_entry;
  123. struct ctl_node *parent_node;
  124. const char *parent_name;
  125. int cmp;
  126. parent = *p;
  127. parent_node = rb_entry(parent, struct ctl_node, node);
  128. parent_head = parent_node->header;
  129. parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
  130. parent_name = parent_entry->procname;
  131. cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
  132. if (cmp < 0)
  133. p = &(*p)->rb_left;
  134. else if (cmp > 0)
  135. p = &(*p)->rb_right;
  136. else {
  137. pr_err("sysctl duplicate entry: ");
  138. sysctl_print_dir(head->parent);
  139. pr_cont("/%s\n", entry->procname);
  140. return -EEXIST;
  141. }
  142. }
  143. rb_link_node(node, parent, p);
  144. rb_insert_color(node, &head->parent->root);
  145. return 0;
  146. }
  147. static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
  148. {
  149. struct rb_node *node = &head->node[entry - head->ctl_table].node;
  150. rb_erase(node, &head->parent->root);
  151. }
  152. static void init_header(struct ctl_table_header *head,
  153. struct ctl_table_root *root, struct ctl_table_set *set,
  154. struct ctl_node *node, struct ctl_table *table)
  155. {
  156. head->ctl_table = table;
  157. head->ctl_table_arg = table;
  158. head->used = 0;
  159. head->count = 1;
  160. head->nreg = 1;
  161. head->unregistering = NULL;
  162. head->root = root;
  163. head->set = set;
  164. head->parent = NULL;
  165. head->node = node;
  166. INIT_LIST_HEAD(&head->inodes);
  167. if (node) {
  168. struct ctl_table *entry;
  169. for (entry = table; entry->procname; entry++, node++)
  170. node->header = head;
  171. }
  172. }
  173. static void erase_header(struct ctl_table_header *head)
  174. {
  175. struct ctl_table *entry;
  176. for (entry = head->ctl_table; entry->procname; entry++)
  177. erase_entry(head, entry);
  178. }
  179. static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
  180. {
  181. struct ctl_table *entry;
  182. int err;
  183. /* Is this a permanently empty directory? */
  184. if (is_empty_dir(&dir->header))
  185. return -EROFS;
  186. /* Am I creating a permanently empty directory? */
  187. if (header->ctl_table == sysctl_mount_point) {
  188. if (!RB_EMPTY_ROOT(&dir->root))
  189. return -EINVAL;
  190. set_empty_dir(dir);
  191. }
  192. dir->header.nreg++;
  193. header->parent = dir;
  194. err = insert_links(header);
  195. if (err)
  196. goto fail_links;
  197. for (entry = header->ctl_table; entry->procname; entry++) {
  198. err = insert_entry(header, entry);
  199. if (err)
  200. goto fail;
  201. }
  202. return 0;
  203. fail:
  204. erase_header(header);
  205. put_links(header);
  206. fail_links:
  207. if (header->ctl_table == sysctl_mount_point)
  208. clear_empty_dir(dir);
  209. header->parent = NULL;
  210. drop_sysctl_table(&dir->header);
  211. return err;
  212. }
  213. /* called under sysctl_lock */
  214. static int use_table(struct ctl_table_header *p)
  215. {
  216. if (unlikely(p->unregistering))
  217. return 0;
  218. p->used++;
  219. return 1;
  220. }
  221. /* called under sysctl_lock */
  222. static void unuse_table(struct ctl_table_header *p)
  223. {
  224. if (!--p->used)
  225. if (unlikely(p->unregistering))
  226. complete(p->unregistering);
  227. }
  228. /* called under sysctl_lock */
  229. static void proc_sys_prune_dcache(struct ctl_table_header *head)
  230. {
  231. struct inode *inode, *prev = NULL;
  232. struct proc_inode *ei;
  233. rcu_read_lock();
  234. list_for_each_entry_rcu(ei, &head->inodes, sysctl_inodes) {
  235. inode = igrab(&ei->vfs_inode);
  236. if (inode) {
  237. rcu_read_unlock();
  238. iput(prev);
  239. prev = inode;
  240. d_prune_aliases(inode);
  241. rcu_read_lock();
  242. }
  243. }
  244. rcu_read_unlock();
  245. iput(prev);
  246. }
  247. /* called under sysctl_lock, will reacquire if has to wait */
  248. static void start_unregistering(struct ctl_table_header *p)
  249. {
  250. /*
  251. * if p->used is 0, nobody will ever touch that entry again;
  252. * we'll eliminate all paths to it before dropping sysctl_lock
  253. */
  254. if (unlikely(p->used)) {
  255. struct completion wait;
  256. init_completion(&wait);
  257. p->unregistering = &wait;
  258. spin_unlock(&sysctl_lock);
  259. wait_for_completion(&wait);
  260. } else {
  261. /* anything non-NULL; we'll never dereference it */
  262. p->unregistering = ERR_PTR(-EINVAL);
  263. spin_unlock(&sysctl_lock);
  264. }
  265. /*
  266. * Prune dentries for unregistered sysctls: namespaced sysctls
  267. * can have duplicate names and contaminate dcache very badly.
  268. */
  269. proc_sys_prune_dcache(p);
  270. /*
  271. * do not remove from the list until nobody holds it; walking the
  272. * list in do_sysctl() relies on that.
  273. */
  274. spin_lock(&sysctl_lock);
  275. erase_header(p);
  276. }
  277. static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
  278. {
  279. BUG_ON(!head);
  280. spin_lock(&sysctl_lock);
  281. if (!use_table(head))
  282. head = ERR_PTR(-ENOENT);
  283. spin_unlock(&sysctl_lock);
  284. return head;
  285. }
  286. static void sysctl_head_finish(struct ctl_table_header *head)
  287. {
  288. if (!head)
  289. return;
  290. spin_lock(&sysctl_lock);
  291. unuse_table(head);
  292. spin_unlock(&sysctl_lock);
  293. }
  294. static struct ctl_table_set *
  295. lookup_header_set(struct ctl_table_root *root)
  296. {
  297. struct ctl_table_set *set = &root->default_set;
  298. if (root->lookup)
  299. set = root->lookup(root);
  300. return set;
  301. }
  302. static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
  303. struct ctl_dir *dir,
  304. const char *name, int namelen)
  305. {
  306. struct ctl_table_header *head;
  307. struct ctl_table *entry;
  308. spin_lock(&sysctl_lock);
  309. entry = find_entry(&head, dir, name, namelen);
  310. if (entry && use_table(head))
  311. *phead = head;
  312. else
  313. entry = NULL;
  314. spin_unlock(&sysctl_lock);
  315. return entry;
  316. }
  317. static struct ctl_node *first_usable_entry(struct rb_node *node)
  318. {
  319. struct ctl_node *ctl_node;
  320. for (;node; node = rb_next(node)) {
  321. ctl_node = rb_entry(node, struct ctl_node, node);
  322. if (use_table(ctl_node->header))
  323. return ctl_node;
  324. }
  325. return NULL;
  326. }
  327. static void first_entry(struct ctl_dir *dir,
  328. struct ctl_table_header **phead, struct ctl_table **pentry)
  329. {
  330. struct ctl_table_header *head = NULL;
  331. struct ctl_table *entry = NULL;
  332. struct ctl_node *ctl_node;
  333. spin_lock(&sysctl_lock);
  334. ctl_node = first_usable_entry(rb_first(&dir->root));
  335. spin_unlock(&sysctl_lock);
  336. if (ctl_node) {
  337. head = ctl_node->header;
  338. entry = &head->ctl_table[ctl_node - head->node];
  339. }
  340. *phead = head;
  341. *pentry = entry;
  342. }
  343. static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
  344. {
  345. struct ctl_table_header *head = *phead;
  346. struct ctl_table *entry = *pentry;
  347. struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
  348. spin_lock(&sysctl_lock);
  349. unuse_table(head);
  350. ctl_node = first_usable_entry(rb_next(&ctl_node->node));
  351. spin_unlock(&sysctl_lock);
  352. head = NULL;
  353. if (ctl_node) {
  354. head = ctl_node->header;
  355. entry = &head->ctl_table[ctl_node - head->node];
  356. }
  357. *phead = head;
  358. *pentry = entry;
  359. }
  360. void register_sysctl_root(struct ctl_table_root *root)
  361. {
  362. }
  363. /*
  364. * sysctl_perm does NOT grant the superuser all rights automatically, because
  365. * some sysctl variables are readonly even to root.
  366. */
  367. static int test_perm(int mode, int op)
  368. {
  369. if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
  370. mode >>= 6;
  371. else if (in_egroup_p(GLOBAL_ROOT_GID))
  372. mode >>= 3;
  373. if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
  374. return 0;
  375. return -EACCES;
  376. }
  377. static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
  378. {
  379. struct ctl_table_root *root = head->root;
  380. int mode;
  381. if (root->permissions)
  382. mode = root->permissions(head, table);
  383. else
  384. mode = table->mode;
  385. return test_perm(mode, op);
  386. }
  387. static struct inode *proc_sys_make_inode(struct super_block *sb,
  388. struct ctl_table_header *head, struct ctl_table *table)
  389. {
  390. struct ctl_table_root *root = head->root;
  391. struct inode *inode;
  392. struct proc_inode *ei;
  393. inode = new_inode(sb);
  394. if (!inode)
  395. goto out;
  396. inode->i_ino = get_next_ino();
  397. ei = PROC_I(inode);
  398. spin_lock(&sysctl_lock);
  399. if (unlikely(head->unregistering)) {
  400. spin_unlock(&sysctl_lock);
  401. iput(inode);
  402. inode = NULL;
  403. goto out;
  404. }
  405. ei->sysctl = head;
  406. ei->sysctl_entry = table;
  407. list_add_rcu(&ei->sysctl_inodes, &head->inodes);
  408. head->count++;
  409. spin_unlock(&sysctl_lock);
  410. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  411. inode->i_mode = table->mode;
  412. if (!S_ISDIR(table->mode)) {
  413. inode->i_mode |= S_IFREG;
  414. inode->i_op = &proc_sys_inode_operations;
  415. inode->i_fop = &proc_sys_file_operations;
  416. } else {
  417. inode->i_mode |= S_IFDIR;
  418. inode->i_op = &proc_sys_dir_operations;
  419. inode->i_fop = &proc_sys_dir_file_operations;
  420. if (is_empty_dir(head))
  421. make_empty_dir_inode(inode);
  422. }
  423. if (root->set_ownership)
  424. root->set_ownership(head, table, &inode->i_uid, &inode->i_gid);
  425. out:
  426. return inode;
  427. }
  428. void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
  429. {
  430. spin_lock(&sysctl_lock);
  431. list_del_rcu(&PROC_I(inode)->sysctl_inodes);
  432. if (!--head->count)
  433. kfree_rcu(head, rcu);
  434. spin_unlock(&sysctl_lock);
  435. }
  436. static struct ctl_table_header *grab_header(struct inode *inode)
  437. {
  438. struct ctl_table_header *head = PROC_I(inode)->sysctl;
  439. if (!head)
  440. head = &sysctl_table_root.default_set.dir.header;
  441. return sysctl_head_grab(head);
  442. }
  443. static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
  444. unsigned int flags)
  445. {
  446. struct ctl_table_header *head = grab_header(dir);
  447. struct ctl_table_header *h = NULL;
  448. const struct qstr *name = &dentry->d_name;
  449. struct ctl_table *p;
  450. struct inode *inode;
  451. struct dentry *err = ERR_PTR(-ENOENT);
  452. struct ctl_dir *ctl_dir;
  453. int ret;
  454. if (IS_ERR(head))
  455. return ERR_CAST(head);
  456. ctl_dir = container_of(head, struct ctl_dir, header);
  457. p = lookup_entry(&h, ctl_dir, name->name, name->len);
  458. if (!p)
  459. goto out;
  460. if (S_ISLNK(p->mode)) {
  461. ret = sysctl_follow_link(&h, &p);
  462. err = ERR_PTR(ret);
  463. if (ret)
  464. goto out;
  465. }
  466. err = ERR_PTR(-ENOMEM);
  467. inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
  468. if (!inode)
  469. goto out;
  470. err = NULL;
  471. d_set_d_op(dentry, &proc_sys_dentry_operations);
  472. d_add(dentry, inode);
  473. out:
  474. if (h)
  475. sysctl_head_finish(h);
  476. sysctl_head_finish(head);
  477. return err;
  478. }
  479. static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
  480. size_t count, loff_t *ppos, int write)
  481. {
  482. struct inode *inode = file_inode(filp);
  483. struct ctl_table_header *head = grab_header(inode);
  484. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  485. ssize_t error;
  486. size_t res;
  487. if (IS_ERR(head))
  488. return PTR_ERR(head);
  489. /*
  490. * At this point we know that the sysctl was not unregistered
  491. * and won't be until we finish.
  492. */
  493. error = -EPERM;
  494. if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
  495. goto out;
  496. /* if that can happen at all, it should be -EINVAL, not -EISDIR */
  497. error = -EINVAL;
  498. if (!table->proc_handler)
  499. goto out;
  500. /* careful: calling conventions are nasty here */
  501. res = count;
  502. error = table->proc_handler(table, write, buf, &res, ppos);
  503. if (!error)
  504. error = res;
  505. out:
  506. sysctl_head_finish(head);
  507. return error;
  508. }
  509. static ssize_t proc_sys_read(struct file *filp, char __user *buf,
  510. size_t count, loff_t *ppos)
  511. {
  512. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
  513. }
  514. static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
  515. size_t count, loff_t *ppos)
  516. {
  517. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
  518. }
  519. static int proc_sys_open(struct inode *inode, struct file *filp)
  520. {
  521. struct ctl_table_header *head = grab_header(inode);
  522. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  523. /* sysctl was unregistered */
  524. if (IS_ERR(head))
  525. return PTR_ERR(head);
  526. if (table->poll)
  527. filp->private_data = proc_sys_poll_event(table->poll);
  528. sysctl_head_finish(head);
  529. return 0;
  530. }
  531. static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
  532. {
  533. struct inode *inode = file_inode(filp);
  534. struct ctl_table_header *head = grab_header(inode);
  535. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  536. unsigned int ret = DEFAULT_POLLMASK;
  537. unsigned long event;
  538. /* sysctl was unregistered */
  539. if (IS_ERR(head))
  540. return POLLERR | POLLHUP;
  541. if (!table->proc_handler)
  542. goto out;
  543. if (!table->poll)
  544. goto out;
  545. event = (unsigned long)filp->private_data;
  546. poll_wait(filp, &table->poll->wait, wait);
  547. if (event != atomic_read(&table->poll->event)) {
  548. filp->private_data = proc_sys_poll_event(table->poll);
  549. ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
  550. }
  551. out:
  552. sysctl_head_finish(head);
  553. return ret;
  554. }
  555. static bool proc_sys_fill_cache(struct file *file,
  556. struct dir_context *ctx,
  557. struct ctl_table_header *head,
  558. struct ctl_table *table)
  559. {
  560. struct dentry *child, *dir = file->f_path.dentry;
  561. struct inode *inode;
  562. struct qstr qname;
  563. ino_t ino = 0;
  564. unsigned type = DT_UNKNOWN;
  565. qname.name = table->procname;
  566. qname.len = strlen(table->procname);
  567. qname.hash = full_name_hash(dir, qname.name, qname.len);
  568. child = d_lookup(dir, &qname);
  569. if (!child) {
  570. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  571. child = d_alloc_parallel(dir, &qname, &wq);
  572. if (IS_ERR(child))
  573. return false;
  574. if (d_in_lookup(child)) {
  575. inode = proc_sys_make_inode(dir->d_sb, head, table);
  576. if (!inode) {
  577. d_lookup_done(child);
  578. dput(child);
  579. return false;
  580. }
  581. d_set_d_op(child, &proc_sys_dentry_operations);
  582. d_add(child, inode);
  583. }
  584. }
  585. inode = d_inode(child);
  586. ino = inode->i_ino;
  587. type = inode->i_mode >> 12;
  588. dput(child);
  589. return dir_emit(ctx, qname.name, qname.len, ino, type);
  590. }
  591. static bool proc_sys_link_fill_cache(struct file *file,
  592. struct dir_context *ctx,
  593. struct ctl_table_header *head,
  594. struct ctl_table *table)
  595. {
  596. bool ret = true;
  597. head = sysctl_head_grab(head);
  598. if (S_ISLNK(table->mode)) {
  599. /* It is not an error if we can not follow the link ignore it */
  600. int err = sysctl_follow_link(&head, &table);
  601. if (err)
  602. goto out;
  603. }
  604. ret = proc_sys_fill_cache(file, ctx, head, table);
  605. out:
  606. sysctl_head_finish(head);
  607. return ret;
  608. }
  609. static int scan(struct ctl_table_header *head, struct ctl_table *table,
  610. unsigned long *pos, struct file *file,
  611. struct dir_context *ctx)
  612. {
  613. bool res;
  614. if ((*pos)++ < ctx->pos)
  615. return true;
  616. if (unlikely(S_ISLNK(table->mode)))
  617. res = proc_sys_link_fill_cache(file, ctx, head, table);
  618. else
  619. res = proc_sys_fill_cache(file, ctx, head, table);
  620. if (res)
  621. ctx->pos = *pos;
  622. return res;
  623. }
  624. static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
  625. {
  626. struct ctl_table_header *head = grab_header(file_inode(file));
  627. struct ctl_table_header *h = NULL;
  628. struct ctl_table *entry;
  629. struct ctl_dir *ctl_dir;
  630. unsigned long pos;
  631. if (IS_ERR(head))
  632. return PTR_ERR(head);
  633. ctl_dir = container_of(head, struct ctl_dir, header);
  634. if (!dir_emit_dots(file, ctx))
  635. goto out;
  636. pos = 2;
  637. for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
  638. if (!scan(h, entry, &pos, file, ctx)) {
  639. sysctl_head_finish(h);
  640. break;
  641. }
  642. }
  643. out:
  644. sysctl_head_finish(head);
  645. return 0;
  646. }
  647. static int proc_sys_permission(struct inode *inode, int mask)
  648. {
  649. /*
  650. * sysctl entries that are not writeable,
  651. * are _NOT_ writeable, capabilities or not.
  652. */
  653. struct ctl_table_header *head;
  654. struct ctl_table *table;
  655. int error;
  656. /* Executable files are not allowed under /proc/sys/ */
  657. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
  658. return -EACCES;
  659. head = grab_header(inode);
  660. if (IS_ERR(head))
  661. return PTR_ERR(head);
  662. table = PROC_I(inode)->sysctl_entry;
  663. if (!table) /* global root - r-xr-xr-x */
  664. error = mask & MAY_WRITE ? -EACCES : 0;
  665. else /* Use the permissions on the sysctl table entry */
  666. error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
  667. sysctl_head_finish(head);
  668. return error;
  669. }
  670. static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
  671. {
  672. struct inode *inode = d_inode(dentry);
  673. int error;
  674. if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  675. return -EPERM;
  676. error = setattr_prepare(dentry, attr);
  677. if (error)
  678. return error;
  679. setattr_copy(inode, attr);
  680. mark_inode_dirty(inode);
  681. return 0;
  682. }
  683. static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  684. {
  685. struct inode *inode = d_inode(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. }