base.c 68 KB

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