base.c 82 KB

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