base.c 68 KB

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