base.c 82 KB

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