base.c 57 KB

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