base.c 81 KB

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