base.c 67 KB

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