base.c 73 KB

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