base.c 87 KB

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