base.c 81 KB

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