base.c 68 KB

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