base.c 54 KB

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