base.c 76 KB

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