base.c 81 KB

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