base.c 62 KB

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