base.c 67 KB

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