base.c 76 KB

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