base.c 71 KB

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