base.c 72 KB

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