base.c 67 KB

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