base.c 66 KB

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