base.c 81 KB

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