base.c 60 KB

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