base.c 77 KB

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