base.c 68 KB

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