base.c 74 KB

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