base.c 80 KB

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