base.c 87 KB

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