base.c 70 KB

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