base.c 76 KB

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