base.c 79 KB

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