proc_sysctl.c 39 KB

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