base.c 65 KB

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