base.c 72 KB

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