base.c 87 KB

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