base.c 67 KB

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