base.c 81 KB

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