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