base.c 68 KB

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