base.c 79 KB

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