base.c 74 KB

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