base.c 86 KB

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