base.c 70 KB

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