base.c 56 KB

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