base.c 56 KB

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