base.c 68 KB

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