base.c 73 KB

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