base.c 71 KB

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