base.c 72 KB

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