base.c 79 KB

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