base.c 81 KB

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