base.c 75 KB

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