proc_sysctl.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  683. {
  684. struct inode *inode = d_inode(dentry);
  685. struct ctl_table_header *head = grab_header(inode);
  686. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  687. if (IS_ERR(head))
  688. return PTR_ERR(head);
  689. generic_fillattr(inode, stat);
  690. if (table)
  691. stat->mode = (stat->mode & S_IFMT) | table->mode;
  692. sysctl_head_finish(head);
  693. return 0;
  694. }
  695. static const struct file_operations proc_sys_file_operations = {
  696. .open = proc_sys_open,
  697. .poll = proc_sys_poll,
  698. .read = proc_sys_read,
  699. .write = proc_sys_write,
  700. .llseek = default_llseek,
  701. };
  702. static const struct file_operations proc_sys_dir_file_operations = {
  703. .read = generic_read_dir,
  704. .iterate_shared = proc_sys_readdir,
  705. .llseek = generic_file_llseek,
  706. };
  707. static const struct inode_operations proc_sys_inode_operations = {
  708. .permission = proc_sys_permission,
  709. .setattr = proc_sys_setattr,
  710. .getattr = proc_sys_getattr,
  711. };
  712. static const struct inode_operations proc_sys_dir_operations = {
  713. .lookup = proc_sys_lookup,
  714. .permission = proc_sys_permission,
  715. .setattr = proc_sys_setattr,
  716. .getattr = proc_sys_getattr,
  717. };
  718. static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
  719. {
  720. if (flags & LOOKUP_RCU)
  721. return -ECHILD;
  722. return !PROC_I(d_inode(dentry))->sysctl->unregistering;
  723. }
  724. static int proc_sys_delete(const struct dentry *dentry)
  725. {
  726. return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
  727. }
  728. static int sysctl_is_seen(struct ctl_table_header *p)
  729. {
  730. struct ctl_table_set *set = p->set;
  731. int res;
  732. spin_lock(&sysctl_lock);
  733. if (p->unregistering)
  734. res = 0;
  735. else if (!set->is_seen)
  736. res = 1;
  737. else
  738. res = set->is_seen(set);
  739. spin_unlock(&sysctl_lock);
  740. return res;
  741. }
  742. static int proc_sys_compare(const struct dentry *dentry,
  743. unsigned int len, const char *str, const struct qstr *name)
  744. {
  745. struct ctl_table_header *head;
  746. struct inode *inode;
  747. /* Although proc doesn't have negative dentries, rcu-walk means
  748. * that inode here can be NULL */
  749. /* AV: can it, indeed? */
  750. inode = d_inode_rcu(dentry);
  751. if (!inode)
  752. return 1;
  753. if (name->len != len)
  754. return 1;
  755. if (memcmp(name->name, str, len))
  756. return 1;
  757. head = rcu_dereference(PROC_I(inode)->sysctl);
  758. return !head || !sysctl_is_seen(head);
  759. }
  760. static const struct dentry_operations proc_sys_dentry_operations = {
  761. .d_revalidate = proc_sys_revalidate,
  762. .d_delete = proc_sys_delete,
  763. .d_compare = proc_sys_compare,
  764. };
  765. static struct ctl_dir *find_subdir(struct ctl_dir *dir,
  766. const char *name, int namelen)
  767. {
  768. struct ctl_table_header *head;
  769. struct ctl_table *entry;
  770. entry = find_entry(&head, dir, name, namelen);
  771. if (!entry)
  772. return ERR_PTR(-ENOENT);
  773. if (!S_ISDIR(entry->mode))
  774. return ERR_PTR(-ENOTDIR);
  775. return container_of(head, struct ctl_dir, header);
  776. }
  777. static struct ctl_dir *new_dir(struct ctl_table_set *set,
  778. const char *name, int namelen)
  779. {
  780. struct ctl_table *table;
  781. struct ctl_dir *new;
  782. struct ctl_node *node;
  783. char *new_name;
  784. new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
  785. sizeof(struct ctl_table)*2 + namelen + 1,
  786. GFP_KERNEL);
  787. if (!new)
  788. return NULL;
  789. node = (struct ctl_node *)(new + 1);
  790. table = (struct ctl_table *)(node + 1);
  791. new_name = (char *)(table + 2);
  792. memcpy(new_name, name, namelen);
  793. new_name[namelen] = '\0';
  794. table[0].procname = new_name;
  795. table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
  796. init_header(&new->header, set->dir.header.root, set, node, table);
  797. return new;
  798. }
  799. /**
  800. * get_subdir - find or create a subdir with the specified name.
  801. * @dir: Directory to create the subdirectory in
  802. * @name: The name of the subdirectory to find or create
  803. * @namelen: The length of name
  804. *
  805. * Takes a directory with an elevated reference count so we know that
  806. * if we drop the lock the directory will not go away. Upon success
  807. * the reference is moved from @dir to the returned subdirectory.
  808. * Upon error an error code is returned and the reference on @dir is
  809. * simply dropped.
  810. */
  811. static struct ctl_dir *get_subdir(struct ctl_dir *dir,
  812. const char *name, int namelen)
  813. {
  814. struct ctl_table_set *set = dir->header.set;
  815. struct ctl_dir *subdir, *new = NULL;
  816. int err;
  817. spin_lock(&sysctl_lock);
  818. subdir = find_subdir(dir, name, namelen);
  819. if (!IS_ERR(subdir))
  820. goto found;
  821. if (PTR_ERR(subdir) != -ENOENT)
  822. goto failed;
  823. spin_unlock(&sysctl_lock);
  824. new = new_dir(set, name, namelen);
  825. spin_lock(&sysctl_lock);
  826. subdir = ERR_PTR(-ENOMEM);
  827. if (!new)
  828. goto failed;
  829. /* Was the subdir added while we dropped the lock? */
  830. subdir = find_subdir(dir, name, namelen);
  831. if (!IS_ERR(subdir))
  832. goto found;
  833. if (PTR_ERR(subdir) != -ENOENT)
  834. goto failed;
  835. /* Nope. Use the our freshly made directory entry. */
  836. err = insert_header(dir, &new->header);
  837. subdir = ERR_PTR(err);
  838. if (err)
  839. goto failed;
  840. subdir = new;
  841. found:
  842. subdir->header.nreg++;
  843. failed:
  844. if (IS_ERR(subdir)) {
  845. pr_err("sysctl could not get directory: ");
  846. sysctl_print_dir(dir);
  847. pr_cont("/%*.*s %ld\n",
  848. namelen, namelen, name, PTR_ERR(subdir));
  849. }
  850. drop_sysctl_table(&dir->header);
  851. if (new)
  852. drop_sysctl_table(&new->header);
  853. spin_unlock(&sysctl_lock);
  854. return subdir;
  855. }
  856. static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
  857. {
  858. struct ctl_dir *parent;
  859. const char *procname;
  860. if (!dir->header.parent)
  861. return &set->dir;
  862. parent = xlate_dir(set, dir->header.parent);
  863. if (IS_ERR(parent))
  864. return parent;
  865. procname = dir->header.ctl_table[0].procname;
  866. return find_subdir(parent, procname, strlen(procname));
  867. }
  868. static int sysctl_follow_link(struct ctl_table_header **phead,
  869. struct ctl_table **pentry)
  870. {
  871. struct ctl_table_header *head;
  872. struct ctl_table_root *root;
  873. struct ctl_table_set *set;
  874. struct ctl_table *entry;
  875. struct ctl_dir *dir;
  876. int ret;
  877. ret = 0;
  878. spin_lock(&sysctl_lock);
  879. root = (*pentry)->data;
  880. set = lookup_header_set(root);
  881. dir = xlate_dir(set, (*phead)->parent);
  882. if (IS_ERR(dir))
  883. ret = PTR_ERR(dir);
  884. else {
  885. const char *procname = (*pentry)->procname;
  886. head = NULL;
  887. entry = find_entry(&head, dir, procname, strlen(procname));
  888. ret = -ENOENT;
  889. if (entry && use_table(head)) {
  890. unuse_table(*phead);
  891. *phead = head;
  892. *pentry = entry;
  893. ret = 0;
  894. }
  895. }
  896. spin_unlock(&sysctl_lock);
  897. return ret;
  898. }
  899. static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
  900. {
  901. struct va_format vaf;
  902. va_list args;
  903. va_start(args, fmt);
  904. vaf.fmt = fmt;
  905. vaf.va = &args;
  906. pr_err("sysctl table check failed: %s/%s %pV\n",
  907. path, table->procname, &vaf);
  908. va_end(args);
  909. return -EINVAL;
  910. }
  911. static int sysctl_check_table(const char *path, struct ctl_table *table)
  912. {
  913. int err = 0;
  914. for (; table->procname; table++) {
  915. if (table->child)
  916. err = sysctl_err(path, table, "Not a file");
  917. if ((table->proc_handler == proc_dostring) ||
  918. (table->proc_handler == proc_dointvec) ||
  919. (table->proc_handler == proc_dointvec_minmax) ||
  920. (table->proc_handler == proc_dointvec_jiffies) ||
  921. (table->proc_handler == proc_dointvec_userhz_jiffies) ||
  922. (table->proc_handler == proc_dointvec_ms_jiffies) ||
  923. (table->proc_handler == proc_doulongvec_minmax) ||
  924. (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
  925. if (!table->data)
  926. err = sysctl_err(path, table, "No data");
  927. if (!table->maxlen)
  928. err = sysctl_err(path, table, "No maxlen");
  929. }
  930. if (!table->proc_handler)
  931. err = sysctl_err(path, table, "No proc_handler");
  932. if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
  933. err = sysctl_err(path, table, "bogus .mode 0%o",
  934. table->mode);
  935. }
  936. return err;
  937. }
  938. static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
  939. struct ctl_table_root *link_root)
  940. {
  941. struct ctl_table *link_table, *entry, *link;
  942. struct ctl_table_header *links;
  943. struct ctl_node *node;
  944. char *link_name;
  945. int nr_entries, name_bytes;
  946. name_bytes = 0;
  947. nr_entries = 0;
  948. for (entry = table; entry->procname; entry++) {
  949. nr_entries++;
  950. name_bytes += strlen(entry->procname) + 1;
  951. }
  952. links = kzalloc(sizeof(struct ctl_table_header) +
  953. sizeof(struct ctl_node)*nr_entries +
  954. sizeof(struct ctl_table)*(nr_entries + 1) +
  955. name_bytes,
  956. GFP_KERNEL);
  957. if (!links)
  958. return NULL;
  959. node = (struct ctl_node *)(links + 1);
  960. link_table = (struct ctl_table *)(node + nr_entries);
  961. link_name = (char *)&link_table[nr_entries + 1];
  962. for (link = link_table, entry = table; entry->procname; link++, entry++) {
  963. int len = strlen(entry->procname) + 1;
  964. memcpy(link_name, entry->procname, len);
  965. link->procname = link_name;
  966. link->mode = S_IFLNK|S_IRWXUGO;
  967. link->data = link_root;
  968. link_name += len;
  969. }
  970. init_header(links, dir->header.root, dir->header.set, node, link_table);
  971. links->nreg = nr_entries;
  972. return links;
  973. }
  974. static bool get_links(struct ctl_dir *dir,
  975. struct ctl_table *table, struct ctl_table_root *link_root)
  976. {
  977. struct ctl_table_header *head;
  978. struct ctl_table *entry, *link;
  979. /* Are there links available for every entry in table? */
  980. for (entry = table; entry->procname; entry++) {
  981. const char *procname = entry->procname;
  982. link = find_entry(&head, dir, procname, strlen(procname));
  983. if (!link)
  984. return false;
  985. if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
  986. continue;
  987. if (S_ISLNK(link->mode) && (link->data == link_root))
  988. continue;
  989. return false;
  990. }
  991. /* The checks passed. Increase the registration count on the links */
  992. for (entry = table; entry->procname; entry++) {
  993. const char *procname = entry->procname;
  994. link = find_entry(&head, dir, procname, strlen(procname));
  995. head->nreg++;
  996. }
  997. return true;
  998. }
  999. static int insert_links(struct ctl_table_header *head)
  1000. {
  1001. struct ctl_table_set *root_set = &sysctl_table_root.default_set;
  1002. struct ctl_dir *core_parent = NULL;
  1003. struct ctl_table_header *links;
  1004. int err;
  1005. if (head->set == root_set)
  1006. return 0;
  1007. core_parent = xlate_dir(root_set, head->parent);
  1008. if (IS_ERR(core_parent))
  1009. return 0;
  1010. if (get_links(core_parent, head->ctl_table, head->root))
  1011. return 0;
  1012. core_parent->header.nreg++;
  1013. spin_unlock(&sysctl_lock);
  1014. links = new_links(core_parent, head->ctl_table, head->root);
  1015. spin_lock(&sysctl_lock);
  1016. err = -ENOMEM;
  1017. if (!links)
  1018. goto out;
  1019. err = 0;
  1020. if (get_links(core_parent, head->ctl_table, head->root)) {
  1021. kfree(links);
  1022. goto out;
  1023. }
  1024. err = insert_header(core_parent, links);
  1025. if (err)
  1026. kfree(links);
  1027. out:
  1028. drop_sysctl_table(&core_parent->header);
  1029. return err;
  1030. }
  1031. /**
  1032. * __register_sysctl_table - register a leaf sysctl table
  1033. * @set: Sysctl tree to register on
  1034. * @path: The path to the directory the sysctl table is in.
  1035. * @table: the top-level table structure
  1036. *
  1037. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1038. * array. A completely 0 filled entry terminates the table.
  1039. *
  1040. * The members of the &struct ctl_table structure are used as follows:
  1041. *
  1042. * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
  1043. * enter a sysctl file
  1044. *
  1045. * data - a pointer to data for use by proc_handler
  1046. *
  1047. * maxlen - the maximum size in bytes of the data
  1048. *
  1049. * mode - the file permissions for the /proc/sys file
  1050. *
  1051. * child - must be %NULL.
  1052. *
  1053. * proc_handler - the text handler routine (described below)
  1054. *
  1055. * extra1, extra2 - extra pointers usable by the proc handler routines
  1056. *
  1057. * Leaf nodes in the sysctl tree will be represented by a single file
  1058. * under /proc; non-leaf nodes will be represented by directories.
  1059. *
  1060. * There must be a proc_handler routine for any terminal nodes.
  1061. * Several default handlers are available to cover common cases -
  1062. *
  1063. * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
  1064. * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
  1065. * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
  1066. *
  1067. * It is the handler's job to read the input buffer from user memory
  1068. * and process it. The handler should return 0 on success.
  1069. *
  1070. * This routine returns %NULL on a failure to register, and a pointer
  1071. * to the table header on success.
  1072. */
  1073. struct ctl_table_header *__register_sysctl_table(
  1074. struct ctl_table_set *set,
  1075. const char *path, struct ctl_table *table)
  1076. {
  1077. struct ctl_table_root *root = set->dir.header.root;
  1078. struct ctl_table_header *header;
  1079. const char *name, *nextname;
  1080. struct ctl_dir *dir;
  1081. struct ctl_table *entry;
  1082. struct ctl_node *node;
  1083. int nr_entries = 0;
  1084. for (entry = table; entry->procname; entry++)
  1085. nr_entries++;
  1086. header = kzalloc(sizeof(struct ctl_table_header) +
  1087. sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
  1088. if (!header)
  1089. return NULL;
  1090. node = (struct ctl_node *)(header + 1);
  1091. init_header(header, root, set, node, table);
  1092. if (sysctl_check_table(path, table))
  1093. goto fail;
  1094. spin_lock(&sysctl_lock);
  1095. dir = &set->dir;
  1096. /* Reference moved down the diretory tree get_subdir */
  1097. dir->header.nreg++;
  1098. spin_unlock(&sysctl_lock);
  1099. /* Find the directory for the ctl_table */
  1100. for (name = path; name; name = nextname) {
  1101. int namelen;
  1102. nextname = strchr(name, '/');
  1103. if (nextname) {
  1104. namelen = nextname - name;
  1105. nextname++;
  1106. } else {
  1107. namelen = strlen(name);
  1108. }
  1109. if (namelen == 0)
  1110. continue;
  1111. dir = get_subdir(dir, name, namelen);
  1112. if (IS_ERR(dir))
  1113. goto fail;
  1114. }
  1115. spin_lock(&sysctl_lock);
  1116. if (insert_header(dir, header))
  1117. goto fail_put_dir_locked;
  1118. drop_sysctl_table(&dir->header);
  1119. spin_unlock(&sysctl_lock);
  1120. return header;
  1121. fail_put_dir_locked:
  1122. drop_sysctl_table(&dir->header);
  1123. spin_unlock(&sysctl_lock);
  1124. fail:
  1125. kfree(header);
  1126. dump_stack();
  1127. return NULL;
  1128. }
  1129. /**
  1130. * register_sysctl - register a sysctl table
  1131. * @path: The path to the directory the sysctl table is in.
  1132. * @table: the table structure
  1133. *
  1134. * Register a sysctl table. @table should be a filled in ctl_table
  1135. * array. A completely 0 filled entry terminates the table.
  1136. *
  1137. * See __register_sysctl_table for more details.
  1138. */
  1139. struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
  1140. {
  1141. return __register_sysctl_table(&sysctl_table_root.default_set,
  1142. path, table);
  1143. }
  1144. EXPORT_SYMBOL(register_sysctl);
  1145. static char *append_path(const char *path, char *pos, const char *name)
  1146. {
  1147. int namelen;
  1148. namelen = strlen(name);
  1149. if (((pos - path) + namelen + 2) >= PATH_MAX)
  1150. return NULL;
  1151. memcpy(pos, name, namelen);
  1152. pos[namelen] = '/';
  1153. pos[namelen + 1] = '\0';
  1154. pos += namelen + 1;
  1155. return pos;
  1156. }
  1157. static int count_subheaders(struct ctl_table *table)
  1158. {
  1159. int has_files = 0;
  1160. int nr_subheaders = 0;
  1161. struct ctl_table *entry;
  1162. /* special case: no directory and empty directory */
  1163. if (!table || !table->procname)
  1164. return 1;
  1165. for (entry = table; entry->procname; entry++) {
  1166. if (entry->child)
  1167. nr_subheaders += count_subheaders(entry->child);
  1168. else
  1169. has_files = 1;
  1170. }
  1171. return nr_subheaders + has_files;
  1172. }
  1173. static int register_leaf_sysctl_tables(const char *path, char *pos,
  1174. struct ctl_table_header ***subheader, struct ctl_table_set *set,
  1175. struct ctl_table *table)
  1176. {
  1177. struct ctl_table *ctl_table_arg = NULL;
  1178. struct ctl_table *entry, *files;
  1179. int nr_files = 0;
  1180. int nr_dirs = 0;
  1181. int err = -ENOMEM;
  1182. for (entry = table; entry->procname; entry++) {
  1183. if (entry->child)
  1184. nr_dirs++;
  1185. else
  1186. nr_files++;
  1187. }
  1188. files = table;
  1189. /* If there are mixed files and directories we need a new table */
  1190. if (nr_dirs && nr_files) {
  1191. struct ctl_table *new;
  1192. files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
  1193. GFP_KERNEL);
  1194. if (!files)
  1195. goto out;
  1196. ctl_table_arg = files;
  1197. for (new = files, entry = table; entry->procname; entry++) {
  1198. if (entry->child)
  1199. continue;
  1200. *new = *entry;
  1201. new++;
  1202. }
  1203. }
  1204. /* Register everything except a directory full of subdirectories */
  1205. if (nr_files || !nr_dirs) {
  1206. struct ctl_table_header *header;
  1207. header = __register_sysctl_table(set, path, files);
  1208. if (!header) {
  1209. kfree(ctl_table_arg);
  1210. goto out;
  1211. }
  1212. /* Remember if we need to free the file table */
  1213. header->ctl_table_arg = ctl_table_arg;
  1214. **subheader = header;
  1215. (*subheader)++;
  1216. }
  1217. /* Recurse into the subdirectories. */
  1218. for (entry = table; entry->procname; entry++) {
  1219. char *child_pos;
  1220. if (!entry->child)
  1221. continue;
  1222. err = -ENAMETOOLONG;
  1223. child_pos = append_path(path, pos, entry->procname);
  1224. if (!child_pos)
  1225. goto out;
  1226. err = register_leaf_sysctl_tables(path, child_pos, subheader,
  1227. set, entry->child);
  1228. pos[0] = '\0';
  1229. if (err)
  1230. goto out;
  1231. }
  1232. err = 0;
  1233. out:
  1234. /* On failure our caller will unregister all registered subheaders */
  1235. return err;
  1236. }
  1237. /**
  1238. * __register_sysctl_paths - register a sysctl table hierarchy
  1239. * @set: Sysctl tree to register on
  1240. * @path: The path to the directory the sysctl table is in.
  1241. * @table: the top-level table structure
  1242. *
  1243. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1244. * array. A completely 0 filled entry terminates the table.
  1245. *
  1246. * See __register_sysctl_table for more details.
  1247. */
  1248. struct ctl_table_header *__register_sysctl_paths(
  1249. struct ctl_table_set *set,
  1250. const struct ctl_path *path, struct ctl_table *table)
  1251. {
  1252. struct ctl_table *ctl_table_arg = table;
  1253. int nr_subheaders = count_subheaders(table);
  1254. struct ctl_table_header *header = NULL, **subheaders, **subheader;
  1255. const struct ctl_path *component;
  1256. char *new_path, *pos;
  1257. pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
  1258. if (!new_path)
  1259. return NULL;
  1260. pos[0] = '\0';
  1261. for (component = path; component->procname; component++) {
  1262. pos = append_path(new_path, pos, component->procname);
  1263. if (!pos)
  1264. goto out;
  1265. }
  1266. while (table->procname && table->child && !table[1].procname) {
  1267. pos = append_path(new_path, pos, table->procname);
  1268. if (!pos)
  1269. goto out;
  1270. table = table->child;
  1271. }
  1272. if (nr_subheaders == 1) {
  1273. header = __register_sysctl_table(set, new_path, table);
  1274. if (header)
  1275. header->ctl_table_arg = ctl_table_arg;
  1276. } else {
  1277. header = kzalloc(sizeof(*header) +
  1278. sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
  1279. if (!header)
  1280. goto out;
  1281. subheaders = (struct ctl_table_header **) (header + 1);
  1282. subheader = subheaders;
  1283. header->ctl_table_arg = ctl_table_arg;
  1284. if (register_leaf_sysctl_tables(new_path, pos, &subheader,
  1285. set, table))
  1286. goto err_register_leaves;
  1287. }
  1288. out:
  1289. kfree(new_path);
  1290. return header;
  1291. err_register_leaves:
  1292. while (subheader > subheaders) {
  1293. struct ctl_table_header *subh = *(--subheader);
  1294. struct ctl_table *table = subh->ctl_table_arg;
  1295. unregister_sysctl_table(subh);
  1296. kfree(table);
  1297. }
  1298. kfree(header);
  1299. header = NULL;
  1300. goto out;
  1301. }
  1302. /**
  1303. * register_sysctl_table_path - register a sysctl table hierarchy
  1304. * @path: The path to the directory the sysctl table is in.
  1305. * @table: the top-level table structure
  1306. *
  1307. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1308. * array. A completely 0 filled entry terminates the table.
  1309. *
  1310. * See __register_sysctl_paths for more details.
  1311. */
  1312. struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
  1313. struct ctl_table *table)
  1314. {
  1315. return __register_sysctl_paths(&sysctl_table_root.default_set,
  1316. path, table);
  1317. }
  1318. EXPORT_SYMBOL(register_sysctl_paths);
  1319. /**
  1320. * register_sysctl_table - register a sysctl table hierarchy
  1321. * @table: the top-level table structure
  1322. *
  1323. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1324. * array. A completely 0 filled entry terminates the table.
  1325. *
  1326. * See register_sysctl_paths for more details.
  1327. */
  1328. struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
  1329. {
  1330. static const struct ctl_path null_path[] = { {} };
  1331. return register_sysctl_paths(null_path, table);
  1332. }
  1333. EXPORT_SYMBOL(register_sysctl_table);
  1334. static void put_links(struct ctl_table_header *header)
  1335. {
  1336. struct ctl_table_set *root_set = &sysctl_table_root.default_set;
  1337. struct ctl_table_root *root = header->root;
  1338. struct ctl_dir *parent = header->parent;
  1339. struct ctl_dir *core_parent;
  1340. struct ctl_table *entry;
  1341. if (header->set == root_set)
  1342. return;
  1343. core_parent = xlate_dir(root_set, parent);
  1344. if (IS_ERR(core_parent))
  1345. return;
  1346. for (entry = header->ctl_table; entry->procname; entry++) {
  1347. struct ctl_table_header *link_head;
  1348. struct ctl_table *link;
  1349. const char *name = entry->procname;
  1350. link = find_entry(&link_head, core_parent, name, strlen(name));
  1351. if (link &&
  1352. ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
  1353. (S_ISLNK(link->mode) && (link->data == root)))) {
  1354. drop_sysctl_table(link_head);
  1355. }
  1356. else {
  1357. pr_err("sysctl link missing during unregister: ");
  1358. sysctl_print_dir(parent);
  1359. pr_cont("/%s\n", name);
  1360. }
  1361. }
  1362. }
  1363. static void drop_sysctl_table(struct ctl_table_header *header)
  1364. {
  1365. struct ctl_dir *parent = header->parent;
  1366. if (--header->nreg)
  1367. return;
  1368. put_links(header);
  1369. start_unregistering(header);
  1370. if (!--header->count)
  1371. kfree_rcu(header, rcu);
  1372. if (parent)
  1373. drop_sysctl_table(&parent->header);
  1374. }
  1375. /**
  1376. * unregister_sysctl_table - unregister a sysctl table hierarchy
  1377. * @header: the header returned from register_sysctl_table
  1378. *
  1379. * Unregisters the sysctl table and all children. proc entries may not
  1380. * actually be removed until they are no longer used by anyone.
  1381. */
  1382. void unregister_sysctl_table(struct ctl_table_header * header)
  1383. {
  1384. int nr_subheaders;
  1385. might_sleep();
  1386. if (header == NULL)
  1387. return;
  1388. nr_subheaders = count_subheaders(header->ctl_table_arg);
  1389. if (unlikely(nr_subheaders > 1)) {
  1390. struct ctl_table_header **subheaders;
  1391. int i;
  1392. subheaders = (struct ctl_table_header **)(header + 1);
  1393. for (i = nr_subheaders -1; i >= 0; i--) {
  1394. struct ctl_table_header *subh = subheaders[i];
  1395. struct ctl_table *table = subh->ctl_table_arg;
  1396. unregister_sysctl_table(subh);
  1397. kfree(table);
  1398. }
  1399. kfree(header);
  1400. return;
  1401. }
  1402. spin_lock(&sysctl_lock);
  1403. drop_sysctl_table(header);
  1404. spin_unlock(&sysctl_lock);
  1405. }
  1406. EXPORT_SYMBOL(unregister_sysctl_table);
  1407. void setup_sysctl_set(struct ctl_table_set *set,
  1408. struct ctl_table_root *root,
  1409. int (*is_seen)(struct ctl_table_set *))
  1410. {
  1411. memset(set, 0, sizeof(*set));
  1412. set->is_seen = is_seen;
  1413. init_header(&set->dir.header, root, set, NULL, root_table);
  1414. }
  1415. void retire_sysctl_set(struct ctl_table_set *set)
  1416. {
  1417. WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
  1418. }
  1419. int __init proc_sys_init(void)
  1420. {
  1421. struct proc_dir_entry *proc_sys_root;
  1422. proc_sys_root = proc_mkdir("sys", NULL);
  1423. proc_sys_root->proc_iops = &proc_sys_dir_operations;
  1424. proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
  1425. proc_sys_root->nlink = 0;
  1426. return sysctl_init();
  1427. }