proc_sysctl.c 39 KB

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