base.c 62 KB

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