base.c 76 KB

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