base.c 86 KB

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