base.c 67 KB

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