base.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565
  1. /*
  2. * linux/fs/proc/base.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * proc base directory handling functions
  7. *
  8. * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
  9. * Instead of using magical inumbers to determine the kind of object
  10. * we allocate and fill in-core inodes upon lookup. They don't even
  11. * go into icache. We cache the reference to task_struct upon lookup too.
  12. * Eventually it should become a filesystem in its own. We don't use the
  13. * rest of procfs anymore.
  14. *
  15. *
  16. * Changelog:
  17. * 17-Jan-2005
  18. * Allan Bezerra
  19. * Bruna Moreira <bruna.moreira@indt.org.br>
  20. * Edjard Mota <edjard.mota@indt.org.br>
  21. * Ilias Biris <ilias.biris@indt.org.br>
  22. * Mauricio Lin <mauricio.lin@indt.org.br>
  23. *
  24. * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
  25. *
  26. * A new process specific entry (smaps) included in /proc. It shows the
  27. * size of rss for each memory area. The maps entry lacks information
  28. * about physical memory size (rss) for each mapped file, i.e.,
  29. * rss information for executables and library files.
  30. * This additional information is useful for any tools that need to know
  31. * about physical memory consumption for a process specific library.
  32. *
  33. * Changelog:
  34. * 21-Feb-2005
  35. * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
  36. * Pud inclusion in the page table walking.
  37. *
  38. * ChangeLog:
  39. * 10-Mar-2005
  40. * 10LE Instituto Nokia de Tecnologia - INdT:
  41. * A better way to walks through the page table as suggested by Hugh Dickins.
  42. *
  43. * Simo Piiroinen <simo.piiroinen@nokia.com>:
  44. * Smaps information related to shared, private, clean and dirty pages.
  45. *
  46. * Paul Mundt <paul.mundt@nokia.com>:
  47. * Overall revision about smaps.
  48. */
  49. #include <asm/uaccess.h>
  50. #include <linux/errno.h>
  51. #include <linux/time.h>
  52. #include <linux/proc_fs.h>
  53. #include <linux/stat.h>
  54. #include <linux/init.h>
  55. #include <linux/capability.h>
  56. #include <linux/file.h>
  57. #include <linux/string.h>
  58. #include <linux/seq_file.h>
  59. #include <linux/namei.h>
  60. #include <linux/mnt_namespace.h>
  61. #include <linux/mm.h>
  62. #include <linux/rcupdate.h>
  63. #include <linux/kallsyms.h>
  64. #include <linux/module.h>
  65. #include <linux/mount.h>
  66. #include <linux/security.h>
  67. #include <linux/ptrace.h>
  68. #include <linux/seccomp.h>
  69. #include <linux/cpuset.h>
  70. #include <linux/audit.h>
  71. #include <linux/poll.h>
  72. #include <linux/nsproxy.h>
  73. #include <linux/oom.h>
  74. #include "internal.h"
  75. /* NOTE:
  76. * Implementing inode permission operations in /proc is almost
  77. * certainly an error. Permission checks need to happen during
  78. * each system call not at open time. The reason is that most of
  79. * what we wish to check for permissions in /proc varies at runtime.
  80. *
  81. * The classic example of a problem is opening file descriptors
  82. * in /proc for a task before it execs a suid executable.
  83. */
  84. /* Worst case buffer size needed for holding an integer. */
  85. #define PROC_NUMBUF 13
  86. struct pid_entry {
  87. char *name;
  88. int len;
  89. mode_t mode;
  90. const struct inode_operations *iop;
  91. const struct file_operations *fop;
  92. union proc_op op;
  93. };
  94. #define NOD(NAME, MODE, IOP, FOP, OP) { \
  95. .name = (NAME), \
  96. .len = sizeof(NAME) - 1, \
  97. .mode = MODE, \
  98. .iop = IOP, \
  99. .fop = FOP, \
  100. .op = OP, \
  101. }
  102. #define DIR(NAME, MODE, OTYPE) \
  103. NOD(NAME, (S_IFDIR|(MODE)), \
  104. &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
  105. {} )
  106. #define LNK(NAME, OTYPE) \
  107. NOD(NAME, (S_IFLNK|S_IRWXUGO), \
  108. &proc_pid_link_inode_operations, NULL, \
  109. { .proc_get_link = &proc_##OTYPE##_link } )
  110. #define REG(NAME, MODE, OTYPE) \
  111. NOD(NAME, (S_IFREG|(MODE)), NULL, \
  112. &proc_##OTYPE##_operations, {})
  113. #define INF(NAME, MODE, OTYPE) \
  114. NOD(NAME, (S_IFREG|(MODE)), \
  115. NULL, &proc_info_file_operations, \
  116. { .proc_read = &proc_##OTYPE } )
  117. int maps_protect;
  118. EXPORT_SYMBOL(maps_protect);
  119. static struct fs_struct *get_fs_struct(struct task_struct *task)
  120. {
  121. struct fs_struct *fs;
  122. task_lock(task);
  123. fs = task->fs;
  124. if(fs)
  125. atomic_inc(&fs->count);
  126. task_unlock(task);
  127. return fs;
  128. }
  129. static int get_nr_threads(struct task_struct *tsk)
  130. {
  131. /* Must be called with the rcu_read_lock held */
  132. unsigned long flags;
  133. int count = 0;
  134. if (lock_task_sighand(tsk, &flags)) {
  135. count = atomic_read(&tsk->signal->count);
  136. unlock_task_sighand(tsk, &flags);
  137. }
  138. return count;
  139. }
  140. static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  141. {
  142. struct task_struct *task = get_proc_task(inode);
  143. struct fs_struct *fs = NULL;
  144. int result = -ENOENT;
  145. if (task) {
  146. fs = get_fs_struct(task);
  147. put_task_struct(task);
  148. }
  149. if (fs) {
  150. read_lock(&fs->lock);
  151. *mnt = mntget(fs->pwdmnt);
  152. *dentry = dget(fs->pwd);
  153. read_unlock(&fs->lock);
  154. result = 0;
  155. put_fs_struct(fs);
  156. }
  157. return result;
  158. }
  159. static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  160. {
  161. struct task_struct *task = get_proc_task(inode);
  162. struct fs_struct *fs = NULL;
  163. int result = -ENOENT;
  164. if (task) {
  165. fs = get_fs_struct(task);
  166. put_task_struct(task);
  167. }
  168. if (fs) {
  169. read_lock(&fs->lock);
  170. *mnt = mntget(fs->rootmnt);
  171. *dentry = dget(fs->root);
  172. read_unlock(&fs->lock);
  173. result = 0;
  174. put_fs_struct(fs);
  175. }
  176. return result;
  177. }
  178. #define MAY_PTRACE(task) \
  179. (task == current || \
  180. (task->parent == current && \
  181. (task->ptrace & PT_PTRACED) && \
  182. (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
  183. security_ptrace(current,task) == 0))
  184. static int proc_pid_environ(struct task_struct *task, char * buffer)
  185. {
  186. int res = 0;
  187. struct mm_struct *mm = get_task_mm(task);
  188. if (mm) {
  189. unsigned int len = mm->env_end - mm->env_start;
  190. if (len > PAGE_SIZE)
  191. len = PAGE_SIZE;
  192. res = access_process_vm(task, mm->env_start, buffer, len, 0);
  193. if (!ptrace_may_attach(task))
  194. res = -ESRCH;
  195. mmput(mm);
  196. }
  197. return res;
  198. }
  199. static int proc_pid_cmdline(struct task_struct *task, char * buffer)
  200. {
  201. int res = 0;
  202. unsigned int len;
  203. struct mm_struct *mm = get_task_mm(task);
  204. if (!mm)
  205. goto out;
  206. if (!mm->arg_end)
  207. goto out_mm; /* Shh! No looking before we're done */
  208. len = mm->arg_end - mm->arg_start;
  209. if (len > PAGE_SIZE)
  210. len = PAGE_SIZE;
  211. res = access_process_vm(task, mm->arg_start, buffer, len, 0);
  212. // If the nul at the end of args has been overwritten, then
  213. // assume application is using setproctitle(3).
  214. if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
  215. len = strnlen(buffer, res);
  216. if (len < res) {
  217. res = len;
  218. } else {
  219. len = mm->env_end - mm->env_start;
  220. if (len > PAGE_SIZE - res)
  221. len = PAGE_SIZE - res;
  222. res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
  223. res = strnlen(buffer, res);
  224. }
  225. }
  226. out_mm:
  227. mmput(mm);
  228. out:
  229. return res;
  230. }
  231. static int proc_pid_auxv(struct task_struct *task, char *buffer)
  232. {
  233. int res = 0;
  234. struct mm_struct *mm = get_task_mm(task);
  235. if (mm) {
  236. unsigned int nwords = 0;
  237. do
  238. nwords += 2;
  239. while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
  240. res = nwords * sizeof(mm->saved_auxv[0]);
  241. if (res > PAGE_SIZE)
  242. res = PAGE_SIZE;
  243. memcpy(buffer, mm->saved_auxv, res);
  244. mmput(mm);
  245. }
  246. return res;
  247. }
  248. #ifdef CONFIG_KALLSYMS
  249. /*
  250. * Provides a wchan file via kallsyms in a proper one-value-per-file format.
  251. * Returns the resolved symbol. If that fails, simply return the address.
  252. */
  253. static int proc_pid_wchan(struct task_struct *task, char *buffer)
  254. {
  255. unsigned long wchan;
  256. char symname[KSYM_NAME_LEN+1];
  257. wchan = get_wchan(task);
  258. if (lookup_symbol_name(wchan, symname) < 0)
  259. return sprintf(buffer, "%lu", wchan);
  260. else
  261. return sprintf(buffer, "%s", symname);
  262. }
  263. #endif /* CONFIG_KALLSYMS */
  264. #ifdef CONFIG_SCHEDSTATS
  265. /*
  266. * Provides /proc/PID/schedstat
  267. */
  268. static int proc_pid_schedstat(struct task_struct *task, char *buffer)
  269. {
  270. return sprintf(buffer, "%lu %lu %lu\n",
  271. task->sched_info.cpu_time,
  272. task->sched_info.run_delay,
  273. task->sched_info.pcnt);
  274. }
  275. #endif
  276. /* The badness from the OOM killer */
  277. unsigned long badness(struct task_struct *p, unsigned long uptime);
  278. static int proc_oom_score(struct task_struct *task, char *buffer)
  279. {
  280. unsigned long points;
  281. struct timespec uptime;
  282. do_posix_clock_monotonic_gettime(&uptime);
  283. read_lock(&tasklist_lock);
  284. points = badness(task, uptime.tv_sec);
  285. read_unlock(&tasklist_lock);
  286. return sprintf(buffer, "%lu\n", points);
  287. }
  288. /************************************************************************/
  289. /* Here the fs part begins */
  290. /************************************************************************/
  291. /* permission checks */
  292. static int proc_fd_access_allowed(struct inode *inode)
  293. {
  294. struct task_struct *task;
  295. int allowed = 0;
  296. /* Allow access to a task's file descriptors if it is us or we
  297. * may use ptrace attach to the process and find out that
  298. * information.
  299. */
  300. task = get_proc_task(inode);
  301. if (task) {
  302. allowed = ptrace_may_attach(task);
  303. put_task_struct(task);
  304. }
  305. return allowed;
  306. }
  307. static int proc_setattr(struct dentry *dentry, struct iattr *attr)
  308. {
  309. int error;
  310. struct inode *inode = dentry->d_inode;
  311. if (attr->ia_valid & ATTR_MODE)
  312. return -EPERM;
  313. error = inode_change_ok(inode, attr);
  314. if (!error) {
  315. error = security_inode_setattr(dentry, attr);
  316. if (!error)
  317. error = inode_setattr(inode, attr);
  318. }
  319. return error;
  320. }
  321. static const struct inode_operations proc_def_inode_operations = {
  322. .setattr = proc_setattr,
  323. };
  324. extern struct seq_operations mounts_op;
  325. struct proc_mounts {
  326. struct seq_file m;
  327. int event;
  328. };
  329. static int mounts_open(struct inode *inode, struct file *file)
  330. {
  331. struct task_struct *task = get_proc_task(inode);
  332. struct mnt_namespace *ns = NULL;
  333. struct proc_mounts *p;
  334. int ret = -EINVAL;
  335. if (task) {
  336. task_lock(task);
  337. if (task->nsproxy) {
  338. ns = task->nsproxy->mnt_ns;
  339. if (ns)
  340. get_mnt_ns(ns);
  341. }
  342. task_unlock(task);
  343. put_task_struct(task);
  344. }
  345. if (ns) {
  346. ret = -ENOMEM;
  347. p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
  348. if (p) {
  349. file->private_data = &p->m;
  350. ret = seq_open(file, &mounts_op);
  351. if (!ret) {
  352. p->m.private = ns;
  353. p->event = ns->event;
  354. return 0;
  355. }
  356. kfree(p);
  357. }
  358. put_mnt_ns(ns);
  359. }
  360. return ret;
  361. }
  362. static int mounts_release(struct inode *inode, struct file *file)
  363. {
  364. struct seq_file *m = file->private_data;
  365. struct mnt_namespace *ns = m->private;
  366. put_mnt_ns(ns);
  367. return seq_release(inode, file);
  368. }
  369. static unsigned mounts_poll(struct file *file, poll_table *wait)
  370. {
  371. struct proc_mounts *p = file->private_data;
  372. struct mnt_namespace *ns = p->m.private;
  373. unsigned res = 0;
  374. poll_wait(file, &ns->poll, wait);
  375. spin_lock(&vfsmount_lock);
  376. if (p->event != ns->event) {
  377. p->event = ns->event;
  378. res = POLLERR;
  379. }
  380. spin_unlock(&vfsmount_lock);
  381. return res;
  382. }
  383. static const struct file_operations proc_mounts_operations = {
  384. .open = mounts_open,
  385. .read = seq_read,
  386. .llseek = seq_lseek,
  387. .release = mounts_release,
  388. .poll = mounts_poll,
  389. };
  390. extern struct seq_operations mountstats_op;
  391. static int mountstats_open(struct inode *inode, struct file *file)
  392. {
  393. int ret = seq_open(file, &mountstats_op);
  394. if (!ret) {
  395. struct seq_file *m = file->private_data;
  396. struct mnt_namespace *mnt_ns = NULL;
  397. struct task_struct *task = get_proc_task(inode);
  398. if (task) {
  399. task_lock(task);
  400. if (task->nsproxy)
  401. mnt_ns = task->nsproxy->mnt_ns;
  402. if (mnt_ns)
  403. get_mnt_ns(mnt_ns);
  404. task_unlock(task);
  405. put_task_struct(task);
  406. }
  407. if (mnt_ns)
  408. m->private = mnt_ns;
  409. else {
  410. seq_release(inode, file);
  411. ret = -EINVAL;
  412. }
  413. }
  414. return ret;
  415. }
  416. static const struct file_operations proc_mountstats_operations = {
  417. .open = mountstats_open,
  418. .read = seq_read,
  419. .llseek = seq_lseek,
  420. .release = mounts_release,
  421. };
  422. #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
  423. static ssize_t proc_info_read(struct file * file, char __user * buf,
  424. size_t count, loff_t *ppos)
  425. {
  426. struct inode * inode = file->f_path.dentry->d_inode;
  427. unsigned long page;
  428. ssize_t length;
  429. struct task_struct *task = get_proc_task(inode);
  430. length = -ESRCH;
  431. if (!task)
  432. goto out_no_task;
  433. if (count > PROC_BLOCK_SIZE)
  434. count = PROC_BLOCK_SIZE;
  435. length = -ENOMEM;
  436. if (!(page = __get_free_page(GFP_KERNEL)))
  437. goto out;
  438. length = PROC_I(inode)->op.proc_read(task, (char*)page);
  439. if (length >= 0)
  440. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  441. free_page(page);
  442. out:
  443. put_task_struct(task);
  444. out_no_task:
  445. return length;
  446. }
  447. static const struct file_operations proc_info_file_operations = {
  448. .read = proc_info_read,
  449. };
  450. static int mem_open(struct inode* inode, struct file* file)
  451. {
  452. file->private_data = (void*)((long)current->self_exec_id);
  453. return 0;
  454. }
  455. static ssize_t mem_read(struct file * file, char __user * buf,
  456. size_t count, loff_t *ppos)
  457. {
  458. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  459. char *page;
  460. unsigned long src = *ppos;
  461. int ret = -ESRCH;
  462. struct mm_struct *mm;
  463. if (!task)
  464. goto out_no_task;
  465. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  466. goto out;
  467. ret = -ENOMEM;
  468. page = (char *)__get_free_page(GFP_USER);
  469. if (!page)
  470. goto out;
  471. ret = 0;
  472. mm = get_task_mm(task);
  473. if (!mm)
  474. goto out_free;
  475. ret = -EIO;
  476. if (file->private_data != (void*)((long)current->self_exec_id))
  477. goto out_put;
  478. ret = 0;
  479. while (count > 0) {
  480. int this_len, retval;
  481. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  482. retval = access_process_vm(task, src, page, this_len, 0);
  483. if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
  484. if (!ret)
  485. ret = -EIO;
  486. break;
  487. }
  488. if (copy_to_user(buf, page, retval)) {
  489. ret = -EFAULT;
  490. break;
  491. }
  492. ret += retval;
  493. src += retval;
  494. buf += retval;
  495. count -= retval;
  496. }
  497. *ppos = src;
  498. out_put:
  499. mmput(mm);
  500. out_free:
  501. free_page((unsigned long) page);
  502. out:
  503. put_task_struct(task);
  504. out_no_task:
  505. return ret;
  506. }
  507. #define mem_write NULL
  508. #ifndef mem_write
  509. /* This is a security hazard */
  510. static ssize_t mem_write(struct file * file, const char __user *buf,
  511. size_t count, loff_t *ppos)
  512. {
  513. int copied;
  514. char *page;
  515. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  516. unsigned long dst = *ppos;
  517. copied = -ESRCH;
  518. if (!task)
  519. goto out_no_task;
  520. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  521. goto out;
  522. copied = -ENOMEM;
  523. page = (char *)__get_free_page(GFP_USER);
  524. if (!page)
  525. goto out;
  526. copied = 0;
  527. while (count > 0) {
  528. int this_len, retval;
  529. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  530. if (copy_from_user(page, buf, this_len)) {
  531. copied = -EFAULT;
  532. break;
  533. }
  534. retval = access_process_vm(task, dst, page, this_len, 1);
  535. if (!retval) {
  536. if (!copied)
  537. copied = -EIO;
  538. break;
  539. }
  540. copied += retval;
  541. buf += retval;
  542. dst += retval;
  543. count -= retval;
  544. }
  545. *ppos = dst;
  546. free_page((unsigned long) page);
  547. out:
  548. put_task_struct(task);
  549. out_no_task:
  550. return copied;
  551. }
  552. #endif
  553. static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
  554. {
  555. switch (orig) {
  556. case 0:
  557. file->f_pos = offset;
  558. break;
  559. case 1:
  560. file->f_pos += offset;
  561. break;
  562. default:
  563. return -EINVAL;
  564. }
  565. force_successful_syscall_return();
  566. return file->f_pos;
  567. }
  568. static const struct file_operations proc_mem_operations = {
  569. .llseek = mem_lseek,
  570. .read = mem_read,
  571. .write = mem_write,
  572. .open = mem_open,
  573. };
  574. static ssize_t oom_adjust_read(struct file *file, char __user *buf,
  575. size_t count, loff_t *ppos)
  576. {
  577. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  578. char buffer[PROC_NUMBUF];
  579. size_t len;
  580. int oom_adjust;
  581. loff_t __ppos = *ppos;
  582. if (!task)
  583. return -ESRCH;
  584. oom_adjust = task->oomkilladj;
  585. put_task_struct(task);
  586. len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
  587. if (__ppos >= len)
  588. return 0;
  589. if (count > len-__ppos)
  590. count = len-__ppos;
  591. if (copy_to_user(buf, buffer + __ppos, count))
  592. return -EFAULT;
  593. *ppos = __ppos + count;
  594. return count;
  595. }
  596. static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
  597. size_t count, loff_t *ppos)
  598. {
  599. struct task_struct *task;
  600. char buffer[PROC_NUMBUF], *end;
  601. int oom_adjust;
  602. memset(buffer, 0, sizeof(buffer));
  603. if (count > sizeof(buffer) - 1)
  604. count = sizeof(buffer) - 1;
  605. if (copy_from_user(buffer, buf, count))
  606. return -EFAULT;
  607. oom_adjust = simple_strtol(buffer, &end, 0);
  608. if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
  609. oom_adjust != OOM_DISABLE)
  610. return -EINVAL;
  611. if (*end == '\n')
  612. end++;
  613. task = get_proc_task(file->f_path.dentry->d_inode);
  614. if (!task)
  615. return -ESRCH;
  616. if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
  617. put_task_struct(task);
  618. return -EACCES;
  619. }
  620. task->oomkilladj = oom_adjust;
  621. put_task_struct(task);
  622. if (end - buffer == 0)
  623. return -EIO;
  624. return end - buffer;
  625. }
  626. static const struct file_operations proc_oom_adjust_operations = {
  627. .read = oom_adjust_read,
  628. .write = oom_adjust_write,
  629. };
  630. static ssize_t clear_refs_write(struct file *file, const char __user *buf,
  631. size_t count, loff_t *ppos)
  632. {
  633. struct task_struct *task;
  634. char buffer[PROC_NUMBUF], *end;
  635. struct mm_struct *mm;
  636. memset(buffer, 0, sizeof(buffer));
  637. if (count > sizeof(buffer) - 1)
  638. count = sizeof(buffer) - 1;
  639. if (copy_from_user(buffer, buf, count))
  640. return -EFAULT;
  641. if (!simple_strtol(buffer, &end, 0))
  642. return -EINVAL;
  643. if (*end == '\n')
  644. end++;
  645. task = get_proc_task(file->f_path.dentry->d_inode);
  646. if (!task)
  647. return -ESRCH;
  648. mm = get_task_mm(task);
  649. if (mm) {
  650. clear_refs_smap(mm);
  651. mmput(mm);
  652. }
  653. put_task_struct(task);
  654. if (end - buffer == 0)
  655. return -EIO;
  656. return end - buffer;
  657. }
  658. static struct file_operations proc_clear_refs_operations = {
  659. .write = clear_refs_write,
  660. };
  661. #ifdef CONFIG_AUDITSYSCALL
  662. #define TMPBUFLEN 21
  663. static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
  664. size_t count, loff_t *ppos)
  665. {
  666. struct inode * inode = file->f_path.dentry->d_inode;
  667. struct task_struct *task = get_proc_task(inode);
  668. ssize_t length;
  669. char tmpbuf[TMPBUFLEN];
  670. if (!task)
  671. return -ESRCH;
  672. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  673. audit_get_loginuid(task->audit_context));
  674. put_task_struct(task);
  675. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  676. }
  677. static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
  678. size_t count, loff_t *ppos)
  679. {
  680. struct inode * inode = file->f_path.dentry->d_inode;
  681. char *page, *tmp;
  682. ssize_t length;
  683. uid_t loginuid;
  684. if (!capable(CAP_AUDIT_CONTROL))
  685. return -EPERM;
  686. if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
  687. return -EPERM;
  688. if (count >= PAGE_SIZE)
  689. count = PAGE_SIZE - 1;
  690. if (*ppos != 0) {
  691. /* No partial writes. */
  692. return -EINVAL;
  693. }
  694. page = (char*)__get_free_page(GFP_USER);
  695. if (!page)
  696. return -ENOMEM;
  697. length = -EFAULT;
  698. if (copy_from_user(page, buf, count))
  699. goto out_free_page;
  700. page[count] = '\0';
  701. loginuid = simple_strtoul(page, &tmp, 10);
  702. if (tmp == page) {
  703. length = -EINVAL;
  704. goto out_free_page;
  705. }
  706. length = audit_set_loginuid(current, loginuid);
  707. if (likely(length == 0))
  708. length = count;
  709. out_free_page:
  710. free_page((unsigned long) page);
  711. return length;
  712. }
  713. static const struct file_operations proc_loginuid_operations = {
  714. .read = proc_loginuid_read,
  715. .write = proc_loginuid_write,
  716. };
  717. #endif
  718. #ifdef CONFIG_SECCOMP
  719. static ssize_t seccomp_read(struct file *file, char __user *buf,
  720. size_t count, loff_t *ppos)
  721. {
  722. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  723. char __buf[20];
  724. loff_t __ppos = *ppos;
  725. size_t len;
  726. if (!tsk)
  727. return -ESRCH;
  728. /* no need to print the trailing zero, so use only len */
  729. len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
  730. put_task_struct(tsk);
  731. if (__ppos >= len)
  732. return 0;
  733. if (count > len - __ppos)
  734. count = len - __ppos;
  735. if (copy_to_user(buf, __buf + __ppos, count))
  736. return -EFAULT;
  737. *ppos = __ppos + count;
  738. return count;
  739. }
  740. static ssize_t seccomp_write(struct file *file, const char __user *buf,
  741. size_t count, loff_t *ppos)
  742. {
  743. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  744. char __buf[20], *end;
  745. unsigned int seccomp_mode;
  746. ssize_t result;
  747. result = -ESRCH;
  748. if (!tsk)
  749. goto out_no_task;
  750. /* can set it only once to be even more secure */
  751. result = -EPERM;
  752. if (unlikely(tsk->seccomp.mode))
  753. goto out;
  754. result = -EFAULT;
  755. memset(__buf, 0, sizeof(__buf));
  756. count = min(count, sizeof(__buf) - 1);
  757. if (copy_from_user(__buf, buf, count))
  758. goto out;
  759. seccomp_mode = simple_strtoul(__buf, &end, 0);
  760. if (*end == '\n')
  761. end++;
  762. result = -EINVAL;
  763. if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
  764. tsk->seccomp.mode = seccomp_mode;
  765. set_tsk_thread_flag(tsk, TIF_SECCOMP);
  766. } else
  767. goto out;
  768. result = -EIO;
  769. if (unlikely(!(end - __buf)))
  770. goto out;
  771. result = end - __buf;
  772. out:
  773. put_task_struct(tsk);
  774. out_no_task:
  775. return result;
  776. }
  777. static const struct file_operations proc_seccomp_operations = {
  778. .read = seccomp_read,
  779. .write = seccomp_write,
  780. };
  781. #endif /* CONFIG_SECCOMP */
  782. #ifdef CONFIG_FAULT_INJECTION
  783. static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
  784. size_t count, loff_t *ppos)
  785. {
  786. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  787. char buffer[PROC_NUMBUF];
  788. size_t len;
  789. int make_it_fail;
  790. loff_t __ppos = *ppos;
  791. if (!task)
  792. return -ESRCH;
  793. make_it_fail = task->make_it_fail;
  794. put_task_struct(task);
  795. len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
  796. if (__ppos >= len)
  797. return 0;
  798. if (count > len-__ppos)
  799. count = len-__ppos;
  800. if (copy_to_user(buf, buffer + __ppos, count))
  801. return -EFAULT;
  802. *ppos = __ppos + count;
  803. return count;
  804. }
  805. static ssize_t proc_fault_inject_write(struct file * file,
  806. const char __user * buf, size_t count, loff_t *ppos)
  807. {
  808. struct task_struct *task;
  809. char buffer[PROC_NUMBUF], *end;
  810. int make_it_fail;
  811. if (!capable(CAP_SYS_RESOURCE))
  812. return -EPERM;
  813. memset(buffer, 0, sizeof(buffer));
  814. if (count > sizeof(buffer) - 1)
  815. count = sizeof(buffer) - 1;
  816. if (copy_from_user(buffer, buf, count))
  817. return -EFAULT;
  818. make_it_fail = simple_strtol(buffer, &end, 0);
  819. if (*end == '\n')
  820. end++;
  821. task = get_proc_task(file->f_dentry->d_inode);
  822. if (!task)
  823. return -ESRCH;
  824. task->make_it_fail = make_it_fail;
  825. put_task_struct(task);
  826. if (end - buffer == 0)
  827. return -EIO;
  828. return end - buffer;
  829. }
  830. static const struct file_operations proc_fault_inject_operations = {
  831. .read = proc_fault_inject_read,
  832. .write = proc_fault_inject_write,
  833. };
  834. #endif
  835. static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
  836. {
  837. struct inode *inode = dentry->d_inode;
  838. int error = -EACCES;
  839. /* We don't need a base pointer in the /proc filesystem */
  840. path_release(nd);
  841. /* Are we allowed to snoop on the tasks file descriptors? */
  842. if (!proc_fd_access_allowed(inode))
  843. goto out;
  844. error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
  845. nd->last_type = LAST_BIND;
  846. out:
  847. return ERR_PTR(error);
  848. }
  849. static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
  850. char __user *buffer, int buflen)
  851. {
  852. struct inode * inode;
  853. char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
  854. int len;
  855. if (!tmp)
  856. return -ENOMEM;
  857. inode = dentry->d_inode;
  858. path = d_path(dentry, mnt, tmp, PAGE_SIZE);
  859. len = PTR_ERR(path);
  860. if (IS_ERR(path))
  861. goto out;
  862. len = tmp + PAGE_SIZE - 1 - path;
  863. if (len > buflen)
  864. len = buflen;
  865. if (copy_to_user(buffer, path, len))
  866. len = -EFAULT;
  867. out:
  868. free_page((unsigned long)tmp);
  869. return len;
  870. }
  871. static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
  872. {
  873. int error = -EACCES;
  874. struct inode *inode = dentry->d_inode;
  875. struct dentry *de;
  876. struct vfsmount *mnt = NULL;
  877. /* Are we allowed to snoop on the tasks file descriptors? */
  878. if (!proc_fd_access_allowed(inode))
  879. goto out;
  880. error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
  881. if (error)
  882. goto out;
  883. error = do_proc_readlink(de, mnt, buffer, buflen);
  884. dput(de);
  885. mntput(mnt);
  886. out:
  887. return error;
  888. }
  889. static const struct inode_operations proc_pid_link_inode_operations = {
  890. .readlink = proc_pid_readlink,
  891. .follow_link = proc_pid_follow_link,
  892. .setattr = proc_setattr,
  893. };
  894. /* building an inode */
  895. static int task_dumpable(struct task_struct *task)
  896. {
  897. int dumpable = 0;
  898. struct mm_struct *mm;
  899. task_lock(task);
  900. mm = task->mm;
  901. if (mm)
  902. dumpable = mm->dumpable;
  903. task_unlock(task);
  904. if(dumpable == 1)
  905. return 1;
  906. return 0;
  907. }
  908. static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
  909. {
  910. struct inode * inode;
  911. struct proc_inode *ei;
  912. /* We need a new inode */
  913. inode = new_inode(sb);
  914. if (!inode)
  915. goto out;
  916. /* Common stuff */
  917. ei = PROC_I(inode);
  918. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  919. inode->i_op = &proc_def_inode_operations;
  920. /*
  921. * grab the reference to task.
  922. */
  923. ei->pid = get_task_pid(task, PIDTYPE_PID);
  924. if (!ei->pid)
  925. goto out_unlock;
  926. inode->i_uid = 0;
  927. inode->i_gid = 0;
  928. if (task_dumpable(task)) {
  929. inode->i_uid = task->euid;
  930. inode->i_gid = task->egid;
  931. }
  932. security_task_to_inode(task, inode);
  933. out:
  934. return inode;
  935. out_unlock:
  936. iput(inode);
  937. return NULL;
  938. }
  939. static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  940. {
  941. struct inode *inode = dentry->d_inode;
  942. struct task_struct *task;
  943. generic_fillattr(inode, stat);
  944. rcu_read_lock();
  945. stat->uid = 0;
  946. stat->gid = 0;
  947. task = pid_task(proc_pid(inode), PIDTYPE_PID);
  948. if (task) {
  949. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  950. task_dumpable(task)) {
  951. stat->uid = task->euid;
  952. stat->gid = task->egid;
  953. }
  954. }
  955. rcu_read_unlock();
  956. return 0;
  957. }
  958. /* dentry stuff */
  959. /*
  960. * Exceptional case: normally we are not allowed to unhash a busy
  961. * directory. In this case, however, we can do it - no aliasing problems
  962. * due to the way we treat inodes.
  963. *
  964. * Rewrite the inode's ownerships here because the owning task may have
  965. * performed a setuid(), etc.
  966. *
  967. * Before the /proc/pid/status file was created the only way to read
  968. * the effective uid of a /process was to stat /proc/pid. Reading
  969. * /proc/pid/status is slow enough that procps and other packages
  970. * kept stating /proc/pid. To keep the rules in /proc simple I have
  971. * made this apply to all per process world readable and executable
  972. * directories.
  973. */
  974. static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
  975. {
  976. struct inode *inode = dentry->d_inode;
  977. struct task_struct *task = get_proc_task(inode);
  978. if (task) {
  979. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  980. task_dumpable(task)) {
  981. inode->i_uid = task->euid;
  982. inode->i_gid = task->egid;
  983. } else {
  984. inode->i_uid = 0;
  985. inode->i_gid = 0;
  986. }
  987. inode->i_mode &= ~(S_ISUID | S_ISGID);
  988. security_task_to_inode(task, inode);
  989. put_task_struct(task);
  990. return 1;
  991. }
  992. d_drop(dentry);
  993. return 0;
  994. }
  995. static int pid_delete_dentry(struct dentry * dentry)
  996. {
  997. /* Is the task we represent dead?
  998. * If so, then don't put the dentry on the lru list,
  999. * kill it immediately.
  1000. */
  1001. return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
  1002. }
  1003. static struct dentry_operations pid_dentry_operations =
  1004. {
  1005. .d_revalidate = pid_revalidate,
  1006. .d_delete = pid_delete_dentry,
  1007. };
  1008. /* Lookups */
  1009. typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
  1010. struct task_struct *, const void *);
  1011. /*
  1012. * Fill a directory entry.
  1013. *
  1014. * If possible create the dcache entry and derive our inode number and
  1015. * file type from dcache entry.
  1016. *
  1017. * Since all of the proc inode numbers are dynamically generated, the inode
  1018. * numbers do not exist until the inode is cache. This means creating the
  1019. * the dcache entry in readdir is necessary to keep the inode numbers
  1020. * reported by readdir in sync with the inode numbers reported
  1021. * by stat.
  1022. */
  1023. static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  1024. char *name, int len,
  1025. instantiate_t instantiate, struct task_struct *task, const void *ptr)
  1026. {
  1027. struct dentry *child, *dir = filp->f_path.dentry;
  1028. struct inode *inode;
  1029. struct qstr qname;
  1030. ino_t ino = 0;
  1031. unsigned type = DT_UNKNOWN;
  1032. qname.name = name;
  1033. qname.len = len;
  1034. qname.hash = full_name_hash(name, len);
  1035. child = d_lookup(dir, &qname);
  1036. if (!child) {
  1037. struct dentry *new;
  1038. new = d_alloc(dir, &qname);
  1039. if (new) {
  1040. child = instantiate(dir->d_inode, new, task, ptr);
  1041. if (child)
  1042. dput(new);
  1043. else
  1044. child = new;
  1045. }
  1046. }
  1047. if (!child || IS_ERR(child) || !child->d_inode)
  1048. goto end_instantiate;
  1049. inode = child->d_inode;
  1050. if (inode) {
  1051. ino = inode->i_ino;
  1052. type = inode->i_mode >> 12;
  1053. }
  1054. dput(child);
  1055. end_instantiate:
  1056. if (!ino)
  1057. ino = find_inode_number(dir, &qname);
  1058. if (!ino)
  1059. ino = 1;
  1060. return filldir(dirent, name, len, filp->f_pos, ino, type);
  1061. }
  1062. static unsigned name_to_int(struct dentry *dentry)
  1063. {
  1064. const char *name = dentry->d_name.name;
  1065. int len = dentry->d_name.len;
  1066. unsigned n = 0;
  1067. if (len > 1 && *name == '0')
  1068. goto out;
  1069. while (len-- > 0) {
  1070. unsigned c = *name++ - '0';
  1071. if (c > 9)
  1072. goto out;
  1073. if (n >= (~0U-9)/10)
  1074. goto out;
  1075. n *= 10;
  1076. n += c;
  1077. }
  1078. return n;
  1079. out:
  1080. return ~0U;
  1081. }
  1082. #define PROC_FDINFO_MAX 64
  1083. static int proc_fd_info(struct inode *inode, struct dentry **dentry,
  1084. struct vfsmount **mnt, char *info)
  1085. {
  1086. struct task_struct *task = get_proc_task(inode);
  1087. struct files_struct *files = NULL;
  1088. struct file *file;
  1089. int fd = proc_fd(inode);
  1090. if (task) {
  1091. files = get_files_struct(task);
  1092. put_task_struct(task);
  1093. }
  1094. if (files) {
  1095. /*
  1096. * We are not taking a ref to the file structure, so we must
  1097. * hold ->file_lock.
  1098. */
  1099. spin_lock(&files->file_lock);
  1100. file = fcheck_files(files, fd);
  1101. if (file) {
  1102. if (mnt)
  1103. *mnt = mntget(file->f_path.mnt);
  1104. if (dentry)
  1105. *dentry = dget(file->f_path.dentry);
  1106. if (info)
  1107. snprintf(info, PROC_FDINFO_MAX,
  1108. "pos:\t%lli\n"
  1109. "flags:\t0%o\n",
  1110. (long long) file->f_pos,
  1111. file->f_flags);
  1112. spin_unlock(&files->file_lock);
  1113. put_files_struct(files);
  1114. return 0;
  1115. }
  1116. spin_unlock(&files->file_lock);
  1117. put_files_struct(files);
  1118. }
  1119. return -ENOENT;
  1120. }
  1121. static int proc_fd_link(struct inode *inode, struct dentry **dentry,
  1122. struct vfsmount **mnt)
  1123. {
  1124. return proc_fd_info(inode, dentry, mnt, NULL);
  1125. }
  1126. static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
  1127. {
  1128. struct inode *inode = dentry->d_inode;
  1129. struct task_struct *task = get_proc_task(inode);
  1130. int fd = proc_fd(inode);
  1131. struct files_struct *files;
  1132. if (task) {
  1133. files = get_files_struct(task);
  1134. if (files) {
  1135. rcu_read_lock();
  1136. if (fcheck_files(files, fd)) {
  1137. rcu_read_unlock();
  1138. put_files_struct(files);
  1139. if (task_dumpable(task)) {
  1140. inode->i_uid = task->euid;
  1141. inode->i_gid = task->egid;
  1142. } else {
  1143. inode->i_uid = 0;
  1144. inode->i_gid = 0;
  1145. }
  1146. inode->i_mode &= ~(S_ISUID | S_ISGID);
  1147. security_task_to_inode(task, inode);
  1148. put_task_struct(task);
  1149. return 1;
  1150. }
  1151. rcu_read_unlock();
  1152. put_files_struct(files);
  1153. }
  1154. put_task_struct(task);
  1155. }
  1156. d_drop(dentry);
  1157. return 0;
  1158. }
  1159. static struct dentry_operations tid_fd_dentry_operations =
  1160. {
  1161. .d_revalidate = tid_fd_revalidate,
  1162. .d_delete = pid_delete_dentry,
  1163. };
  1164. static struct dentry *proc_fd_instantiate(struct inode *dir,
  1165. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1166. {
  1167. unsigned fd = *(const unsigned *)ptr;
  1168. struct file *file;
  1169. struct files_struct *files;
  1170. struct inode *inode;
  1171. struct proc_inode *ei;
  1172. struct dentry *error = ERR_PTR(-ENOENT);
  1173. inode = proc_pid_make_inode(dir->i_sb, task);
  1174. if (!inode)
  1175. goto out;
  1176. ei = PROC_I(inode);
  1177. ei->fd = fd;
  1178. files = get_files_struct(task);
  1179. if (!files)
  1180. goto out_iput;
  1181. inode->i_mode = S_IFLNK;
  1182. /*
  1183. * We are not taking a ref to the file structure, so we must
  1184. * hold ->file_lock.
  1185. */
  1186. spin_lock(&files->file_lock);
  1187. file = fcheck_files(files, fd);
  1188. if (!file)
  1189. goto out_unlock;
  1190. if (file->f_mode & 1)
  1191. inode->i_mode |= S_IRUSR | S_IXUSR;
  1192. if (file->f_mode & 2)
  1193. inode->i_mode |= S_IWUSR | S_IXUSR;
  1194. spin_unlock(&files->file_lock);
  1195. put_files_struct(files);
  1196. inode->i_op = &proc_pid_link_inode_operations;
  1197. inode->i_size = 64;
  1198. ei->op.proc_get_link = proc_fd_link;
  1199. dentry->d_op = &tid_fd_dentry_operations;
  1200. d_add(dentry, inode);
  1201. /* Close the race of the process dying before we return the dentry */
  1202. if (tid_fd_revalidate(dentry, NULL))
  1203. error = NULL;
  1204. out:
  1205. return error;
  1206. out_unlock:
  1207. spin_unlock(&files->file_lock);
  1208. put_files_struct(files);
  1209. out_iput:
  1210. iput(inode);
  1211. goto out;
  1212. }
  1213. static struct dentry *proc_lookupfd_common(struct inode *dir,
  1214. struct dentry *dentry,
  1215. instantiate_t instantiate)
  1216. {
  1217. struct task_struct *task = get_proc_task(dir);
  1218. unsigned fd = name_to_int(dentry);
  1219. struct dentry *result = ERR_PTR(-ENOENT);
  1220. if (!task)
  1221. goto out_no_task;
  1222. if (fd == ~0U)
  1223. goto out;
  1224. result = instantiate(dir, dentry, task, &fd);
  1225. out:
  1226. put_task_struct(task);
  1227. out_no_task:
  1228. return result;
  1229. }
  1230. static int proc_readfd_common(struct file * filp, void * dirent,
  1231. filldir_t filldir, instantiate_t instantiate)
  1232. {
  1233. struct dentry *dentry = filp->f_path.dentry;
  1234. struct inode *inode = dentry->d_inode;
  1235. struct task_struct *p = get_proc_task(inode);
  1236. unsigned int fd, tid, ino;
  1237. int retval;
  1238. struct files_struct * files;
  1239. struct fdtable *fdt;
  1240. retval = -ENOENT;
  1241. if (!p)
  1242. goto out_no_task;
  1243. retval = 0;
  1244. tid = p->pid;
  1245. fd = filp->f_pos;
  1246. switch (fd) {
  1247. case 0:
  1248. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  1249. goto out;
  1250. filp->f_pos++;
  1251. case 1:
  1252. ino = parent_ino(dentry);
  1253. if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
  1254. goto out;
  1255. filp->f_pos++;
  1256. default:
  1257. files = get_files_struct(p);
  1258. if (!files)
  1259. goto out;
  1260. rcu_read_lock();
  1261. fdt = files_fdtable(files);
  1262. for (fd = filp->f_pos-2;
  1263. fd < fdt->max_fds;
  1264. fd++, filp->f_pos++) {
  1265. char name[PROC_NUMBUF];
  1266. int len;
  1267. if (!fcheck_files(files, fd))
  1268. continue;
  1269. rcu_read_unlock();
  1270. len = snprintf(name, sizeof(name), "%d", fd);
  1271. if (proc_fill_cache(filp, dirent, filldir,
  1272. name, len, instantiate,
  1273. p, &fd) < 0) {
  1274. rcu_read_lock();
  1275. break;
  1276. }
  1277. rcu_read_lock();
  1278. }
  1279. rcu_read_unlock();
  1280. put_files_struct(files);
  1281. }
  1282. out:
  1283. put_task_struct(p);
  1284. out_no_task:
  1285. return retval;
  1286. }
  1287. static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
  1288. struct nameidata *nd)
  1289. {
  1290. return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
  1291. }
  1292. static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
  1293. {
  1294. return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
  1295. }
  1296. static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
  1297. size_t len, loff_t *ppos)
  1298. {
  1299. char tmp[PROC_FDINFO_MAX];
  1300. int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
  1301. if (!err)
  1302. err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
  1303. return err;
  1304. }
  1305. static const struct file_operations proc_fdinfo_file_operations = {
  1306. .open = nonseekable_open,
  1307. .read = proc_fdinfo_read,
  1308. };
  1309. static const struct file_operations proc_fd_operations = {
  1310. .read = generic_read_dir,
  1311. .readdir = proc_readfd,
  1312. };
  1313. /*
  1314. * /proc/pid/fd needs a special permission handler so that a process can still
  1315. * access /proc/self/fd after it has executed a setuid().
  1316. */
  1317. static int proc_fd_permission(struct inode *inode, int mask,
  1318. struct nameidata *nd)
  1319. {
  1320. int rv;
  1321. rv = generic_permission(inode, mask, NULL);
  1322. if (rv == 0)
  1323. return 0;
  1324. if (task_pid(current) == proc_pid(inode))
  1325. rv = 0;
  1326. return rv;
  1327. }
  1328. /*
  1329. * proc directories can do almost nothing..
  1330. */
  1331. static const struct inode_operations proc_fd_inode_operations = {
  1332. .lookup = proc_lookupfd,
  1333. .permission = proc_fd_permission,
  1334. .setattr = proc_setattr,
  1335. };
  1336. static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
  1337. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1338. {
  1339. unsigned fd = *(unsigned *)ptr;
  1340. struct inode *inode;
  1341. struct proc_inode *ei;
  1342. struct dentry *error = ERR_PTR(-ENOENT);
  1343. inode = proc_pid_make_inode(dir->i_sb, task);
  1344. if (!inode)
  1345. goto out;
  1346. ei = PROC_I(inode);
  1347. ei->fd = fd;
  1348. inode->i_mode = S_IFREG | S_IRUSR;
  1349. inode->i_fop = &proc_fdinfo_file_operations;
  1350. dentry->d_op = &tid_fd_dentry_operations;
  1351. d_add(dentry, inode);
  1352. /* Close the race of the process dying before we return the dentry */
  1353. if (tid_fd_revalidate(dentry, NULL))
  1354. error = NULL;
  1355. out:
  1356. return error;
  1357. }
  1358. static struct dentry *proc_lookupfdinfo(struct inode *dir,
  1359. struct dentry *dentry,
  1360. struct nameidata *nd)
  1361. {
  1362. return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
  1363. }
  1364. static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
  1365. {
  1366. return proc_readfd_common(filp, dirent, filldir,
  1367. proc_fdinfo_instantiate);
  1368. }
  1369. static const struct file_operations proc_fdinfo_operations = {
  1370. .read = generic_read_dir,
  1371. .readdir = proc_readfdinfo,
  1372. };
  1373. /*
  1374. * proc directories can do almost nothing..
  1375. */
  1376. static const struct inode_operations proc_fdinfo_inode_operations = {
  1377. .lookup = proc_lookupfdinfo,
  1378. .setattr = proc_setattr,
  1379. };
  1380. static struct dentry *proc_pident_instantiate(struct inode *dir,
  1381. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1382. {
  1383. const struct pid_entry *p = ptr;
  1384. struct inode *inode;
  1385. struct proc_inode *ei;
  1386. struct dentry *error = ERR_PTR(-EINVAL);
  1387. inode = proc_pid_make_inode(dir->i_sb, task);
  1388. if (!inode)
  1389. goto out;
  1390. ei = PROC_I(inode);
  1391. inode->i_mode = p->mode;
  1392. if (S_ISDIR(inode->i_mode))
  1393. inode->i_nlink = 2; /* Use getattr to fix if necessary */
  1394. if (p->iop)
  1395. inode->i_op = p->iop;
  1396. if (p->fop)
  1397. inode->i_fop = p->fop;
  1398. ei->op = p->op;
  1399. dentry->d_op = &pid_dentry_operations;
  1400. d_add(dentry, inode);
  1401. /* Close the race of the process dying before we return the dentry */
  1402. if (pid_revalidate(dentry, NULL))
  1403. error = NULL;
  1404. out:
  1405. return error;
  1406. }
  1407. static struct dentry *proc_pident_lookup(struct inode *dir,
  1408. struct dentry *dentry,
  1409. const struct pid_entry *ents,
  1410. unsigned int nents)
  1411. {
  1412. struct inode *inode;
  1413. struct dentry *error;
  1414. struct task_struct *task = get_proc_task(dir);
  1415. const struct pid_entry *p, *last;
  1416. error = ERR_PTR(-ENOENT);
  1417. inode = NULL;
  1418. if (!task)
  1419. goto out_no_task;
  1420. /*
  1421. * Yes, it does not scale. And it should not. Don't add
  1422. * new entries into /proc/<tgid>/ without very good reasons.
  1423. */
  1424. last = &ents[nents - 1];
  1425. for (p = ents; p <= last; p++) {
  1426. if (p->len != dentry->d_name.len)
  1427. continue;
  1428. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1429. break;
  1430. }
  1431. if (p > last)
  1432. goto out;
  1433. error = proc_pident_instantiate(dir, dentry, task, p);
  1434. out:
  1435. put_task_struct(task);
  1436. out_no_task:
  1437. return error;
  1438. }
  1439. static int proc_pident_fill_cache(struct file *filp, void *dirent,
  1440. filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
  1441. {
  1442. return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
  1443. proc_pident_instantiate, task, p);
  1444. }
  1445. static int proc_pident_readdir(struct file *filp,
  1446. void *dirent, filldir_t filldir,
  1447. const struct pid_entry *ents, unsigned int nents)
  1448. {
  1449. int i;
  1450. int pid;
  1451. struct dentry *dentry = filp->f_path.dentry;
  1452. struct inode *inode = dentry->d_inode;
  1453. struct task_struct *task = get_proc_task(inode);
  1454. const struct pid_entry *p, *last;
  1455. ino_t ino;
  1456. int ret;
  1457. ret = -ENOENT;
  1458. if (!task)
  1459. goto out_no_task;
  1460. ret = 0;
  1461. pid = task->pid;
  1462. i = filp->f_pos;
  1463. switch (i) {
  1464. case 0:
  1465. ino = inode->i_ino;
  1466. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  1467. goto out;
  1468. i++;
  1469. filp->f_pos++;
  1470. /* fall through */
  1471. case 1:
  1472. ino = parent_ino(dentry);
  1473. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  1474. goto out;
  1475. i++;
  1476. filp->f_pos++;
  1477. /* fall through */
  1478. default:
  1479. i -= 2;
  1480. if (i >= nents) {
  1481. ret = 1;
  1482. goto out;
  1483. }
  1484. p = ents + i;
  1485. last = &ents[nents - 1];
  1486. while (p <= last) {
  1487. if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
  1488. goto out;
  1489. filp->f_pos++;
  1490. p++;
  1491. }
  1492. }
  1493. ret = 1;
  1494. out:
  1495. put_task_struct(task);
  1496. out_no_task:
  1497. return ret;
  1498. }
  1499. #ifdef CONFIG_SECURITY
  1500. static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
  1501. size_t count, loff_t *ppos)
  1502. {
  1503. struct inode * inode = file->f_path.dentry->d_inode;
  1504. char *p = NULL;
  1505. ssize_t length;
  1506. struct task_struct *task = get_proc_task(inode);
  1507. if (!task)
  1508. return -ESRCH;
  1509. length = security_getprocattr(task,
  1510. (char*)file->f_path.dentry->d_name.name,
  1511. &p);
  1512. put_task_struct(task);
  1513. if (length > 0)
  1514. length = simple_read_from_buffer(buf, count, ppos, p, length);
  1515. kfree(p);
  1516. return length;
  1517. }
  1518. static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
  1519. size_t count, loff_t *ppos)
  1520. {
  1521. struct inode * inode = file->f_path.dentry->d_inode;
  1522. char *page;
  1523. ssize_t length;
  1524. struct task_struct *task = get_proc_task(inode);
  1525. length = -ESRCH;
  1526. if (!task)
  1527. goto out_no_task;
  1528. if (count > PAGE_SIZE)
  1529. count = PAGE_SIZE;
  1530. /* No partial writes. */
  1531. length = -EINVAL;
  1532. if (*ppos != 0)
  1533. goto out;
  1534. length = -ENOMEM;
  1535. page = (char*)__get_free_page(GFP_USER);
  1536. if (!page)
  1537. goto out;
  1538. length = -EFAULT;
  1539. if (copy_from_user(page, buf, count))
  1540. goto out_free;
  1541. length = security_setprocattr(task,
  1542. (char*)file->f_path.dentry->d_name.name,
  1543. (void*)page, count);
  1544. out_free:
  1545. free_page((unsigned long) page);
  1546. out:
  1547. put_task_struct(task);
  1548. out_no_task:
  1549. return length;
  1550. }
  1551. static const struct file_operations proc_pid_attr_operations = {
  1552. .read = proc_pid_attr_read,
  1553. .write = proc_pid_attr_write,
  1554. };
  1555. static const struct pid_entry attr_dir_stuff[] = {
  1556. REG("current", S_IRUGO|S_IWUGO, pid_attr),
  1557. REG("prev", S_IRUGO, pid_attr),
  1558. REG("exec", S_IRUGO|S_IWUGO, pid_attr),
  1559. REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
  1560. REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
  1561. REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
  1562. };
  1563. static int proc_attr_dir_readdir(struct file * filp,
  1564. void * dirent, filldir_t filldir)
  1565. {
  1566. return proc_pident_readdir(filp,dirent,filldir,
  1567. attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
  1568. }
  1569. static const struct file_operations proc_attr_dir_operations = {
  1570. .read = generic_read_dir,
  1571. .readdir = proc_attr_dir_readdir,
  1572. };
  1573. static struct dentry *proc_attr_dir_lookup(struct inode *dir,
  1574. struct dentry *dentry, struct nameidata *nd)
  1575. {
  1576. return proc_pident_lookup(dir, dentry,
  1577. attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
  1578. }
  1579. static const struct inode_operations proc_attr_dir_inode_operations = {
  1580. .lookup = proc_attr_dir_lookup,
  1581. .getattr = pid_getattr,
  1582. .setattr = proc_setattr,
  1583. };
  1584. #endif
  1585. /*
  1586. * /proc/self:
  1587. */
  1588. static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
  1589. int buflen)
  1590. {
  1591. char tmp[PROC_NUMBUF];
  1592. sprintf(tmp, "%d", current->tgid);
  1593. return vfs_readlink(dentry,buffer,buflen,tmp);
  1594. }
  1595. static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
  1596. {
  1597. char tmp[PROC_NUMBUF];
  1598. sprintf(tmp, "%d", current->tgid);
  1599. return ERR_PTR(vfs_follow_link(nd,tmp));
  1600. }
  1601. static const struct inode_operations proc_self_inode_operations = {
  1602. .readlink = proc_self_readlink,
  1603. .follow_link = proc_self_follow_link,
  1604. };
  1605. /*
  1606. * proc base
  1607. *
  1608. * These are the directory entries in the root directory of /proc
  1609. * that properly belong to the /proc filesystem, as they describe
  1610. * describe something that is process related.
  1611. */
  1612. static const struct pid_entry proc_base_stuff[] = {
  1613. NOD("self", S_IFLNK|S_IRWXUGO,
  1614. &proc_self_inode_operations, NULL, {}),
  1615. };
  1616. /*
  1617. * Exceptional case: normally we are not allowed to unhash a busy
  1618. * directory. In this case, however, we can do it - no aliasing problems
  1619. * due to the way we treat inodes.
  1620. */
  1621. static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
  1622. {
  1623. struct inode *inode = dentry->d_inode;
  1624. struct task_struct *task = get_proc_task(inode);
  1625. if (task) {
  1626. put_task_struct(task);
  1627. return 1;
  1628. }
  1629. d_drop(dentry);
  1630. return 0;
  1631. }
  1632. static struct dentry_operations proc_base_dentry_operations =
  1633. {
  1634. .d_revalidate = proc_base_revalidate,
  1635. .d_delete = pid_delete_dentry,
  1636. };
  1637. static struct dentry *proc_base_instantiate(struct inode *dir,
  1638. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1639. {
  1640. const struct pid_entry *p = ptr;
  1641. struct inode *inode;
  1642. struct proc_inode *ei;
  1643. struct dentry *error = ERR_PTR(-EINVAL);
  1644. /* Allocate the inode */
  1645. error = ERR_PTR(-ENOMEM);
  1646. inode = new_inode(dir->i_sb);
  1647. if (!inode)
  1648. goto out;
  1649. /* Initialize the inode */
  1650. ei = PROC_I(inode);
  1651. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1652. /*
  1653. * grab the reference to the task.
  1654. */
  1655. ei->pid = get_task_pid(task, PIDTYPE_PID);
  1656. if (!ei->pid)
  1657. goto out_iput;
  1658. inode->i_uid = 0;
  1659. inode->i_gid = 0;
  1660. inode->i_mode = p->mode;
  1661. if (S_ISDIR(inode->i_mode))
  1662. inode->i_nlink = 2;
  1663. if (S_ISLNK(inode->i_mode))
  1664. inode->i_size = 64;
  1665. if (p->iop)
  1666. inode->i_op = p->iop;
  1667. if (p->fop)
  1668. inode->i_fop = p->fop;
  1669. ei->op = p->op;
  1670. dentry->d_op = &proc_base_dentry_operations;
  1671. d_add(dentry, inode);
  1672. error = NULL;
  1673. out:
  1674. return error;
  1675. out_iput:
  1676. iput(inode);
  1677. goto out;
  1678. }
  1679. static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
  1680. {
  1681. struct dentry *error;
  1682. struct task_struct *task = get_proc_task(dir);
  1683. const struct pid_entry *p, *last;
  1684. error = ERR_PTR(-ENOENT);
  1685. if (!task)
  1686. goto out_no_task;
  1687. /* Lookup the directory entry */
  1688. last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
  1689. for (p = proc_base_stuff; p <= last; p++) {
  1690. if (p->len != dentry->d_name.len)
  1691. continue;
  1692. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1693. break;
  1694. }
  1695. if (p > last)
  1696. goto out;
  1697. error = proc_base_instantiate(dir, dentry, task, p);
  1698. out:
  1699. put_task_struct(task);
  1700. out_no_task:
  1701. return error;
  1702. }
  1703. static int proc_base_fill_cache(struct file *filp, void *dirent,
  1704. filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
  1705. {
  1706. return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
  1707. proc_base_instantiate, task, p);
  1708. }
  1709. #ifdef CONFIG_TASK_IO_ACCOUNTING
  1710. static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
  1711. {
  1712. return sprintf(buffer,
  1713. #ifdef CONFIG_TASK_XACCT
  1714. "rchar: %llu\n"
  1715. "wchar: %llu\n"
  1716. "syscr: %llu\n"
  1717. "syscw: %llu\n"
  1718. #endif
  1719. "read_bytes: %llu\n"
  1720. "write_bytes: %llu\n"
  1721. "cancelled_write_bytes: %llu\n",
  1722. #ifdef CONFIG_TASK_XACCT
  1723. (unsigned long long)task->rchar,
  1724. (unsigned long long)task->wchar,
  1725. (unsigned long long)task->syscr,
  1726. (unsigned long long)task->syscw,
  1727. #endif
  1728. (unsigned long long)task->ioac.read_bytes,
  1729. (unsigned long long)task->ioac.write_bytes,
  1730. (unsigned long long)task->ioac.cancelled_write_bytes);
  1731. }
  1732. #endif
  1733. /*
  1734. * Thread groups
  1735. */
  1736. static const struct file_operations proc_task_operations;
  1737. static const struct inode_operations proc_task_inode_operations;
  1738. static const struct pid_entry tgid_base_stuff[] = {
  1739. DIR("task", S_IRUGO|S_IXUGO, task),
  1740. DIR("fd", S_IRUSR|S_IXUSR, fd),
  1741. DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
  1742. INF("environ", S_IRUSR, pid_environ),
  1743. INF("auxv", S_IRUSR, pid_auxv),
  1744. INF("status", S_IRUGO, pid_status),
  1745. INF("cmdline", S_IRUGO, pid_cmdline),
  1746. INF("stat", S_IRUGO, tgid_stat),
  1747. INF("statm", S_IRUGO, pid_statm),
  1748. REG("maps", S_IRUGO, maps),
  1749. #ifdef CONFIG_NUMA
  1750. REG("numa_maps", S_IRUGO, numa_maps),
  1751. #endif
  1752. REG("mem", S_IRUSR|S_IWUSR, mem),
  1753. #ifdef CONFIG_SECCOMP
  1754. REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
  1755. #endif
  1756. LNK("cwd", cwd),
  1757. LNK("root", root),
  1758. LNK("exe", exe),
  1759. REG("mounts", S_IRUGO, mounts),
  1760. REG("mountstats", S_IRUSR, mountstats),
  1761. #ifdef CONFIG_MMU
  1762. REG("clear_refs", S_IWUSR, clear_refs),
  1763. REG("smaps", S_IRUGO, smaps),
  1764. #endif
  1765. #ifdef CONFIG_SECURITY
  1766. DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
  1767. #endif
  1768. #ifdef CONFIG_KALLSYMS
  1769. INF("wchan", S_IRUGO, pid_wchan),
  1770. #endif
  1771. #ifdef CONFIG_SCHEDSTATS
  1772. INF("schedstat", S_IRUGO, pid_schedstat),
  1773. #endif
  1774. #ifdef CONFIG_CPUSETS
  1775. REG("cpuset", S_IRUGO, cpuset),
  1776. #endif
  1777. INF("oom_score", S_IRUGO, oom_score),
  1778. REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
  1779. #ifdef CONFIG_AUDITSYSCALL
  1780. REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
  1781. #endif
  1782. #ifdef CONFIG_FAULT_INJECTION
  1783. REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
  1784. #endif
  1785. #ifdef CONFIG_TASK_IO_ACCOUNTING
  1786. INF("io", S_IRUGO, pid_io_accounting),
  1787. #endif
  1788. };
  1789. static int proc_tgid_base_readdir(struct file * filp,
  1790. void * dirent, filldir_t filldir)
  1791. {
  1792. return proc_pident_readdir(filp,dirent,filldir,
  1793. tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
  1794. }
  1795. static const struct file_operations proc_tgid_base_operations = {
  1796. .read = generic_read_dir,
  1797. .readdir = proc_tgid_base_readdir,
  1798. };
  1799. static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1800. return proc_pident_lookup(dir, dentry,
  1801. tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
  1802. }
  1803. static const struct inode_operations proc_tgid_base_inode_operations = {
  1804. .lookup = proc_tgid_base_lookup,
  1805. .getattr = pid_getattr,
  1806. .setattr = proc_setattr,
  1807. };
  1808. /**
  1809. * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
  1810. *
  1811. * @task: task that should be flushed.
  1812. *
  1813. * Looks in the dcache for
  1814. * /proc/@pid
  1815. * /proc/@tgid/task/@pid
  1816. * if either directory is present flushes it and all of it'ts children
  1817. * from the dcache.
  1818. *
  1819. * It is safe and reasonable to cache /proc entries for a task until
  1820. * that task exits. After that they just clog up the dcache with
  1821. * useless entries, possibly causing useful dcache entries to be
  1822. * flushed instead. This routine is proved to flush those useless
  1823. * dcache entries at process exit time.
  1824. *
  1825. * NOTE: This routine is just an optimization so it does not guarantee
  1826. * that no dcache entries will exist at process exit time it
  1827. * just makes it very unlikely that any will persist.
  1828. */
  1829. void proc_flush_task(struct task_struct *task)
  1830. {
  1831. struct dentry *dentry, *leader, *dir;
  1832. char buf[PROC_NUMBUF];
  1833. struct qstr name;
  1834. name.name = buf;
  1835. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1836. dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1837. if (dentry) {
  1838. shrink_dcache_parent(dentry);
  1839. d_drop(dentry);
  1840. dput(dentry);
  1841. }
  1842. if (thread_group_leader(task))
  1843. goto out;
  1844. name.name = buf;
  1845. name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
  1846. leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1847. if (!leader)
  1848. goto out;
  1849. name.name = "task";
  1850. name.len = strlen(name.name);
  1851. dir = d_hash_and_lookup(leader, &name);
  1852. if (!dir)
  1853. goto out_put_leader;
  1854. name.name = buf;
  1855. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1856. dentry = d_hash_and_lookup(dir, &name);
  1857. if (dentry) {
  1858. shrink_dcache_parent(dentry);
  1859. d_drop(dentry);
  1860. dput(dentry);
  1861. }
  1862. dput(dir);
  1863. out_put_leader:
  1864. dput(leader);
  1865. out:
  1866. return;
  1867. }
  1868. static struct dentry *proc_pid_instantiate(struct inode *dir,
  1869. struct dentry * dentry,
  1870. struct task_struct *task, const void *ptr)
  1871. {
  1872. struct dentry *error = ERR_PTR(-ENOENT);
  1873. struct inode *inode;
  1874. inode = proc_pid_make_inode(dir->i_sb, task);
  1875. if (!inode)
  1876. goto out;
  1877. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1878. inode->i_op = &proc_tgid_base_inode_operations;
  1879. inode->i_fop = &proc_tgid_base_operations;
  1880. inode->i_flags|=S_IMMUTABLE;
  1881. inode->i_nlink = 5;
  1882. #ifdef CONFIG_SECURITY
  1883. inode->i_nlink += 1;
  1884. #endif
  1885. dentry->d_op = &pid_dentry_operations;
  1886. d_add(dentry, inode);
  1887. /* Close the race of the process dying before we return the dentry */
  1888. if (pid_revalidate(dentry, NULL))
  1889. error = NULL;
  1890. out:
  1891. return error;
  1892. }
  1893. struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1894. {
  1895. struct dentry *result = ERR_PTR(-ENOENT);
  1896. struct task_struct *task;
  1897. unsigned tgid;
  1898. result = proc_base_lookup(dir, dentry);
  1899. if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
  1900. goto out;
  1901. tgid = name_to_int(dentry);
  1902. if (tgid == ~0U)
  1903. goto out;
  1904. rcu_read_lock();
  1905. task = find_task_by_pid(tgid);
  1906. if (task)
  1907. get_task_struct(task);
  1908. rcu_read_unlock();
  1909. if (!task)
  1910. goto out;
  1911. result = proc_pid_instantiate(dir, dentry, task, NULL);
  1912. put_task_struct(task);
  1913. out:
  1914. return result;
  1915. }
  1916. /*
  1917. * Find the first task with tgid >= tgid
  1918. *
  1919. */
  1920. static struct task_struct *next_tgid(unsigned int tgid)
  1921. {
  1922. struct task_struct *task;
  1923. struct pid *pid;
  1924. rcu_read_lock();
  1925. retry:
  1926. task = NULL;
  1927. pid = find_ge_pid(tgid);
  1928. if (pid) {
  1929. tgid = pid->nr + 1;
  1930. task = pid_task(pid, PIDTYPE_PID);
  1931. /* What we to know is if the pid we have find is the
  1932. * pid of a thread_group_leader. Testing for task
  1933. * being a thread_group_leader is the obvious thing
  1934. * todo but there is a window when it fails, due to
  1935. * the pid transfer logic in de_thread.
  1936. *
  1937. * So we perform the straight forward test of seeing
  1938. * if the pid we have found is the pid of a thread
  1939. * group leader, and don't worry if the task we have
  1940. * found doesn't happen to be a thread group leader.
  1941. * As we don't care in the case of readdir.
  1942. */
  1943. if (!task || !has_group_leader_pid(task))
  1944. goto retry;
  1945. get_task_struct(task);
  1946. }
  1947. rcu_read_unlock();
  1948. return task;
  1949. }
  1950. #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
  1951. static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  1952. struct task_struct *task, int tgid)
  1953. {
  1954. char name[PROC_NUMBUF];
  1955. int len = snprintf(name, sizeof(name), "%d", tgid);
  1956. return proc_fill_cache(filp, dirent, filldir, name, len,
  1957. proc_pid_instantiate, task, NULL);
  1958. }
  1959. /* for the /proc/ directory itself, after non-process stuff has been done */
  1960. int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
  1961. {
  1962. unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
  1963. struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
  1964. struct task_struct *task;
  1965. int tgid;
  1966. if (!reaper)
  1967. goto out_no_task;
  1968. for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
  1969. const struct pid_entry *p = &proc_base_stuff[nr];
  1970. if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
  1971. goto out;
  1972. }
  1973. tgid = filp->f_pos - TGID_OFFSET;
  1974. for (task = next_tgid(tgid);
  1975. task;
  1976. put_task_struct(task), task = next_tgid(tgid + 1)) {
  1977. tgid = task->pid;
  1978. filp->f_pos = tgid + TGID_OFFSET;
  1979. if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
  1980. put_task_struct(task);
  1981. goto out;
  1982. }
  1983. }
  1984. filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
  1985. out:
  1986. put_task_struct(reaper);
  1987. out_no_task:
  1988. return 0;
  1989. }
  1990. /*
  1991. * Tasks
  1992. */
  1993. static const struct pid_entry tid_base_stuff[] = {
  1994. DIR("fd", S_IRUSR|S_IXUSR, fd),
  1995. DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
  1996. INF("environ", S_IRUSR, pid_environ),
  1997. INF("auxv", S_IRUSR, pid_auxv),
  1998. INF("status", S_IRUGO, pid_status),
  1999. INF("cmdline", S_IRUGO, pid_cmdline),
  2000. INF("stat", S_IRUGO, tid_stat),
  2001. INF("statm", S_IRUGO, pid_statm),
  2002. REG("maps", S_IRUGO, maps),
  2003. #ifdef CONFIG_NUMA
  2004. REG("numa_maps", S_IRUGO, numa_maps),
  2005. #endif
  2006. REG("mem", S_IRUSR|S_IWUSR, mem),
  2007. #ifdef CONFIG_SECCOMP
  2008. REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
  2009. #endif
  2010. LNK("cwd", cwd),
  2011. LNK("root", root),
  2012. LNK("exe", exe),
  2013. REG("mounts", S_IRUGO, mounts),
  2014. #ifdef CONFIG_MMU
  2015. REG("clear_refs", S_IWUSR, clear_refs),
  2016. REG("smaps", S_IRUGO, smaps),
  2017. #endif
  2018. #ifdef CONFIG_SECURITY
  2019. DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
  2020. #endif
  2021. #ifdef CONFIG_KALLSYMS
  2022. INF("wchan", S_IRUGO, pid_wchan),
  2023. #endif
  2024. #ifdef CONFIG_SCHEDSTATS
  2025. INF("schedstat", S_IRUGO, pid_schedstat),
  2026. #endif
  2027. #ifdef CONFIG_CPUSETS
  2028. REG("cpuset", S_IRUGO, cpuset),
  2029. #endif
  2030. INF("oom_score", S_IRUGO, oom_score),
  2031. REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
  2032. #ifdef CONFIG_AUDITSYSCALL
  2033. REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
  2034. #endif
  2035. #ifdef CONFIG_FAULT_INJECTION
  2036. REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
  2037. #endif
  2038. };
  2039. static int proc_tid_base_readdir(struct file * filp,
  2040. void * dirent, filldir_t filldir)
  2041. {
  2042. return proc_pident_readdir(filp,dirent,filldir,
  2043. tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
  2044. }
  2045. static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  2046. return proc_pident_lookup(dir, dentry,
  2047. tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
  2048. }
  2049. static const struct file_operations proc_tid_base_operations = {
  2050. .read = generic_read_dir,
  2051. .readdir = proc_tid_base_readdir,
  2052. };
  2053. static const struct inode_operations proc_tid_base_inode_operations = {
  2054. .lookup = proc_tid_base_lookup,
  2055. .getattr = pid_getattr,
  2056. .setattr = proc_setattr,
  2057. };
  2058. static struct dentry *proc_task_instantiate(struct inode *dir,
  2059. struct dentry *dentry, struct task_struct *task, const void *ptr)
  2060. {
  2061. struct dentry *error = ERR_PTR(-ENOENT);
  2062. struct inode *inode;
  2063. inode = proc_pid_make_inode(dir->i_sb, task);
  2064. if (!inode)
  2065. goto out;
  2066. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  2067. inode->i_op = &proc_tid_base_inode_operations;
  2068. inode->i_fop = &proc_tid_base_operations;
  2069. inode->i_flags|=S_IMMUTABLE;
  2070. inode->i_nlink = 4;
  2071. #ifdef CONFIG_SECURITY
  2072. inode->i_nlink += 1;
  2073. #endif
  2074. dentry->d_op = &pid_dentry_operations;
  2075. d_add(dentry, inode);
  2076. /* Close the race of the process dying before we return the dentry */
  2077. if (pid_revalidate(dentry, NULL))
  2078. error = NULL;
  2079. out:
  2080. return error;
  2081. }
  2082. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  2083. {
  2084. struct dentry *result = ERR_PTR(-ENOENT);
  2085. struct task_struct *task;
  2086. struct task_struct *leader = get_proc_task(dir);
  2087. unsigned tid;
  2088. if (!leader)
  2089. goto out_no_task;
  2090. tid = name_to_int(dentry);
  2091. if (tid == ~0U)
  2092. goto out;
  2093. rcu_read_lock();
  2094. task = find_task_by_pid(tid);
  2095. if (task)
  2096. get_task_struct(task);
  2097. rcu_read_unlock();
  2098. if (!task)
  2099. goto out;
  2100. if (leader->tgid != task->tgid)
  2101. goto out_drop_task;
  2102. result = proc_task_instantiate(dir, dentry, task, NULL);
  2103. out_drop_task:
  2104. put_task_struct(task);
  2105. out:
  2106. put_task_struct(leader);
  2107. out_no_task:
  2108. return result;
  2109. }
  2110. /*
  2111. * Find the first tid of a thread group to return to user space.
  2112. *
  2113. * Usually this is just the thread group leader, but if the users
  2114. * buffer was too small or there was a seek into the middle of the
  2115. * directory we have more work todo.
  2116. *
  2117. * In the case of a short read we start with find_task_by_pid.
  2118. *
  2119. * In the case of a seek we start with the leader and walk nr
  2120. * threads past it.
  2121. */
  2122. static struct task_struct *first_tid(struct task_struct *leader,
  2123. int tid, int nr)
  2124. {
  2125. struct task_struct *pos;
  2126. rcu_read_lock();
  2127. /* Attempt to start with the pid of a thread */
  2128. if (tid && (nr > 0)) {
  2129. pos = find_task_by_pid(tid);
  2130. if (pos && (pos->group_leader == leader))
  2131. goto found;
  2132. }
  2133. /* If nr exceeds the number of threads there is nothing todo */
  2134. pos = NULL;
  2135. if (nr && nr >= get_nr_threads(leader))
  2136. goto out;
  2137. /* If we haven't found our starting place yet start
  2138. * with the leader and walk nr threads forward.
  2139. */
  2140. for (pos = leader; nr > 0; --nr) {
  2141. pos = next_thread(pos);
  2142. if (pos == leader) {
  2143. pos = NULL;
  2144. goto out;
  2145. }
  2146. }
  2147. found:
  2148. get_task_struct(pos);
  2149. out:
  2150. rcu_read_unlock();
  2151. return pos;
  2152. }
  2153. /*
  2154. * Find the next thread in the thread list.
  2155. * Return NULL if there is an error or no next thread.
  2156. *
  2157. * The reference to the input task_struct is released.
  2158. */
  2159. static struct task_struct *next_tid(struct task_struct *start)
  2160. {
  2161. struct task_struct *pos = NULL;
  2162. rcu_read_lock();
  2163. if (pid_alive(start)) {
  2164. pos = next_thread(start);
  2165. if (thread_group_leader(pos))
  2166. pos = NULL;
  2167. else
  2168. get_task_struct(pos);
  2169. }
  2170. rcu_read_unlock();
  2171. put_task_struct(start);
  2172. return pos;
  2173. }
  2174. static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  2175. struct task_struct *task, int tid)
  2176. {
  2177. char name[PROC_NUMBUF];
  2178. int len = snprintf(name, sizeof(name), "%d", tid);
  2179. return proc_fill_cache(filp, dirent, filldir, name, len,
  2180. proc_task_instantiate, task, NULL);
  2181. }
  2182. /* for the /proc/TGID/task/ directories */
  2183. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2184. {
  2185. struct dentry *dentry = filp->f_path.dentry;
  2186. struct inode *inode = dentry->d_inode;
  2187. struct task_struct *leader = NULL;
  2188. struct task_struct *task;
  2189. int retval = -ENOENT;
  2190. ino_t ino;
  2191. int tid;
  2192. unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
  2193. task = get_proc_task(inode);
  2194. if (!task)
  2195. goto out_no_task;
  2196. rcu_read_lock();
  2197. if (pid_alive(task)) {
  2198. leader = task->group_leader;
  2199. get_task_struct(leader);
  2200. }
  2201. rcu_read_unlock();
  2202. put_task_struct(task);
  2203. if (!leader)
  2204. goto out_no_task;
  2205. retval = 0;
  2206. switch (pos) {
  2207. case 0:
  2208. ino = inode->i_ino;
  2209. if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
  2210. goto out;
  2211. pos++;
  2212. /* fall through */
  2213. case 1:
  2214. ino = parent_ino(dentry);
  2215. if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
  2216. goto out;
  2217. pos++;
  2218. /* fall through */
  2219. }
  2220. /* f_version caches the tgid value that the last readdir call couldn't
  2221. * return. lseek aka telldir automagically resets f_version to 0.
  2222. */
  2223. tid = filp->f_version;
  2224. filp->f_version = 0;
  2225. for (task = first_tid(leader, tid, pos - 2);
  2226. task;
  2227. task = next_tid(task), pos++) {
  2228. tid = task->pid;
  2229. if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
  2230. /* returning this tgid failed, save it as the first
  2231. * pid for the next readir call */
  2232. filp->f_version = tid;
  2233. put_task_struct(task);
  2234. break;
  2235. }
  2236. }
  2237. out:
  2238. filp->f_pos = pos;
  2239. put_task_struct(leader);
  2240. out_no_task:
  2241. return retval;
  2242. }
  2243. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  2244. {
  2245. struct inode *inode = dentry->d_inode;
  2246. struct task_struct *p = get_proc_task(inode);
  2247. generic_fillattr(inode, stat);
  2248. if (p) {
  2249. rcu_read_lock();
  2250. stat->nlink += get_nr_threads(p);
  2251. rcu_read_unlock();
  2252. put_task_struct(p);
  2253. }
  2254. return 0;
  2255. }
  2256. static const struct inode_operations proc_task_inode_operations = {
  2257. .lookup = proc_task_lookup,
  2258. .getattr = proc_task_getattr,
  2259. .setattr = proc_setattr,
  2260. };
  2261. static const struct file_operations proc_task_operations = {
  2262. .read = generic_read_dir,
  2263. .readdir = proc_task_readdir,
  2264. };