base.c 86 KB

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