base.c 76 KB

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