base.c 79 KB

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