base.c 76 KB

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