base.c 75 KB

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