base.c 86 KB

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