base.c 82 KB

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