base.c 54 KB

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