base.c 75 KB

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