base.c 87 KB

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