base.c 61 KB

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