base.c 59 KB

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