base.c 77 KB

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