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