base.c 54 KB

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