base.c 76 KB

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