base.c 81 KB

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