base.c 61 KB

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