base.c 54 KB

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