base.c 75 KB

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