base.c 88 KB

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