root.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * linux/fs/proc/root.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * proc root directory handling functions
  7. */
  8. #include <asm/uaccess.h>
  9. #include <linux/errno.h>
  10. #include <linux/time.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/stat.h>
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include <linux/module.h>
  16. #include <linux/bitops.h>
  17. #include <linux/mount.h>
  18. #include <linux/pid_namespace.h>
  19. #include <linux/parser.h>
  20. #include "internal.h"
  21. static int proc_test_super(struct super_block *sb, void *data)
  22. {
  23. return sb->s_fs_info == data;
  24. }
  25. static int proc_set_super(struct super_block *sb, void *data)
  26. {
  27. int err = set_anon_super(sb, NULL);
  28. if (!err) {
  29. struct pid_namespace *ns = (struct pid_namespace *)data;
  30. sb->s_fs_info = get_pid_ns(ns);
  31. }
  32. return err;
  33. }
  34. enum {
  35. Opt_gid, Opt_hidepid, Opt_err,
  36. };
  37. static const match_table_t tokens = {
  38. {Opt_hidepid, "hidepid=%u"},
  39. {Opt_gid, "gid=%u"},
  40. {Opt_err, NULL},
  41. };
  42. static int proc_parse_options(char *options, struct pid_namespace *pid)
  43. {
  44. char *p;
  45. substring_t args[MAX_OPT_ARGS];
  46. int option;
  47. if (!options)
  48. return 1;
  49. while ((p = strsep(&options, ",")) != NULL) {
  50. int token;
  51. if (!*p)
  52. continue;
  53. args[0].to = args[0].from = 0;
  54. token = match_token(p, tokens, args);
  55. switch (token) {
  56. case Opt_gid:
  57. if (match_int(&args[0], &option))
  58. return 0;
  59. pid->pid_gid = option;
  60. break;
  61. case Opt_hidepid:
  62. if (match_int(&args[0], &option))
  63. return 0;
  64. if (option < 0 || option > 2) {
  65. pr_err("proc: hidepid value must be between 0 and 2.\n");
  66. return 0;
  67. }
  68. pid->hide_pid = option;
  69. break;
  70. default:
  71. pr_err("proc: unrecognized mount option \"%s\" "
  72. "or missing value\n", p);
  73. return 0;
  74. }
  75. }
  76. return 1;
  77. }
  78. int proc_remount(struct super_block *sb, int *flags, char *data)
  79. {
  80. struct pid_namespace *pid = sb->s_fs_info;
  81. sync_filesystem(sb);
  82. return !proc_parse_options(data, pid);
  83. }
  84. static struct dentry *proc_mount(struct file_system_type *fs_type,
  85. int flags, const char *dev_name, void *data)
  86. {
  87. int err;
  88. struct super_block *sb;
  89. struct pid_namespace *ns;
  90. struct proc_inode *ei;
  91. char *options;
  92. if (flags & MS_KERNMOUNT) {
  93. ns = (struct pid_namespace *)data;
  94. options = NULL;
  95. } else {
  96. ns = current->nsproxy->pid_ns;
  97. options = data;
  98. }
  99. sb = sget(fs_type, proc_test_super, proc_set_super, ns);
  100. if (IS_ERR(sb))
  101. return ERR_CAST(sb);
  102. if (!proc_parse_options(options, ns)) {
  103. deactivate_locked_super(sb);
  104. return ERR_PTR(-EINVAL);
  105. }
  106. if (!sb->s_root) {
  107. sb->s_flags = flags;
  108. err = proc_fill_super(sb);
  109. if (err) {
  110. deactivate_locked_super(sb);
  111. return ERR_PTR(err);
  112. }
  113. sb->s_flags |= MS_ACTIVE;
  114. }
  115. ei = PROC_I(sb->s_root->d_inode);
  116. if (!ei->pid) {
  117. rcu_read_lock();
  118. ei->pid = get_pid(find_pid_ns(1, ns));
  119. rcu_read_unlock();
  120. }
  121. return dget(sb->s_root);
  122. }
  123. static void proc_kill_sb(struct super_block *sb)
  124. {
  125. struct pid_namespace *ns;
  126. ns = (struct pid_namespace *)sb->s_fs_info;
  127. kill_anon_super(sb);
  128. put_pid_ns(ns);
  129. }
  130. static struct file_system_type proc_fs_type = {
  131. .name = "proc",
  132. .mount = proc_mount,
  133. .kill_sb = proc_kill_sb,
  134. };
  135. #ifdef CONFIG_DEFERRED_INITCALLS
  136. extern void do_deferred_initcalls(void);
  137. static ssize_t deferred_initcalls_read_proc(struct file *file, char __user *buf,
  138. size_t nbytes, loff_t *ppos)
  139. {
  140. static int deferred_initcalls_done = 0;
  141. int len, ret;
  142. char tmp[3] = "1\n";
  143. if (*ppos >= 3)
  144. return 0;
  145. if ((! deferred_initcalls_done) && ! (*ppos)) {
  146. tmp[0] = '0';
  147. do_deferred_initcalls();
  148. deferred_initcalls_done = 1;
  149. }
  150. len = min(nbytes, (size_t)3);
  151. ret = copy_to_user(buf, tmp, len);
  152. if (ret)
  153. return -EFAULT;
  154. *ppos += len;
  155. return len;
  156. }
  157. static const struct file_operations deferred_initcalls_fops = {
  158. .read = deferred_initcalls_read_proc,
  159. };
  160. #endif
  161. void __init proc_root_init(void)
  162. {
  163. int err;
  164. proc_init_inodecache();
  165. err = register_filesystem(&proc_fs_type);
  166. if (err)
  167. return;
  168. err = pid_ns_prepare_proc(&init_pid_ns);
  169. if (err) {
  170. unregister_filesystem(&proc_fs_type);
  171. return;
  172. }
  173. #ifdef CONFIG_DEFERRED_INITCALLS
  174. proc_create("deferred_initcalls", 0, NULL, &deferred_initcalls_fops);
  175. #endif
  176. proc_symlink("mounts", NULL, "self/mounts");
  177. proc_net_init();
  178. #ifdef CONFIG_SYSVIPC
  179. proc_mkdir("sysvipc", NULL);
  180. #endif
  181. proc_mkdir("fs", NULL);
  182. proc_mkdir("driver", NULL);
  183. proc_mkdir("fs/nfsd", NULL); /* somewhere for the nfsd filesystem to be mounted */
  184. #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
  185. /* just give it a mountpoint */
  186. proc_mkdir("openprom", NULL);
  187. #endif
  188. proc_tty_init();
  189. #ifdef CONFIG_PROC_DEVICETREE
  190. proc_device_tree_init();
  191. #endif
  192. proc_mkdir("bus", NULL);
  193. proc_sys_init();
  194. }
  195. static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
  196. )
  197. {
  198. generic_fillattr(dentry->d_inode, stat);
  199. stat->nlink = proc_root.nlink + nr_processes();
  200. return 0;
  201. }
  202. static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  203. {
  204. if (!proc_lookup(dir, dentry, nd)) {
  205. return NULL;
  206. }
  207. return proc_pid_lookup(dir, dentry, nd);
  208. }
  209. static int proc_root_readdir(struct file * filp,
  210. void * dirent, filldir_t filldir)
  211. {
  212. unsigned int nr = filp->f_pos;
  213. int ret;
  214. if (nr < FIRST_PROCESS_ENTRY) {
  215. int error = proc_readdir(filp, dirent, filldir);
  216. if (error <= 0)
  217. return error;
  218. filp->f_pos = FIRST_PROCESS_ENTRY;
  219. }
  220. ret = proc_pid_readdir(filp, dirent, filldir);
  221. return ret;
  222. }
  223. /*
  224. * The root /proc directory is special, as it has the
  225. * <pid> directories. Thus we don't use the generic
  226. * directory handling functions for that..
  227. */
  228. static const struct file_operations proc_root_operations = {
  229. .read = generic_read_dir,
  230. .readdir = proc_root_readdir,
  231. .llseek = default_llseek,
  232. };
  233. /*
  234. * proc root can do almost nothing..
  235. */
  236. static const struct inode_operations proc_root_inode_operations = {
  237. .lookup = proc_root_lookup,
  238. .getattr = proc_root_getattr,
  239. };
  240. /*
  241. * This is the root "inode" in the /proc tree..
  242. */
  243. struct proc_dir_entry proc_root = {
  244. .low_ino = PROC_ROOT_INO,
  245. .namelen = 5,
  246. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  247. .nlink = 2,
  248. .count = ATOMIC_INIT(1),
  249. .proc_iops = &proc_root_inode_operations,
  250. .proc_fops = &proc_root_operations,
  251. .parent = &proc_root,
  252. .name = "/proc",
  253. };
  254. int pid_ns_prepare_proc(struct pid_namespace *ns)
  255. {
  256. struct vfsmount *mnt;
  257. mnt = kern_mount_data(&proc_fs_type, ns);
  258. if (IS_ERR(mnt))
  259. return PTR_ERR(mnt);
  260. ns->proc_mnt = mnt;
  261. return 0;
  262. }
  263. void pid_ns_release_proc(struct pid_namespace *ns)
  264. {
  265. kern_unmount(ns->proc_mnt);
  266. }