base.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870
  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/init.h>
  55. #include <linux/capability.h>
  56. #include <linux/file.h>
  57. #include <linux/string.h>
  58. #include <linux/seq_file.h>
  59. #include <linux/namei.h>
  60. #include <linux/mnt_namespace.h>
  61. #include <linux/mm.h>
  62. #include <linux/rcupdate.h>
  63. #include <linux/kallsyms.h>
  64. #include <linux/resource.h>
  65. #include <linux/module.h>
  66. #include <linux/mount.h>
  67. #include <linux/security.h>
  68. #include <linux/ptrace.h>
  69. #include <linux/cgroup.h>
  70. #include <linux/cpuset.h>
  71. #include <linux/audit.h>
  72. #include <linux/poll.h>
  73. #include <linux/nsproxy.h>
  74. #include <linux/oom.h>
  75. #include <linux/elf.h>
  76. #include <linux/pid_namespace.h>
  77. #include "internal.h"
  78. /* NOTE:
  79. * Implementing inode permission operations in /proc is almost
  80. * certainly an error. Permission checks need to happen during
  81. * each system call not at open time. The reason is that most of
  82. * what we wish to check for permissions in /proc varies at runtime.
  83. *
  84. * The classic example of a problem is opening file descriptors
  85. * in /proc for a task before it execs a suid executable.
  86. */
  87. struct pid_entry {
  88. char *name;
  89. int len;
  90. mode_t mode;
  91. const struct inode_operations *iop;
  92. const struct file_operations *fop;
  93. union proc_op op;
  94. };
  95. #define NOD(NAME, MODE, IOP, FOP, OP) { \
  96. .name = (NAME), \
  97. .len = sizeof(NAME) - 1, \
  98. .mode = MODE, \
  99. .iop = IOP, \
  100. .fop = FOP, \
  101. .op = OP, \
  102. }
  103. #define DIR(NAME, MODE, OTYPE) \
  104. NOD(NAME, (S_IFDIR|(MODE)), \
  105. &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
  106. {} )
  107. #define LNK(NAME, OTYPE) \
  108. NOD(NAME, (S_IFLNK|S_IRWXUGO), \
  109. &proc_pid_link_inode_operations, NULL, \
  110. { .proc_get_link = &proc_##OTYPE##_link } )
  111. #define REG(NAME, MODE, OTYPE) \
  112. NOD(NAME, (S_IFREG|(MODE)), NULL, \
  113. &proc_##OTYPE##_operations, {})
  114. #define INF(NAME, MODE, OTYPE) \
  115. NOD(NAME, (S_IFREG|(MODE)), \
  116. NULL, &proc_info_file_operations, \
  117. { .proc_read = &proc_##OTYPE } )
  118. int maps_protect;
  119. EXPORT_SYMBOL(maps_protect);
  120. static struct fs_struct *get_fs_struct(struct task_struct *task)
  121. {
  122. struct fs_struct *fs;
  123. task_lock(task);
  124. fs = task->fs;
  125. if(fs)
  126. atomic_inc(&fs->count);
  127. task_unlock(task);
  128. return fs;
  129. }
  130. static int get_nr_threads(struct task_struct *tsk)
  131. {
  132. /* Must be called with the rcu_read_lock held */
  133. unsigned long flags;
  134. int count = 0;
  135. if (lock_task_sighand(tsk, &flags)) {
  136. count = atomic_read(&tsk->signal->count);
  137. unlock_task_sighand(tsk, &flags);
  138. }
  139. return count;
  140. }
  141. static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  142. {
  143. struct task_struct *task = get_proc_task(inode);
  144. struct fs_struct *fs = NULL;
  145. int result = -ENOENT;
  146. if (task) {
  147. fs = get_fs_struct(task);
  148. put_task_struct(task);
  149. }
  150. if (fs) {
  151. read_lock(&fs->lock);
  152. *mnt = mntget(fs->pwdmnt);
  153. *dentry = dget(fs->pwd);
  154. read_unlock(&fs->lock);
  155. result = 0;
  156. put_fs_struct(fs);
  157. }
  158. return result;
  159. }
  160. static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  161. {
  162. struct task_struct *task = get_proc_task(inode);
  163. struct fs_struct *fs = NULL;
  164. int result = -ENOENT;
  165. if (task) {
  166. fs = get_fs_struct(task);
  167. put_task_struct(task);
  168. }
  169. if (fs) {
  170. read_lock(&fs->lock);
  171. *mnt = mntget(fs->rootmnt);
  172. *dentry = dget(fs->root);
  173. read_unlock(&fs->lock);
  174. result = 0;
  175. put_fs_struct(fs);
  176. }
  177. return result;
  178. }
  179. #define MAY_PTRACE(task) \
  180. (task == current || \
  181. (task->parent == current && \
  182. (task->ptrace & PT_PTRACED) && \
  183. (task_is_stopped_or_traced(task)) && \
  184. security_ptrace(current,task) == 0))
  185. struct mm_struct *mm_for_maps(struct task_struct *task)
  186. {
  187. struct mm_struct *mm = get_task_mm(task);
  188. if (!mm)
  189. return NULL;
  190. down_read(&mm->mmap_sem);
  191. task_lock(task);
  192. if (task->mm != mm)
  193. goto out;
  194. if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
  195. goto out;
  196. task_unlock(task);
  197. return mm;
  198. out:
  199. task_unlock(task);
  200. up_read(&mm->mmap_sem);
  201. mmput(mm);
  202. return NULL;
  203. }
  204. static int proc_pid_cmdline(struct task_struct *task, char * buffer)
  205. {
  206. int res = 0;
  207. unsigned int len;
  208. struct mm_struct *mm = get_task_mm(task);
  209. if (!mm)
  210. goto out;
  211. if (!mm->arg_end)
  212. goto out_mm; /* Shh! No looking before we're done */
  213. len = mm->arg_end - mm->arg_start;
  214. if (len > PAGE_SIZE)
  215. len = PAGE_SIZE;
  216. res = access_process_vm(task, mm->arg_start, buffer, len, 0);
  217. // If the nul at the end of args has been overwritten, then
  218. // assume application is using setproctitle(3).
  219. if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
  220. len = strnlen(buffer, res);
  221. if (len < res) {
  222. res = len;
  223. } else {
  224. len = mm->env_end - mm->env_start;
  225. if (len > PAGE_SIZE - res)
  226. len = PAGE_SIZE - res;
  227. res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
  228. res = strnlen(buffer, res);
  229. }
  230. }
  231. out_mm:
  232. mmput(mm);
  233. out:
  234. return res;
  235. }
  236. static int proc_pid_auxv(struct task_struct *task, char *buffer)
  237. {
  238. int res = 0;
  239. struct mm_struct *mm = get_task_mm(task);
  240. if (mm) {
  241. unsigned int nwords = 0;
  242. do
  243. nwords += 2;
  244. while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
  245. res = nwords * sizeof(mm->saved_auxv[0]);
  246. if (res > PAGE_SIZE)
  247. res = PAGE_SIZE;
  248. memcpy(buffer, mm->saved_auxv, res);
  249. mmput(mm);
  250. }
  251. return res;
  252. }
  253. #ifdef CONFIG_KALLSYMS
  254. /*
  255. * Provides a wchan file via kallsyms in a proper one-value-per-file format.
  256. * Returns the resolved symbol. If that fails, simply return the address.
  257. */
  258. static int proc_pid_wchan(struct task_struct *task, char *buffer)
  259. {
  260. unsigned long wchan;
  261. char symname[KSYM_NAME_LEN];
  262. wchan = get_wchan(task);
  263. if (lookup_symbol_name(wchan, symname) < 0)
  264. return sprintf(buffer, "%lu", wchan);
  265. else
  266. return sprintf(buffer, "%s", symname);
  267. }
  268. #endif /* CONFIG_KALLSYMS */
  269. #ifdef CONFIG_SCHEDSTATS
  270. /*
  271. * Provides /proc/PID/schedstat
  272. */
  273. static int proc_pid_schedstat(struct task_struct *task, char *buffer)
  274. {
  275. return sprintf(buffer, "%llu %llu %lu\n",
  276. task->sched_info.cpu_time,
  277. task->sched_info.run_delay,
  278. task->sched_info.pcount);
  279. }
  280. #endif
  281. #ifdef CONFIG_LATENCYTOP
  282. static int lstats_show_proc(struct seq_file *m, void *v)
  283. {
  284. int i;
  285. struct task_struct *task = m->private;
  286. seq_puts(m, "Latency Top version : v0.1\n");
  287. for (i = 0; i < 32; i++) {
  288. if (task->latency_record[i].backtrace[0]) {
  289. int q;
  290. seq_printf(m, "%i %li %li ",
  291. task->latency_record[i].count,
  292. task->latency_record[i].time,
  293. task->latency_record[i].max);
  294. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  295. char sym[KSYM_NAME_LEN];
  296. char *c;
  297. if (!task->latency_record[i].backtrace[q])
  298. break;
  299. if (task->latency_record[i].backtrace[q] == ULONG_MAX)
  300. break;
  301. sprint_symbol(sym, task->latency_record[i].backtrace[q]);
  302. c = strchr(sym, '+');
  303. if (c)
  304. *c = 0;
  305. seq_printf(m, "%s ", sym);
  306. }
  307. seq_printf(m, "\n");
  308. }
  309. }
  310. return 0;
  311. }
  312. static int lstats_open(struct inode *inode, struct file *file)
  313. {
  314. int ret;
  315. struct seq_file *m;
  316. struct task_struct *task = get_proc_task(inode);
  317. ret = single_open(file, lstats_show_proc, NULL);
  318. if (!ret) {
  319. m = file->private_data;
  320. m->private = task;
  321. }
  322. return ret;
  323. }
  324. static ssize_t lstats_write(struct file *file, const char __user *buf,
  325. size_t count, loff_t *offs)
  326. {
  327. struct seq_file *m;
  328. struct task_struct *task;
  329. m = file->private_data;
  330. task = m->private;
  331. clear_all_latency_tracing(task);
  332. return count;
  333. }
  334. static const struct file_operations proc_lstats_operations = {
  335. .open = lstats_open,
  336. .read = seq_read,
  337. .write = lstats_write,
  338. .llseek = seq_lseek,
  339. .release = single_release,
  340. };
  341. #endif
  342. /* The badness from the OOM killer */
  343. unsigned long badness(struct task_struct *p, unsigned long uptime);
  344. static int proc_oom_score(struct task_struct *task, char *buffer)
  345. {
  346. unsigned long points;
  347. struct timespec uptime;
  348. do_posix_clock_monotonic_gettime(&uptime);
  349. read_lock(&tasklist_lock);
  350. points = badness(task, uptime.tv_sec);
  351. read_unlock(&tasklist_lock);
  352. return sprintf(buffer, "%lu\n", points);
  353. }
  354. struct limit_names {
  355. char *name;
  356. char *unit;
  357. };
  358. static const struct limit_names lnames[RLIM_NLIMITS] = {
  359. [RLIMIT_CPU] = {"Max cpu time", "ms"},
  360. [RLIMIT_FSIZE] = {"Max file size", "bytes"},
  361. [RLIMIT_DATA] = {"Max data size", "bytes"},
  362. [RLIMIT_STACK] = {"Max stack size", "bytes"},
  363. [RLIMIT_CORE] = {"Max core file size", "bytes"},
  364. [RLIMIT_RSS] = {"Max resident set", "bytes"},
  365. [RLIMIT_NPROC] = {"Max processes", "processes"},
  366. [RLIMIT_NOFILE] = {"Max open files", "files"},
  367. [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
  368. [RLIMIT_AS] = {"Max address space", "bytes"},
  369. [RLIMIT_LOCKS] = {"Max file locks", "locks"},
  370. [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
  371. [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
  372. [RLIMIT_NICE] = {"Max nice priority", NULL},
  373. [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
  374. };
  375. /* Display limits for a process */
  376. static int proc_pid_limits(struct task_struct *task, char *buffer)
  377. {
  378. unsigned int i;
  379. int count = 0;
  380. unsigned long flags;
  381. char *bufptr = buffer;
  382. struct rlimit rlim[RLIM_NLIMITS];
  383. rcu_read_lock();
  384. if (!lock_task_sighand(task,&flags)) {
  385. rcu_read_unlock();
  386. return 0;
  387. }
  388. memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
  389. unlock_task_sighand(task, &flags);
  390. rcu_read_unlock();
  391. /*
  392. * print the file header
  393. */
  394. count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
  395. "Limit", "Soft Limit", "Hard Limit", "Units");
  396. for (i = 0; i < RLIM_NLIMITS; i++) {
  397. if (rlim[i].rlim_cur == RLIM_INFINITY)
  398. count += sprintf(&bufptr[count], "%-25s %-20s ",
  399. lnames[i].name, "unlimited");
  400. else
  401. count += sprintf(&bufptr[count], "%-25s %-20lu ",
  402. lnames[i].name, rlim[i].rlim_cur);
  403. if (rlim[i].rlim_max == RLIM_INFINITY)
  404. count += sprintf(&bufptr[count], "%-20s ", "unlimited");
  405. else
  406. count += sprintf(&bufptr[count], "%-20lu ",
  407. rlim[i].rlim_max);
  408. if (lnames[i].unit)
  409. count += sprintf(&bufptr[count], "%-10s\n",
  410. lnames[i].unit);
  411. else
  412. count += sprintf(&bufptr[count], "\n");
  413. }
  414. return count;
  415. }
  416. /************************************************************************/
  417. /* Here the fs part begins */
  418. /************************************************************************/
  419. /* permission checks */
  420. static int proc_fd_access_allowed(struct inode *inode)
  421. {
  422. struct task_struct *task;
  423. int allowed = 0;
  424. /* Allow access to a task's file descriptors if it is us or we
  425. * may use ptrace attach to the process and find out that
  426. * information.
  427. */
  428. task = get_proc_task(inode);
  429. if (task) {
  430. allowed = ptrace_may_attach(task);
  431. put_task_struct(task);
  432. }
  433. return allowed;
  434. }
  435. static int proc_setattr(struct dentry *dentry, struct iattr *attr)
  436. {
  437. int error;
  438. struct inode *inode = dentry->d_inode;
  439. if (attr->ia_valid & ATTR_MODE)
  440. return -EPERM;
  441. error = inode_change_ok(inode, attr);
  442. if (!error)
  443. error = inode_setattr(inode, attr);
  444. return error;
  445. }
  446. static const struct inode_operations proc_def_inode_operations = {
  447. .setattr = proc_setattr,
  448. };
  449. extern struct seq_operations mounts_op;
  450. struct proc_mounts {
  451. struct seq_file m;
  452. int event;
  453. };
  454. static int mounts_open(struct inode *inode, struct file *file)
  455. {
  456. struct task_struct *task = get_proc_task(inode);
  457. struct nsproxy *nsp;
  458. struct mnt_namespace *ns = NULL;
  459. struct proc_mounts *p;
  460. int ret = -EINVAL;
  461. if (task) {
  462. rcu_read_lock();
  463. nsp = task_nsproxy(task);
  464. if (nsp) {
  465. ns = nsp->mnt_ns;
  466. if (ns)
  467. get_mnt_ns(ns);
  468. }
  469. rcu_read_unlock();
  470. put_task_struct(task);
  471. }
  472. if (ns) {
  473. ret = -ENOMEM;
  474. p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
  475. if (p) {
  476. file->private_data = &p->m;
  477. ret = seq_open(file, &mounts_op);
  478. if (!ret) {
  479. p->m.private = ns;
  480. p->event = ns->event;
  481. return 0;
  482. }
  483. kfree(p);
  484. }
  485. put_mnt_ns(ns);
  486. }
  487. return ret;
  488. }
  489. static int mounts_release(struct inode *inode, struct file *file)
  490. {
  491. struct seq_file *m = file->private_data;
  492. struct mnt_namespace *ns = m->private;
  493. put_mnt_ns(ns);
  494. return seq_release(inode, file);
  495. }
  496. static unsigned mounts_poll(struct file *file, poll_table *wait)
  497. {
  498. struct proc_mounts *p = file->private_data;
  499. struct mnt_namespace *ns = p->m.private;
  500. unsigned res = 0;
  501. poll_wait(file, &ns->poll, wait);
  502. spin_lock(&vfsmount_lock);
  503. if (p->event != ns->event) {
  504. p->event = ns->event;
  505. res = POLLERR;
  506. }
  507. spin_unlock(&vfsmount_lock);
  508. return res;
  509. }
  510. static const struct file_operations proc_mounts_operations = {
  511. .open = mounts_open,
  512. .read = seq_read,
  513. .llseek = seq_lseek,
  514. .release = mounts_release,
  515. .poll = mounts_poll,
  516. };
  517. extern struct seq_operations mountstats_op;
  518. static int mountstats_open(struct inode *inode, struct file *file)
  519. {
  520. int ret = seq_open(file, &mountstats_op);
  521. if (!ret) {
  522. struct seq_file *m = file->private_data;
  523. struct nsproxy *nsp;
  524. struct mnt_namespace *mnt_ns = NULL;
  525. struct task_struct *task = get_proc_task(inode);
  526. if (task) {
  527. rcu_read_lock();
  528. nsp = task_nsproxy(task);
  529. if (nsp) {
  530. mnt_ns = nsp->mnt_ns;
  531. if (mnt_ns)
  532. get_mnt_ns(mnt_ns);
  533. }
  534. rcu_read_unlock();
  535. put_task_struct(task);
  536. }
  537. if (mnt_ns)
  538. m->private = mnt_ns;
  539. else {
  540. seq_release(inode, file);
  541. ret = -EINVAL;
  542. }
  543. }
  544. return ret;
  545. }
  546. static const struct file_operations proc_mountstats_operations = {
  547. .open = mountstats_open,
  548. .read = seq_read,
  549. .llseek = seq_lseek,
  550. .release = mounts_release,
  551. };
  552. #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
  553. static ssize_t proc_info_read(struct file * file, char __user * buf,
  554. size_t count, loff_t *ppos)
  555. {
  556. struct inode * inode = file->f_path.dentry->d_inode;
  557. unsigned long page;
  558. ssize_t length;
  559. struct task_struct *task = get_proc_task(inode);
  560. length = -ESRCH;
  561. if (!task)
  562. goto out_no_task;
  563. if (count > PROC_BLOCK_SIZE)
  564. count = PROC_BLOCK_SIZE;
  565. length = -ENOMEM;
  566. if (!(page = __get_free_page(GFP_TEMPORARY)))
  567. goto out;
  568. length = PROC_I(inode)->op.proc_read(task, (char*)page);
  569. if (length >= 0)
  570. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  571. free_page(page);
  572. out:
  573. put_task_struct(task);
  574. out_no_task:
  575. return length;
  576. }
  577. static const struct file_operations proc_info_file_operations = {
  578. .read = proc_info_read,
  579. };
  580. static int mem_open(struct inode* inode, struct file* file)
  581. {
  582. file->private_data = (void*)((long)current->self_exec_id);
  583. return 0;
  584. }
  585. static ssize_t mem_read(struct file * file, char __user * buf,
  586. size_t count, loff_t *ppos)
  587. {
  588. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  589. char *page;
  590. unsigned long src = *ppos;
  591. int ret = -ESRCH;
  592. struct mm_struct *mm;
  593. if (!task)
  594. goto out_no_task;
  595. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  596. goto out;
  597. ret = -ENOMEM;
  598. page = (char *)__get_free_page(GFP_TEMPORARY);
  599. if (!page)
  600. goto out;
  601. ret = 0;
  602. mm = get_task_mm(task);
  603. if (!mm)
  604. goto out_free;
  605. ret = -EIO;
  606. if (file->private_data != (void*)((long)current->self_exec_id))
  607. goto out_put;
  608. ret = 0;
  609. while (count > 0) {
  610. int this_len, retval;
  611. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  612. retval = access_process_vm(task, src, page, this_len, 0);
  613. if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
  614. if (!ret)
  615. ret = -EIO;
  616. break;
  617. }
  618. if (copy_to_user(buf, page, retval)) {
  619. ret = -EFAULT;
  620. break;
  621. }
  622. ret += retval;
  623. src += retval;
  624. buf += retval;
  625. count -= retval;
  626. }
  627. *ppos = src;
  628. out_put:
  629. mmput(mm);
  630. out_free:
  631. free_page((unsigned long) page);
  632. out:
  633. put_task_struct(task);
  634. out_no_task:
  635. return ret;
  636. }
  637. #define mem_write NULL
  638. #ifndef mem_write
  639. /* This is a security hazard */
  640. static ssize_t mem_write(struct file * file, const char __user *buf,
  641. size_t count, loff_t *ppos)
  642. {
  643. int copied;
  644. char *page;
  645. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  646. unsigned long dst = *ppos;
  647. copied = -ESRCH;
  648. if (!task)
  649. goto out_no_task;
  650. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  651. goto out;
  652. copied = -ENOMEM;
  653. page = (char *)__get_free_page(GFP_TEMPORARY);
  654. if (!page)
  655. goto out;
  656. copied = 0;
  657. while (count > 0) {
  658. int this_len, retval;
  659. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  660. if (copy_from_user(page, buf, this_len)) {
  661. copied = -EFAULT;
  662. break;
  663. }
  664. retval = access_process_vm(task, dst, page, this_len, 1);
  665. if (!retval) {
  666. if (!copied)
  667. copied = -EIO;
  668. break;
  669. }
  670. copied += retval;
  671. buf += retval;
  672. dst += retval;
  673. count -= retval;
  674. }
  675. *ppos = dst;
  676. free_page((unsigned long) page);
  677. out:
  678. put_task_struct(task);
  679. out_no_task:
  680. return copied;
  681. }
  682. #endif
  683. loff_t mem_lseek(struct file *file, loff_t offset, int orig)
  684. {
  685. switch (orig) {
  686. case 0:
  687. file->f_pos = offset;
  688. break;
  689. case 1:
  690. file->f_pos += offset;
  691. break;
  692. default:
  693. return -EINVAL;
  694. }
  695. force_successful_syscall_return();
  696. return file->f_pos;
  697. }
  698. static const struct file_operations proc_mem_operations = {
  699. .llseek = mem_lseek,
  700. .read = mem_read,
  701. .write = mem_write,
  702. .open = mem_open,
  703. };
  704. static ssize_t environ_read(struct file *file, char __user *buf,
  705. size_t count, loff_t *ppos)
  706. {
  707. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  708. char *page;
  709. unsigned long src = *ppos;
  710. int ret = -ESRCH;
  711. struct mm_struct *mm;
  712. if (!task)
  713. goto out_no_task;
  714. if (!ptrace_may_attach(task))
  715. goto out;
  716. ret = -ENOMEM;
  717. page = (char *)__get_free_page(GFP_TEMPORARY);
  718. if (!page)
  719. goto out;
  720. ret = 0;
  721. mm = get_task_mm(task);
  722. if (!mm)
  723. goto out_free;
  724. while (count > 0) {
  725. int this_len, retval, max_len;
  726. this_len = mm->env_end - (mm->env_start + src);
  727. if (this_len <= 0)
  728. break;
  729. max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  730. this_len = (this_len > max_len) ? max_len : this_len;
  731. retval = access_process_vm(task, (mm->env_start + src),
  732. page, this_len, 0);
  733. if (retval <= 0) {
  734. ret = retval;
  735. break;
  736. }
  737. if (copy_to_user(buf, page, retval)) {
  738. ret = -EFAULT;
  739. break;
  740. }
  741. ret += retval;
  742. src += retval;
  743. buf += retval;
  744. count -= retval;
  745. }
  746. *ppos = src;
  747. mmput(mm);
  748. out_free:
  749. free_page((unsigned long) page);
  750. out:
  751. put_task_struct(task);
  752. out_no_task:
  753. return ret;
  754. }
  755. static const struct file_operations proc_environ_operations = {
  756. .read = environ_read,
  757. };
  758. static ssize_t oom_adjust_read(struct file *file, char __user *buf,
  759. size_t count, loff_t *ppos)
  760. {
  761. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  762. char buffer[PROC_NUMBUF];
  763. size_t len;
  764. int oom_adjust;
  765. if (!task)
  766. return -ESRCH;
  767. oom_adjust = task->oomkilladj;
  768. put_task_struct(task);
  769. len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
  770. return simple_read_from_buffer(buf, count, ppos, buffer, len);
  771. }
  772. static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
  773. size_t count, loff_t *ppos)
  774. {
  775. struct task_struct *task;
  776. char buffer[PROC_NUMBUF], *end;
  777. int oom_adjust;
  778. memset(buffer, 0, sizeof(buffer));
  779. if (count > sizeof(buffer) - 1)
  780. count = sizeof(buffer) - 1;
  781. if (copy_from_user(buffer, buf, count))
  782. return -EFAULT;
  783. oom_adjust = simple_strtol(buffer, &end, 0);
  784. if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
  785. oom_adjust != OOM_DISABLE)
  786. return -EINVAL;
  787. if (*end == '\n')
  788. end++;
  789. task = get_proc_task(file->f_path.dentry->d_inode);
  790. if (!task)
  791. return -ESRCH;
  792. if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
  793. put_task_struct(task);
  794. return -EACCES;
  795. }
  796. task->oomkilladj = oom_adjust;
  797. put_task_struct(task);
  798. if (end - buffer == 0)
  799. return -EIO;
  800. return end - buffer;
  801. }
  802. static const struct file_operations proc_oom_adjust_operations = {
  803. .read = oom_adjust_read,
  804. .write = oom_adjust_write,
  805. };
  806. #ifdef CONFIG_AUDITSYSCALL
  807. #define TMPBUFLEN 21
  808. static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
  809. size_t count, loff_t *ppos)
  810. {
  811. struct inode * inode = file->f_path.dentry->d_inode;
  812. struct task_struct *task = get_proc_task(inode);
  813. ssize_t length;
  814. char tmpbuf[TMPBUFLEN];
  815. if (!task)
  816. return -ESRCH;
  817. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  818. audit_get_loginuid(task));
  819. put_task_struct(task);
  820. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  821. }
  822. static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
  823. size_t count, loff_t *ppos)
  824. {
  825. struct inode * inode = file->f_path.dentry->d_inode;
  826. char *page, *tmp;
  827. ssize_t length;
  828. uid_t loginuid;
  829. if (!capable(CAP_AUDIT_CONTROL))
  830. return -EPERM;
  831. if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
  832. return -EPERM;
  833. if (count >= PAGE_SIZE)
  834. count = PAGE_SIZE - 1;
  835. if (*ppos != 0) {
  836. /* No partial writes. */
  837. return -EINVAL;
  838. }
  839. page = (char*)__get_free_page(GFP_TEMPORARY);
  840. if (!page)
  841. return -ENOMEM;
  842. length = -EFAULT;
  843. if (copy_from_user(page, buf, count))
  844. goto out_free_page;
  845. page[count] = '\0';
  846. loginuid = simple_strtoul(page, &tmp, 10);
  847. if (tmp == page) {
  848. length = -EINVAL;
  849. goto out_free_page;
  850. }
  851. length = audit_set_loginuid(current, loginuid);
  852. if (likely(length == 0))
  853. length = count;
  854. out_free_page:
  855. free_page((unsigned long) page);
  856. return length;
  857. }
  858. static const struct file_operations proc_loginuid_operations = {
  859. .read = proc_loginuid_read,
  860. .write = proc_loginuid_write,
  861. };
  862. #endif
  863. #ifdef CONFIG_FAULT_INJECTION
  864. static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
  865. size_t count, loff_t *ppos)
  866. {
  867. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  868. char buffer[PROC_NUMBUF];
  869. size_t len;
  870. int make_it_fail;
  871. if (!task)
  872. return -ESRCH;
  873. make_it_fail = task->make_it_fail;
  874. put_task_struct(task);
  875. len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
  876. return simple_read_from_buffer(buf, count, ppos, buffer, len);
  877. }
  878. static ssize_t proc_fault_inject_write(struct file * file,
  879. const char __user * buf, size_t count, loff_t *ppos)
  880. {
  881. struct task_struct *task;
  882. char buffer[PROC_NUMBUF], *end;
  883. int make_it_fail;
  884. if (!capable(CAP_SYS_RESOURCE))
  885. return -EPERM;
  886. memset(buffer, 0, sizeof(buffer));
  887. if (count > sizeof(buffer) - 1)
  888. count = sizeof(buffer) - 1;
  889. if (copy_from_user(buffer, buf, count))
  890. return -EFAULT;
  891. make_it_fail = simple_strtol(buffer, &end, 0);
  892. if (*end == '\n')
  893. end++;
  894. task = get_proc_task(file->f_dentry->d_inode);
  895. if (!task)
  896. return -ESRCH;
  897. task->make_it_fail = make_it_fail;
  898. put_task_struct(task);
  899. if (end - buffer == 0)
  900. return -EIO;
  901. return end - buffer;
  902. }
  903. static const struct file_operations proc_fault_inject_operations = {
  904. .read = proc_fault_inject_read,
  905. .write = proc_fault_inject_write,
  906. };
  907. #endif
  908. #ifdef CONFIG_SCHED_DEBUG
  909. /*
  910. * Print out various scheduling related per-task fields:
  911. */
  912. static int sched_show(struct seq_file *m, void *v)
  913. {
  914. struct inode *inode = m->private;
  915. struct task_struct *p;
  916. WARN_ON(!inode);
  917. p = get_proc_task(inode);
  918. if (!p)
  919. return -ESRCH;
  920. proc_sched_show_task(p, m);
  921. put_task_struct(p);
  922. return 0;
  923. }
  924. static ssize_t
  925. sched_write(struct file *file, const char __user *buf,
  926. size_t count, loff_t *offset)
  927. {
  928. struct inode *inode = file->f_path.dentry->d_inode;
  929. struct task_struct *p;
  930. WARN_ON(!inode);
  931. p = get_proc_task(inode);
  932. if (!p)
  933. return -ESRCH;
  934. proc_sched_set_task(p);
  935. put_task_struct(p);
  936. return count;
  937. }
  938. static int sched_open(struct inode *inode, struct file *filp)
  939. {
  940. int ret;
  941. ret = single_open(filp, sched_show, NULL);
  942. if (!ret) {
  943. struct seq_file *m = filp->private_data;
  944. m->private = inode;
  945. }
  946. return ret;
  947. }
  948. static const struct file_operations proc_pid_sched_operations = {
  949. .open = sched_open,
  950. .read = seq_read,
  951. .write = sched_write,
  952. .llseek = seq_lseek,
  953. .release = single_release,
  954. };
  955. #endif
  956. static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
  957. {
  958. struct inode *inode = dentry->d_inode;
  959. int error = -EACCES;
  960. /* We don't need a base pointer in the /proc filesystem */
  961. path_release(nd);
  962. /* Are we allowed to snoop on the tasks file descriptors? */
  963. if (!proc_fd_access_allowed(inode))
  964. goto out;
  965. error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
  966. nd->last_type = LAST_BIND;
  967. out:
  968. return ERR_PTR(error);
  969. }
  970. static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
  971. char __user *buffer, int buflen)
  972. {
  973. struct inode * inode;
  974. char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
  975. char *path;
  976. int len;
  977. if (!tmp)
  978. return -ENOMEM;
  979. inode = dentry->d_inode;
  980. path = d_path(dentry, mnt, tmp, PAGE_SIZE);
  981. len = PTR_ERR(path);
  982. if (IS_ERR(path))
  983. goto out;
  984. len = tmp + PAGE_SIZE - 1 - path;
  985. if (len > buflen)
  986. len = buflen;
  987. if (copy_to_user(buffer, path, len))
  988. len = -EFAULT;
  989. out:
  990. free_page((unsigned long)tmp);
  991. return len;
  992. }
  993. static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
  994. {
  995. int error = -EACCES;
  996. struct inode *inode = dentry->d_inode;
  997. struct dentry *de;
  998. struct vfsmount *mnt = NULL;
  999. /* Are we allowed to snoop on the tasks file descriptors? */
  1000. if (!proc_fd_access_allowed(inode))
  1001. goto out;
  1002. error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
  1003. if (error)
  1004. goto out;
  1005. error = do_proc_readlink(de, mnt, buffer, buflen);
  1006. dput(de);
  1007. mntput(mnt);
  1008. out:
  1009. return error;
  1010. }
  1011. static const struct inode_operations proc_pid_link_inode_operations = {
  1012. .readlink = proc_pid_readlink,
  1013. .follow_link = proc_pid_follow_link,
  1014. .setattr = proc_setattr,
  1015. };
  1016. /* building an inode */
  1017. static int task_dumpable(struct task_struct *task)
  1018. {
  1019. int dumpable = 0;
  1020. struct mm_struct *mm;
  1021. task_lock(task);
  1022. mm = task->mm;
  1023. if (mm)
  1024. dumpable = get_dumpable(mm);
  1025. task_unlock(task);
  1026. if(dumpable == 1)
  1027. return 1;
  1028. return 0;
  1029. }
  1030. static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
  1031. {
  1032. struct inode * inode;
  1033. struct proc_inode *ei;
  1034. /* We need a new inode */
  1035. inode = new_inode(sb);
  1036. if (!inode)
  1037. goto out;
  1038. /* Common stuff */
  1039. ei = PROC_I(inode);
  1040. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1041. inode->i_op = &proc_def_inode_operations;
  1042. /*
  1043. * grab the reference to task.
  1044. */
  1045. ei->pid = get_task_pid(task, PIDTYPE_PID);
  1046. if (!ei->pid)
  1047. goto out_unlock;
  1048. inode->i_uid = 0;
  1049. inode->i_gid = 0;
  1050. if (task_dumpable(task)) {
  1051. inode->i_uid = task->euid;
  1052. inode->i_gid = task->egid;
  1053. }
  1054. security_task_to_inode(task, inode);
  1055. out:
  1056. return inode;
  1057. out_unlock:
  1058. iput(inode);
  1059. return NULL;
  1060. }
  1061. static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  1062. {
  1063. struct inode *inode = dentry->d_inode;
  1064. struct task_struct *task;
  1065. generic_fillattr(inode, stat);
  1066. rcu_read_lock();
  1067. stat->uid = 0;
  1068. stat->gid = 0;
  1069. task = pid_task(proc_pid(inode), PIDTYPE_PID);
  1070. if (task) {
  1071. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1072. task_dumpable(task)) {
  1073. stat->uid = task->euid;
  1074. stat->gid = task->egid;
  1075. }
  1076. }
  1077. rcu_read_unlock();
  1078. return 0;
  1079. }
  1080. /* dentry stuff */
  1081. /*
  1082. * Exceptional case: normally we are not allowed to unhash a busy
  1083. * directory. In this case, however, we can do it - no aliasing problems
  1084. * due to the way we treat inodes.
  1085. *
  1086. * Rewrite the inode's ownerships here because the owning task may have
  1087. * performed a setuid(), etc.
  1088. *
  1089. * Before the /proc/pid/status file was created the only way to read
  1090. * the effective uid of a /process was to stat /proc/pid. Reading
  1091. * /proc/pid/status is slow enough that procps and other packages
  1092. * kept stating /proc/pid. To keep the rules in /proc simple I have
  1093. * made this apply to all per process world readable and executable
  1094. * directories.
  1095. */
  1096. static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
  1097. {
  1098. struct inode *inode = dentry->d_inode;
  1099. struct task_struct *task = get_proc_task(inode);
  1100. if (task) {
  1101. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1102. task_dumpable(task)) {
  1103. inode->i_uid = task->euid;
  1104. inode->i_gid = task->egid;
  1105. } else {
  1106. inode->i_uid = 0;
  1107. inode->i_gid = 0;
  1108. }
  1109. inode->i_mode &= ~(S_ISUID | S_ISGID);
  1110. security_task_to_inode(task, inode);
  1111. put_task_struct(task);
  1112. return 1;
  1113. }
  1114. d_drop(dentry);
  1115. return 0;
  1116. }
  1117. static int pid_delete_dentry(struct dentry * dentry)
  1118. {
  1119. /* Is the task we represent dead?
  1120. * If so, then don't put the dentry on the lru list,
  1121. * kill it immediately.
  1122. */
  1123. return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
  1124. }
  1125. static struct dentry_operations pid_dentry_operations =
  1126. {
  1127. .d_revalidate = pid_revalidate,
  1128. .d_delete = pid_delete_dentry,
  1129. };
  1130. /* Lookups */
  1131. typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
  1132. struct task_struct *, const void *);
  1133. /*
  1134. * Fill a directory entry.
  1135. *
  1136. * If possible create the dcache entry and derive our inode number and
  1137. * file type from dcache entry.
  1138. *
  1139. * Since all of the proc inode numbers are dynamically generated, the inode
  1140. * numbers do not exist until the inode is cache. This means creating the
  1141. * the dcache entry in readdir is necessary to keep the inode numbers
  1142. * reported by readdir in sync with the inode numbers reported
  1143. * by stat.
  1144. */
  1145. static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  1146. char *name, int len,
  1147. instantiate_t instantiate, struct task_struct *task, const void *ptr)
  1148. {
  1149. struct dentry *child, *dir = filp->f_path.dentry;
  1150. struct inode *inode;
  1151. struct qstr qname;
  1152. ino_t ino = 0;
  1153. unsigned type = DT_UNKNOWN;
  1154. qname.name = name;
  1155. qname.len = len;
  1156. qname.hash = full_name_hash(name, len);
  1157. child = d_lookup(dir, &qname);
  1158. if (!child) {
  1159. struct dentry *new;
  1160. new = d_alloc(dir, &qname);
  1161. if (new) {
  1162. child = instantiate(dir->d_inode, new, task, ptr);
  1163. if (child)
  1164. dput(new);
  1165. else
  1166. child = new;
  1167. }
  1168. }
  1169. if (!child || IS_ERR(child) || !child->d_inode)
  1170. goto end_instantiate;
  1171. inode = child->d_inode;
  1172. if (inode) {
  1173. ino = inode->i_ino;
  1174. type = inode->i_mode >> 12;
  1175. }
  1176. dput(child);
  1177. end_instantiate:
  1178. if (!ino)
  1179. ino = find_inode_number(dir, &qname);
  1180. if (!ino)
  1181. ino = 1;
  1182. return filldir(dirent, name, len, filp->f_pos, ino, type);
  1183. }
  1184. static unsigned name_to_int(struct dentry *dentry)
  1185. {
  1186. const char *name = dentry->d_name.name;
  1187. int len = dentry->d_name.len;
  1188. unsigned n = 0;
  1189. if (len > 1 && *name == '0')
  1190. goto out;
  1191. while (len-- > 0) {
  1192. unsigned c = *name++ - '0';
  1193. if (c > 9)
  1194. goto out;
  1195. if (n >= (~0U-9)/10)
  1196. goto out;
  1197. n *= 10;
  1198. n += c;
  1199. }
  1200. return n;
  1201. out:
  1202. return ~0U;
  1203. }
  1204. #define PROC_FDINFO_MAX 64
  1205. static int proc_fd_info(struct inode *inode, struct dentry **dentry,
  1206. struct vfsmount **mnt, char *info)
  1207. {
  1208. struct task_struct *task = get_proc_task(inode);
  1209. struct files_struct *files = NULL;
  1210. struct file *file;
  1211. int fd = proc_fd(inode);
  1212. if (task) {
  1213. files = get_files_struct(task);
  1214. put_task_struct(task);
  1215. }
  1216. if (files) {
  1217. /*
  1218. * We are not taking a ref to the file structure, so we must
  1219. * hold ->file_lock.
  1220. */
  1221. spin_lock(&files->file_lock);
  1222. file = fcheck_files(files, fd);
  1223. if (file) {
  1224. if (mnt)
  1225. *mnt = mntget(file->f_path.mnt);
  1226. if (dentry)
  1227. *dentry = dget(file->f_path.dentry);
  1228. if (info)
  1229. snprintf(info, PROC_FDINFO_MAX,
  1230. "pos:\t%lli\n"
  1231. "flags:\t0%o\n",
  1232. (long long) file->f_pos,
  1233. file->f_flags);
  1234. spin_unlock(&files->file_lock);
  1235. put_files_struct(files);
  1236. return 0;
  1237. }
  1238. spin_unlock(&files->file_lock);
  1239. put_files_struct(files);
  1240. }
  1241. return -ENOENT;
  1242. }
  1243. static int proc_fd_link(struct inode *inode, struct dentry **dentry,
  1244. struct vfsmount **mnt)
  1245. {
  1246. return proc_fd_info(inode, dentry, mnt, NULL);
  1247. }
  1248. static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
  1249. {
  1250. struct inode *inode = dentry->d_inode;
  1251. struct task_struct *task = get_proc_task(inode);
  1252. int fd = proc_fd(inode);
  1253. struct files_struct *files;
  1254. if (task) {
  1255. files = get_files_struct(task);
  1256. if (files) {
  1257. rcu_read_lock();
  1258. if (fcheck_files(files, fd)) {
  1259. rcu_read_unlock();
  1260. put_files_struct(files);
  1261. if (task_dumpable(task)) {
  1262. inode->i_uid = task->euid;
  1263. inode->i_gid = task->egid;
  1264. } else {
  1265. inode->i_uid = 0;
  1266. inode->i_gid = 0;
  1267. }
  1268. inode->i_mode &= ~(S_ISUID | S_ISGID);
  1269. security_task_to_inode(task, inode);
  1270. put_task_struct(task);
  1271. return 1;
  1272. }
  1273. rcu_read_unlock();
  1274. put_files_struct(files);
  1275. }
  1276. put_task_struct(task);
  1277. }
  1278. d_drop(dentry);
  1279. return 0;
  1280. }
  1281. static struct dentry_operations tid_fd_dentry_operations =
  1282. {
  1283. .d_revalidate = tid_fd_revalidate,
  1284. .d_delete = pid_delete_dentry,
  1285. };
  1286. static struct dentry *proc_fd_instantiate(struct inode *dir,
  1287. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1288. {
  1289. unsigned fd = *(const unsigned *)ptr;
  1290. struct file *file;
  1291. struct files_struct *files;
  1292. struct inode *inode;
  1293. struct proc_inode *ei;
  1294. struct dentry *error = ERR_PTR(-ENOENT);
  1295. inode = proc_pid_make_inode(dir->i_sb, task);
  1296. if (!inode)
  1297. goto out;
  1298. ei = PROC_I(inode);
  1299. ei->fd = fd;
  1300. files = get_files_struct(task);
  1301. if (!files)
  1302. goto out_iput;
  1303. inode->i_mode = S_IFLNK;
  1304. /*
  1305. * We are not taking a ref to the file structure, so we must
  1306. * hold ->file_lock.
  1307. */
  1308. spin_lock(&files->file_lock);
  1309. file = fcheck_files(files, fd);
  1310. if (!file)
  1311. goto out_unlock;
  1312. if (file->f_mode & 1)
  1313. inode->i_mode |= S_IRUSR | S_IXUSR;
  1314. if (file->f_mode & 2)
  1315. inode->i_mode |= S_IWUSR | S_IXUSR;
  1316. spin_unlock(&files->file_lock);
  1317. put_files_struct(files);
  1318. inode->i_op = &proc_pid_link_inode_operations;
  1319. inode->i_size = 64;
  1320. ei->op.proc_get_link = proc_fd_link;
  1321. dentry->d_op = &tid_fd_dentry_operations;
  1322. d_add(dentry, inode);
  1323. /* Close the race of the process dying before we return the dentry */
  1324. if (tid_fd_revalidate(dentry, NULL))
  1325. error = NULL;
  1326. out:
  1327. return error;
  1328. out_unlock:
  1329. spin_unlock(&files->file_lock);
  1330. put_files_struct(files);
  1331. out_iput:
  1332. iput(inode);
  1333. goto out;
  1334. }
  1335. static struct dentry *proc_lookupfd_common(struct inode *dir,
  1336. struct dentry *dentry,
  1337. instantiate_t instantiate)
  1338. {
  1339. struct task_struct *task = get_proc_task(dir);
  1340. unsigned fd = name_to_int(dentry);
  1341. struct dentry *result = ERR_PTR(-ENOENT);
  1342. if (!task)
  1343. goto out_no_task;
  1344. if (fd == ~0U)
  1345. goto out;
  1346. result = instantiate(dir, dentry, task, &fd);
  1347. out:
  1348. put_task_struct(task);
  1349. out_no_task:
  1350. return result;
  1351. }
  1352. static int proc_readfd_common(struct file * filp, void * dirent,
  1353. filldir_t filldir, instantiate_t instantiate)
  1354. {
  1355. struct dentry *dentry = filp->f_path.dentry;
  1356. struct inode *inode = dentry->d_inode;
  1357. struct task_struct *p = get_proc_task(inode);
  1358. unsigned int fd, ino;
  1359. int retval;
  1360. struct files_struct * files;
  1361. struct fdtable *fdt;
  1362. retval = -ENOENT;
  1363. if (!p)
  1364. goto out_no_task;
  1365. retval = 0;
  1366. fd = filp->f_pos;
  1367. switch (fd) {
  1368. case 0:
  1369. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  1370. goto out;
  1371. filp->f_pos++;
  1372. case 1:
  1373. ino = parent_ino(dentry);
  1374. if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
  1375. goto out;
  1376. filp->f_pos++;
  1377. default:
  1378. files = get_files_struct(p);
  1379. if (!files)
  1380. goto out;
  1381. rcu_read_lock();
  1382. fdt = files_fdtable(files);
  1383. for (fd = filp->f_pos-2;
  1384. fd < fdt->max_fds;
  1385. fd++, filp->f_pos++) {
  1386. char name[PROC_NUMBUF];
  1387. int len;
  1388. if (!fcheck_files(files, fd))
  1389. continue;
  1390. rcu_read_unlock();
  1391. len = snprintf(name, sizeof(name), "%d", fd);
  1392. if (proc_fill_cache(filp, dirent, filldir,
  1393. name, len, instantiate,
  1394. p, &fd) < 0) {
  1395. rcu_read_lock();
  1396. break;
  1397. }
  1398. rcu_read_lock();
  1399. }
  1400. rcu_read_unlock();
  1401. put_files_struct(files);
  1402. }
  1403. out:
  1404. put_task_struct(p);
  1405. out_no_task:
  1406. return retval;
  1407. }
  1408. static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
  1409. struct nameidata *nd)
  1410. {
  1411. return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
  1412. }
  1413. static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
  1414. {
  1415. return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
  1416. }
  1417. static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
  1418. size_t len, loff_t *ppos)
  1419. {
  1420. char tmp[PROC_FDINFO_MAX];
  1421. int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
  1422. if (!err)
  1423. err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
  1424. return err;
  1425. }
  1426. static const struct file_operations proc_fdinfo_file_operations = {
  1427. .open = nonseekable_open,
  1428. .read = proc_fdinfo_read,
  1429. };
  1430. static const struct file_operations proc_fd_operations = {
  1431. .read = generic_read_dir,
  1432. .readdir = proc_readfd,
  1433. };
  1434. /*
  1435. * /proc/pid/fd needs a special permission handler so that a process can still
  1436. * access /proc/self/fd after it has executed a setuid().
  1437. */
  1438. static int proc_fd_permission(struct inode *inode, int mask,
  1439. struct nameidata *nd)
  1440. {
  1441. int rv;
  1442. rv = generic_permission(inode, mask, NULL);
  1443. if (rv == 0)
  1444. return 0;
  1445. if (task_pid(current) == proc_pid(inode))
  1446. rv = 0;
  1447. return rv;
  1448. }
  1449. /*
  1450. * proc directories can do almost nothing..
  1451. */
  1452. static const struct inode_operations proc_fd_inode_operations = {
  1453. .lookup = proc_lookupfd,
  1454. .permission = proc_fd_permission,
  1455. .setattr = proc_setattr,
  1456. };
  1457. static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
  1458. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1459. {
  1460. unsigned fd = *(unsigned *)ptr;
  1461. struct inode *inode;
  1462. struct proc_inode *ei;
  1463. struct dentry *error = ERR_PTR(-ENOENT);
  1464. inode = proc_pid_make_inode(dir->i_sb, task);
  1465. if (!inode)
  1466. goto out;
  1467. ei = PROC_I(inode);
  1468. ei->fd = fd;
  1469. inode->i_mode = S_IFREG | S_IRUSR;
  1470. inode->i_fop = &proc_fdinfo_file_operations;
  1471. dentry->d_op = &tid_fd_dentry_operations;
  1472. d_add(dentry, inode);
  1473. /* Close the race of the process dying before we return the dentry */
  1474. if (tid_fd_revalidate(dentry, NULL))
  1475. error = NULL;
  1476. out:
  1477. return error;
  1478. }
  1479. static struct dentry *proc_lookupfdinfo(struct inode *dir,
  1480. struct dentry *dentry,
  1481. struct nameidata *nd)
  1482. {
  1483. return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
  1484. }
  1485. static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
  1486. {
  1487. return proc_readfd_common(filp, dirent, filldir,
  1488. proc_fdinfo_instantiate);
  1489. }
  1490. static const struct file_operations proc_fdinfo_operations = {
  1491. .read = generic_read_dir,
  1492. .readdir = proc_readfdinfo,
  1493. };
  1494. /*
  1495. * proc directories can do almost nothing..
  1496. */
  1497. static const struct inode_operations proc_fdinfo_inode_operations = {
  1498. .lookup = proc_lookupfdinfo,
  1499. .setattr = proc_setattr,
  1500. };
  1501. static struct dentry *proc_pident_instantiate(struct inode *dir,
  1502. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1503. {
  1504. const struct pid_entry *p = ptr;
  1505. struct inode *inode;
  1506. struct proc_inode *ei;
  1507. struct dentry *error = ERR_PTR(-EINVAL);
  1508. inode = proc_pid_make_inode(dir->i_sb, task);
  1509. if (!inode)
  1510. goto out;
  1511. ei = PROC_I(inode);
  1512. inode->i_mode = p->mode;
  1513. if (S_ISDIR(inode->i_mode))
  1514. inode->i_nlink = 2; /* Use getattr to fix if necessary */
  1515. if (p->iop)
  1516. inode->i_op = p->iop;
  1517. if (p->fop)
  1518. inode->i_fop = p->fop;
  1519. ei->op = p->op;
  1520. dentry->d_op = &pid_dentry_operations;
  1521. d_add(dentry, inode);
  1522. /* Close the race of the process dying before we return the dentry */
  1523. if (pid_revalidate(dentry, NULL))
  1524. error = NULL;
  1525. out:
  1526. return error;
  1527. }
  1528. static struct dentry *proc_pident_lookup(struct inode *dir,
  1529. struct dentry *dentry,
  1530. const struct pid_entry *ents,
  1531. unsigned int nents)
  1532. {
  1533. struct inode *inode;
  1534. struct dentry *error;
  1535. struct task_struct *task = get_proc_task(dir);
  1536. const struct pid_entry *p, *last;
  1537. error = ERR_PTR(-ENOENT);
  1538. inode = NULL;
  1539. if (!task)
  1540. goto out_no_task;
  1541. /*
  1542. * Yes, it does not scale. And it should not. Don't add
  1543. * new entries into /proc/<tgid>/ without very good reasons.
  1544. */
  1545. last = &ents[nents - 1];
  1546. for (p = ents; p <= last; p++) {
  1547. if (p->len != dentry->d_name.len)
  1548. continue;
  1549. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1550. break;
  1551. }
  1552. if (p > last)
  1553. goto out;
  1554. error = proc_pident_instantiate(dir, dentry, task, p);
  1555. out:
  1556. put_task_struct(task);
  1557. out_no_task:
  1558. return error;
  1559. }
  1560. static int proc_pident_fill_cache(struct file *filp, void *dirent,
  1561. filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
  1562. {
  1563. return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
  1564. proc_pident_instantiate, task, p);
  1565. }
  1566. static int proc_pident_readdir(struct file *filp,
  1567. void *dirent, filldir_t filldir,
  1568. const struct pid_entry *ents, unsigned int nents)
  1569. {
  1570. int i;
  1571. struct dentry *dentry = filp->f_path.dentry;
  1572. struct inode *inode = dentry->d_inode;
  1573. struct task_struct *task = get_proc_task(inode);
  1574. const struct pid_entry *p, *last;
  1575. ino_t ino;
  1576. int ret;
  1577. ret = -ENOENT;
  1578. if (!task)
  1579. goto out_no_task;
  1580. ret = 0;
  1581. i = filp->f_pos;
  1582. switch (i) {
  1583. case 0:
  1584. ino = inode->i_ino;
  1585. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  1586. goto out;
  1587. i++;
  1588. filp->f_pos++;
  1589. /* fall through */
  1590. case 1:
  1591. ino = parent_ino(dentry);
  1592. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  1593. goto out;
  1594. i++;
  1595. filp->f_pos++;
  1596. /* fall through */
  1597. default:
  1598. i -= 2;
  1599. if (i >= nents) {
  1600. ret = 1;
  1601. goto out;
  1602. }
  1603. p = ents + i;
  1604. last = &ents[nents - 1];
  1605. while (p <= last) {
  1606. if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
  1607. goto out;
  1608. filp->f_pos++;
  1609. p++;
  1610. }
  1611. }
  1612. ret = 1;
  1613. out:
  1614. put_task_struct(task);
  1615. out_no_task:
  1616. return ret;
  1617. }
  1618. #ifdef CONFIG_SECURITY
  1619. static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
  1620. size_t count, loff_t *ppos)
  1621. {
  1622. struct inode * inode = file->f_path.dentry->d_inode;
  1623. char *p = NULL;
  1624. ssize_t length;
  1625. struct task_struct *task = get_proc_task(inode);
  1626. if (!task)
  1627. return -ESRCH;
  1628. length = security_getprocattr(task,
  1629. (char*)file->f_path.dentry->d_name.name,
  1630. &p);
  1631. put_task_struct(task);
  1632. if (length > 0)
  1633. length = simple_read_from_buffer(buf, count, ppos, p, length);
  1634. kfree(p);
  1635. return length;
  1636. }
  1637. static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
  1638. size_t count, loff_t *ppos)
  1639. {
  1640. struct inode * inode = file->f_path.dentry->d_inode;
  1641. char *page;
  1642. ssize_t length;
  1643. struct task_struct *task = get_proc_task(inode);
  1644. length = -ESRCH;
  1645. if (!task)
  1646. goto out_no_task;
  1647. if (count > PAGE_SIZE)
  1648. count = PAGE_SIZE;
  1649. /* No partial writes. */
  1650. length = -EINVAL;
  1651. if (*ppos != 0)
  1652. goto out;
  1653. length = -ENOMEM;
  1654. page = (char*)__get_free_page(GFP_TEMPORARY);
  1655. if (!page)
  1656. goto out;
  1657. length = -EFAULT;
  1658. if (copy_from_user(page, buf, count))
  1659. goto out_free;
  1660. length = security_setprocattr(task,
  1661. (char*)file->f_path.dentry->d_name.name,
  1662. (void*)page, count);
  1663. out_free:
  1664. free_page((unsigned long) page);
  1665. out:
  1666. put_task_struct(task);
  1667. out_no_task:
  1668. return length;
  1669. }
  1670. static const struct file_operations proc_pid_attr_operations = {
  1671. .read = proc_pid_attr_read,
  1672. .write = proc_pid_attr_write,
  1673. };
  1674. static const struct pid_entry attr_dir_stuff[] = {
  1675. REG("current", S_IRUGO|S_IWUGO, pid_attr),
  1676. REG("prev", S_IRUGO, pid_attr),
  1677. REG("exec", S_IRUGO|S_IWUGO, pid_attr),
  1678. REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
  1679. REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
  1680. REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
  1681. };
  1682. static int proc_attr_dir_readdir(struct file * filp,
  1683. void * dirent, filldir_t filldir)
  1684. {
  1685. return proc_pident_readdir(filp,dirent,filldir,
  1686. attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
  1687. }
  1688. static const struct file_operations proc_attr_dir_operations = {
  1689. .read = generic_read_dir,
  1690. .readdir = proc_attr_dir_readdir,
  1691. };
  1692. static struct dentry *proc_attr_dir_lookup(struct inode *dir,
  1693. struct dentry *dentry, struct nameidata *nd)
  1694. {
  1695. return proc_pident_lookup(dir, dentry,
  1696. attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
  1697. }
  1698. static const struct inode_operations proc_attr_dir_inode_operations = {
  1699. .lookup = proc_attr_dir_lookup,
  1700. .getattr = pid_getattr,
  1701. .setattr = proc_setattr,
  1702. };
  1703. #endif
  1704. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  1705. static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
  1706. size_t count, loff_t *ppos)
  1707. {
  1708. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  1709. struct mm_struct *mm;
  1710. char buffer[PROC_NUMBUF];
  1711. size_t len;
  1712. int ret;
  1713. if (!task)
  1714. return -ESRCH;
  1715. ret = 0;
  1716. mm = get_task_mm(task);
  1717. if (mm) {
  1718. len = snprintf(buffer, sizeof(buffer), "%08lx\n",
  1719. ((mm->flags & MMF_DUMP_FILTER_MASK) >>
  1720. MMF_DUMP_FILTER_SHIFT));
  1721. mmput(mm);
  1722. ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
  1723. }
  1724. put_task_struct(task);
  1725. return ret;
  1726. }
  1727. static ssize_t proc_coredump_filter_write(struct file *file,
  1728. const char __user *buf,
  1729. size_t count,
  1730. loff_t *ppos)
  1731. {
  1732. struct task_struct *task;
  1733. struct mm_struct *mm;
  1734. char buffer[PROC_NUMBUF], *end;
  1735. unsigned int val;
  1736. int ret;
  1737. int i;
  1738. unsigned long mask;
  1739. ret = -EFAULT;
  1740. memset(buffer, 0, sizeof(buffer));
  1741. if (count > sizeof(buffer) - 1)
  1742. count = sizeof(buffer) - 1;
  1743. if (copy_from_user(buffer, buf, count))
  1744. goto out_no_task;
  1745. ret = -EINVAL;
  1746. val = (unsigned int)simple_strtoul(buffer, &end, 0);
  1747. if (*end == '\n')
  1748. end++;
  1749. if (end - buffer == 0)
  1750. goto out_no_task;
  1751. ret = -ESRCH;
  1752. task = get_proc_task(file->f_dentry->d_inode);
  1753. if (!task)
  1754. goto out_no_task;
  1755. ret = end - buffer;
  1756. mm = get_task_mm(task);
  1757. if (!mm)
  1758. goto out_no_mm;
  1759. for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
  1760. if (val & mask)
  1761. set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
  1762. else
  1763. clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
  1764. }
  1765. mmput(mm);
  1766. out_no_mm:
  1767. put_task_struct(task);
  1768. out_no_task:
  1769. return ret;
  1770. }
  1771. static const struct file_operations proc_coredump_filter_operations = {
  1772. .read = proc_coredump_filter_read,
  1773. .write = proc_coredump_filter_write,
  1774. };
  1775. #endif
  1776. /*
  1777. * /proc/self:
  1778. */
  1779. static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
  1780. int buflen)
  1781. {
  1782. char tmp[PROC_NUMBUF];
  1783. sprintf(tmp, "%d", task_tgid_vnr(current));
  1784. return vfs_readlink(dentry,buffer,buflen,tmp);
  1785. }
  1786. static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
  1787. {
  1788. char tmp[PROC_NUMBUF];
  1789. sprintf(tmp, "%d", task_tgid_vnr(current));
  1790. return ERR_PTR(vfs_follow_link(nd,tmp));
  1791. }
  1792. static const struct inode_operations proc_self_inode_operations = {
  1793. .readlink = proc_self_readlink,
  1794. .follow_link = proc_self_follow_link,
  1795. };
  1796. /*
  1797. * proc base
  1798. *
  1799. * These are the directory entries in the root directory of /proc
  1800. * that properly belong to the /proc filesystem, as they describe
  1801. * describe something that is process related.
  1802. */
  1803. static const struct pid_entry proc_base_stuff[] = {
  1804. NOD("self", S_IFLNK|S_IRWXUGO,
  1805. &proc_self_inode_operations, NULL, {}),
  1806. };
  1807. /*
  1808. * Exceptional case: normally we are not allowed to unhash a busy
  1809. * directory. In this case, however, we can do it - no aliasing problems
  1810. * due to the way we treat inodes.
  1811. */
  1812. static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
  1813. {
  1814. struct inode *inode = dentry->d_inode;
  1815. struct task_struct *task = get_proc_task(inode);
  1816. if (task) {
  1817. put_task_struct(task);
  1818. return 1;
  1819. }
  1820. d_drop(dentry);
  1821. return 0;
  1822. }
  1823. static struct dentry_operations proc_base_dentry_operations =
  1824. {
  1825. .d_revalidate = proc_base_revalidate,
  1826. .d_delete = pid_delete_dentry,
  1827. };
  1828. static struct dentry *proc_base_instantiate(struct inode *dir,
  1829. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1830. {
  1831. const struct pid_entry *p = ptr;
  1832. struct inode *inode;
  1833. struct proc_inode *ei;
  1834. struct dentry *error = ERR_PTR(-EINVAL);
  1835. /* Allocate the inode */
  1836. error = ERR_PTR(-ENOMEM);
  1837. inode = new_inode(dir->i_sb);
  1838. if (!inode)
  1839. goto out;
  1840. /* Initialize the inode */
  1841. ei = PROC_I(inode);
  1842. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1843. /*
  1844. * grab the reference to the task.
  1845. */
  1846. ei->pid = get_task_pid(task, PIDTYPE_PID);
  1847. if (!ei->pid)
  1848. goto out_iput;
  1849. inode->i_uid = 0;
  1850. inode->i_gid = 0;
  1851. inode->i_mode = p->mode;
  1852. if (S_ISDIR(inode->i_mode))
  1853. inode->i_nlink = 2;
  1854. if (S_ISLNK(inode->i_mode))
  1855. inode->i_size = 64;
  1856. if (p->iop)
  1857. inode->i_op = p->iop;
  1858. if (p->fop)
  1859. inode->i_fop = p->fop;
  1860. ei->op = p->op;
  1861. dentry->d_op = &proc_base_dentry_operations;
  1862. d_add(dentry, inode);
  1863. error = NULL;
  1864. out:
  1865. return error;
  1866. out_iput:
  1867. iput(inode);
  1868. goto out;
  1869. }
  1870. static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
  1871. {
  1872. struct dentry *error;
  1873. struct task_struct *task = get_proc_task(dir);
  1874. const struct pid_entry *p, *last;
  1875. error = ERR_PTR(-ENOENT);
  1876. if (!task)
  1877. goto out_no_task;
  1878. /* Lookup the directory entry */
  1879. last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
  1880. for (p = proc_base_stuff; p <= last; p++) {
  1881. if (p->len != dentry->d_name.len)
  1882. continue;
  1883. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1884. break;
  1885. }
  1886. if (p > last)
  1887. goto out;
  1888. error = proc_base_instantiate(dir, dentry, task, p);
  1889. out:
  1890. put_task_struct(task);
  1891. out_no_task:
  1892. return error;
  1893. }
  1894. static int proc_base_fill_cache(struct file *filp, void *dirent,
  1895. filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
  1896. {
  1897. return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
  1898. proc_base_instantiate, task, p);
  1899. }
  1900. #ifdef CONFIG_TASK_IO_ACCOUNTING
  1901. static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
  1902. {
  1903. return sprintf(buffer,
  1904. #ifdef CONFIG_TASK_XACCT
  1905. "rchar: %llu\n"
  1906. "wchar: %llu\n"
  1907. "syscr: %llu\n"
  1908. "syscw: %llu\n"
  1909. #endif
  1910. "read_bytes: %llu\n"
  1911. "write_bytes: %llu\n"
  1912. "cancelled_write_bytes: %llu\n",
  1913. #ifdef CONFIG_TASK_XACCT
  1914. (unsigned long long)task->rchar,
  1915. (unsigned long long)task->wchar,
  1916. (unsigned long long)task->syscr,
  1917. (unsigned long long)task->syscw,
  1918. #endif
  1919. (unsigned long long)task->ioac.read_bytes,
  1920. (unsigned long long)task->ioac.write_bytes,
  1921. (unsigned long long)task->ioac.cancelled_write_bytes);
  1922. }
  1923. #endif
  1924. /*
  1925. * Thread groups
  1926. */
  1927. static const struct file_operations proc_task_operations;
  1928. static const struct inode_operations proc_task_inode_operations;
  1929. static const struct pid_entry tgid_base_stuff[] = {
  1930. DIR("task", S_IRUGO|S_IXUGO, task),
  1931. DIR("fd", S_IRUSR|S_IXUSR, fd),
  1932. DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
  1933. REG("environ", S_IRUSR, environ),
  1934. INF("auxv", S_IRUSR, pid_auxv),
  1935. INF("status", S_IRUGO, pid_status),
  1936. INF("limits", S_IRUSR, pid_limits),
  1937. #ifdef CONFIG_SCHED_DEBUG
  1938. REG("sched", S_IRUGO|S_IWUSR, pid_sched),
  1939. #endif
  1940. INF("cmdline", S_IRUGO, pid_cmdline),
  1941. INF("stat", S_IRUGO, tgid_stat),
  1942. INF("statm", S_IRUGO, pid_statm),
  1943. REG("maps", S_IRUGO, maps),
  1944. #ifdef CONFIG_NUMA
  1945. REG("numa_maps", S_IRUGO, numa_maps),
  1946. #endif
  1947. REG("mem", S_IRUSR|S_IWUSR, mem),
  1948. LNK("cwd", cwd),
  1949. LNK("root", root),
  1950. LNK("exe", exe),
  1951. REG("mounts", S_IRUGO, mounts),
  1952. REG("mountstats", S_IRUSR, mountstats),
  1953. #ifdef CONFIG_MMU
  1954. REG("clear_refs", S_IWUSR, clear_refs),
  1955. REG("smaps", S_IRUGO, smaps),
  1956. REG("pagemap", S_IRUSR, pagemap),
  1957. #endif
  1958. #ifdef CONFIG_SECURITY
  1959. DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
  1960. #endif
  1961. #ifdef CONFIG_KALLSYMS
  1962. INF("wchan", S_IRUGO, pid_wchan),
  1963. #endif
  1964. #ifdef CONFIG_SCHEDSTATS
  1965. INF("schedstat", S_IRUGO, pid_schedstat),
  1966. #endif
  1967. #ifdef CONFIG_LATENCYTOP
  1968. REG("latency", S_IRUGO, lstats),
  1969. #endif
  1970. #ifdef CONFIG_PROC_PID_CPUSET
  1971. REG("cpuset", S_IRUGO, cpuset),
  1972. #endif
  1973. #ifdef CONFIG_CGROUPS
  1974. REG("cgroup", S_IRUGO, cgroup),
  1975. #endif
  1976. INF("oom_score", S_IRUGO, oom_score),
  1977. REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
  1978. #ifdef CONFIG_AUDITSYSCALL
  1979. REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
  1980. #endif
  1981. #ifdef CONFIG_FAULT_INJECTION
  1982. REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
  1983. #endif
  1984. #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
  1985. REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
  1986. #endif
  1987. #ifdef CONFIG_TASK_IO_ACCOUNTING
  1988. INF("io", S_IRUGO, pid_io_accounting),
  1989. #endif
  1990. };
  1991. static int proc_tgid_base_readdir(struct file * filp,
  1992. void * dirent, filldir_t filldir)
  1993. {
  1994. return proc_pident_readdir(filp,dirent,filldir,
  1995. tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
  1996. }
  1997. static const struct file_operations proc_tgid_base_operations = {
  1998. .read = generic_read_dir,
  1999. .readdir = proc_tgid_base_readdir,
  2000. };
  2001. static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  2002. return proc_pident_lookup(dir, dentry,
  2003. tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
  2004. }
  2005. static const struct inode_operations proc_tgid_base_inode_operations = {
  2006. .lookup = proc_tgid_base_lookup,
  2007. .getattr = pid_getattr,
  2008. .setattr = proc_setattr,
  2009. };
  2010. static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
  2011. {
  2012. struct dentry *dentry, *leader, *dir;
  2013. char buf[PROC_NUMBUF];
  2014. struct qstr name;
  2015. name.name = buf;
  2016. name.len = snprintf(buf, sizeof(buf), "%d", pid);
  2017. dentry = d_hash_and_lookup(mnt->mnt_root, &name);
  2018. if (dentry) {
  2019. shrink_dcache_parent(dentry);
  2020. d_drop(dentry);
  2021. dput(dentry);
  2022. }
  2023. if (tgid == 0)
  2024. goto out;
  2025. name.name = buf;
  2026. name.len = snprintf(buf, sizeof(buf), "%d", tgid);
  2027. leader = d_hash_and_lookup(mnt->mnt_root, &name);
  2028. if (!leader)
  2029. goto out;
  2030. name.name = "task";
  2031. name.len = strlen(name.name);
  2032. dir = d_hash_and_lookup(leader, &name);
  2033. if (!dir)
  2034. goto out_put_leader;
  2035. name.name = buf;
  2036. name.len = snprintf(buf, sizeof(buf), "%d", pid);
  2037. dentry = d_hash_and_lookup(dir, &name);
  2038. if (dentry) {
  2039. shrink_dcache_parent(dentry);
  2040. d_drop(dentry);
  2041. dput(dentry);
  2042. }
  2043. dput(dir);
  2044. out_put_leader:
  2045. dput(leader);
  2046. out:
  2047. return;
  2048. }
  2049. /**
  2050. * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
  2051. * @task: task that should be flushed.
  2052. *
  2053. * When flushing dentries from proc, one needs to flush them from global
  2054. * proc (proc_mnt) and from all the namespaces' procs this task was seen
  2055. * in. This call is supposed to do all of this job.
  2056. *
  2057. * Looks in the dcache for
  2058. * /proc/@pid
  2059. * /proc/@tgid/task/@pid
  2060. * if either directory is present flushes it and all of it'ts children
  2061. * from the dcache.
  2062. *
  2063. * It is safe and reasonable to cache /proc entries for a task until
  2064. * that task exits. After that they just clog up the dcache with
  2065. * useless entries, possibly causing useful dcache entries to be
  2066. * flushed instead. This routine is proved to flush those useless
  2067. * dcache entries at process exit time.
  2068. *
  2069. * NOTE: This routine is just an optimization so it does not guarantee
  2070. * that no dcache entries will exist at process exit time it
  2071. * just makes it very unlikely that any will persist.
  2072. */
  2073. void proc_flush_task(struct task_struct *task)
  2074. {
  2075. int i;
  2076. struct pid *pid, *tgid = NULL;
  2077. struct upid *upid;
  2078. pid = task_pid(task);
  2079. if (thread_group_leader(task))
  2080. tgid = task_tgid(task);
  2081. for (i = 0; i <= pid->level; i++) {
  2082. upid = &pid->numbers[i];
  2083. proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
  2084. tgid ? tgid->numbers[i].nr : 0);
  2085. }
  2086. upid = &pid->numbers[pid->level];
  2087. if (upid->nr == 1)
  2088. pid_ns_release_proc(upid->ns);
  2089. }
  2090. static struct dentry *proc_pid_instantiate(struct inode *dir,
  2091. struct dentry * dentry,
  2092. struct task_struct *task, const void *ptr)
  2093. {
  2094. struct dentry *error = ERR_PTR(-ENOENT);
  2095. struct inode *inode;
  2096. inode = proc_pid_make_inode(dir->i_sb, task);
  2097. if (!inode)
  2098. goto out;
  2099. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  2100. inode->i_op = &proc_tgid_base_inode_operations;
  2101. inode->i_fop = &proc_tgid_base_operations;
  2102. inode->i_flags|=S_IMMUTABLE;
  2103. inode->i_nlink = 5;
  2104. #ifdef CONFIG_SECURITY
  2105. inode->i_nlink += 1;
  2106. #endif
  2107. dentry->d_op = &pid_dentry_operations;
  2108. d_add(dentry, inode);
  2109. /* Close the race of the process dying before we return the dentry */
  2110. if (pid_revalidate(dentry, NULL))
  2111. error = NULL;
  2112. out:
  2113. return error;
  2114. }
  2115. struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  2116. {
  2117. struct dentry *result = ERR_PTR(-ENOENT);
  2118. struct task_struct *task;
  2119. unsigned tgid;
  2120. struct pid_namespace *ns;
  2121. result = proc_base_lookup(dir, dentry);
  2122. if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
  2123. goto out;
  2124. tgid = name_to_int(dentry);
  2125. if (tgid == ~0U)
  2126. goto out;
  2127. ns = dentry->d_sb->s_fs_info;
  2128. rcu_read_lock();
  2129. task = find_task_by_pid_ns(tgid, ns);
  2130. if (task)
  2131. get_task_struct(task);
  2132. rcu_read_unlock();
  2133. if (!task)
  2134. goto out;
  2135. result = proc_pid_instantiate(dir, dentry, task, NULL);
  2136. put_task_struct(task);
  2137. out:
  2138. return result;
  2139. }
  2140. /*
  2141. * Find the first task with tgid >= tgid
  2142. *
  2143. */
  2144. struct tgid_iter {
  2145. unsigned int tgid;
  2146. struct task_struct *task;
  2147. };
  2148. static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
  2149. {
  2150. struct pid *pid;
  2151. if (iter.task)
  2152. put_task_struct(iter.task);
  2153. rcu_read_lock();
  2154. retry:
  2155. iter.task = NULL;
  2156. pid = find_ge_pid(iter.tgid, ns);
  2157. if (pid) {
  2158. iter.tgid = pid_nr_ns(pid, ns);
  2159. iter.task = pid_task(pid, PIDTYPE_PID);
  2160. /* What we to know is if the pid we have find is the
  2161. * pid of a thread_group_leader. Testing for task
  2162. * being a thread_group_leader is the obvious thing
  2163. * todo but there is a window when it fails, due to
  2164. * the pid transfer logic in de_thread.
  2165. *
  2166. * So we perform the straight forward test of seeing
  2167. * if the pid we have found is the pid of a thread
  2168. * group leader, and don't worry if the task we have
  2169. * found doesn't happen to be a thread group leader.
  2170. * As we don't care in the case of readdir.
  2171. */
  2172. if (!iter.task || !has_group_leader_pid(iter.task)) {
  2173. iter.tgid += 1;
  2174. goto retry;
  2175. }
  2176. get_task_struct(iter.task);
  2177. }
  2178. rcu_read_unlock();
  2179. return iter;
  2180. }
  2181. #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
  2182. static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  2183. struct tgid_iter iter)
  2184. {
  2185. char name[PROC_NUMBUF];
  2186. int len = snprintf(name, sizeof(name), "%d", iter.tgid);
  2187. return proc_fill_cache(filp, dirent, filldir, name, len,
  2188. proc_pid_instantiate, iter.task, NULL);
  2189. }
  2190. /* for the /proc/ directory itself, after non-process stuff has been done */
  2191. int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2192. {
  2193. unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
  2194. struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
  2195. struct tgid_iter iter;
  2196. struct pid_namespace *ns;
  2197. if (!reaper)
  2198. goto out_no_task;
  2199. for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
  2200. const struct pid_entry *p = &proc_base_stuff[nr];
  2201. if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
  2202. goto out;
  2203. }
  2204. ns = filp->f_dentry->d_sb->s_fs_info;
  2205. iter.task = NULL;
  2206. iter.tgid = filp->f_pos - TGID_OFFSET;
  2207. for (iter = next_tgid(ns, iter);
  2208. iter.task;
  2209. iter.tgid += 1, iter = next_tgid(ns, iter)) {
  2210. filp->f_pos = iter.tgid + TGID_OFFSET;
  2211. if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
  2212. put_task_struct(iter.task);
  2213. goto out;
  2214. }
  2215. }
  2216. filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
  2217. out:
  2218. put_task_struct(reaper);
  2219. out_no_task:
  2220. return 0;
  2221. }
  2222. /*
  2223. * Tasks
  2224. */
  2225. static const struct pid_entry tid_base_stuff[] = {
  2226. DIR("fd", S_IRUSR|S_IXUSR, fd),
  2227. DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
  2228. REG("environ", S_IRUSR, environ),
  2229. INF("auxv", S_IRUSR, pid_auxv),
  2230. INF("status", S_IRUGO, pid_status),
  2231. INF("limits", S_IRUSR, pid_limits),
  2232. #ifdef CONFIG_SCHED_DEBUG
  2233. REG("sched", S_IRUGO|S_IWUSR, pid_sched),
  2234. #endif
  2235. INF("cmdline", S_IRUGO, pid_cmdline),
  2236. INF("stat", S_IRUGO, tid_stat),
  2237. INF("statm", S_IRUGO, pid_statm),
  2238. REG("maps", S_IRUGO, maps),
  2239. #ifdef CONFIG_NUMA
  2240. REG("numa_maps", S_IRUGO, numa_maps),
  2241. #endif
  2242. REG("mem", S_IRUSR|S_IWUSR, mem),
  2243. LNK("cwd", cwd),
  2244. LNK("root", root),
  2245. LNK("exe", exe),
  2246. REG("mounts", S_IRUGO, mounts),
  2247. #ifdef CONFIG_MMU
  2248. REG("clear_refs", S_IWUSR, clear_refs),
  2249. REG("smaps", S_IRUGO, smaps),
  2250. REG("pagemap", S_IRUSR, pagemap),
  2251. #endif
  2252. #ifdef CONFIG_SECURITY
  2253. DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
  2254. #endif
  2255. #ifdef CONFIG_KALLSYMS
  2256. INF("wchan", S_IRUGO, pid_wchan),
  2257. #endif
  2258. #ifdef CONFIG_SCHEDSTATS
  2259. INF("schedstat", S_IRUGO, pid_schedstat),
  2260. #endif
  2261. #ifdef CONFIG_LATENCYTOP
  2262. REG("latency", S_IRUGO, lstats),
  2263. #endif
  2264. #ifdef CONFIG_PROC_PID_CPUSET
  2265. REG("cpuset", S_IRUGO, cpuset),
  2266. #endif
  2267. #ifdef CONFIG_CGROUPS
  2268. REG("cgroup", S_IRUGO, cgroup),
  2269. #endif
  2270. INF("oom_score", S_IRUGO, oom_score),
  2271. REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
  2272. #ifdef CONFIG_AUDITSYSCALL
  2273. REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
  2274. #endif
  2275. #ifdef CONFIG_FAULT_INJECTION
  2276. REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
  2277. #endif
  2278. };
  2279. static int proc_tid_base_readdir(struct file * filp,
  2280. void * dirent, filldir_t filldir)
  2281. {
  2282. return proc_pident_readdir(filp,dirent,filldir,
  2283. tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
  2284. }
  2285. static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  2286. return proc_pident_lookup(dir, dentry,
  2287. tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
  2288. }
  2289. static const struct file_operations proc_tid_base_operations = {
  2290. .read = generic_read_dir,
  2291. .readdir = proc_tid_base_readdir,
  2292. };
  2293. static const struct inode_operations proc_tid_base_inode_operations = {
  2294. .lookup = proc_tid_base_lookup,
  2295. .getattr = pid_getattr,
  2296. .setattr = proc_setattr,
  2297. };
  2298. static struct dentry *proc_task_instantiate(struct inode *dir,
  2299. struct dentry *dentry, struct task_struct *task, const void *ptr)
  2300. {
  2301. struct dentry *error = ERR_PTR(-ENOENT);
  2302. struct inode *inode;
  2303. inode = proc_pid_make_inode(dir->i_sb, task);
  2304. if (!inode)
  2305. goto out;
  2306. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  2307. inode->i_op = &proc_tid_base_inode_operations;
  2308. inode->i_fop = &proc_tid_base_operations;
  2309. inode->i_flags|=S_IMMUTABLE;
  2310. inode->i_nlink = 4;
  2311. #ifdef CONFIG_SECURITY
  2312. inode->i_nlink += 1;
  2313. #endif
  2314. dentry->d_op = &pid_dentry_operations;
  2315. d_add(dentry, inode);
  2316. /* Close the race of the process dying before we return the dentry */
  2317. if (pid_revalidate(dentry, NULL))
  2318. error = NULL;
  2319. out:
  2320. return error;
  2321. }
  2322. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  2323. {
  2324. struct dentry *result = ERR_PTR(-ENOENT);
  2325. struct task_struct *task;
  2326. struct task_struct *leader = get_proc_task(dir);
  2327. unsigned tid;
  2328. struct pid_namespace *ns;
  2329. if (!leader)
  2330. goto out_no_task;
  2331. tid = name_to_int(dentry);
  2332. if (tid == ~0U)
  2333. goto out;
  2334. ns = dentry->d_sb->s_fs_info;
  2335. rcu_read_lock();
  2336. task = find_task_by_pid_ns(tid, ns);
  2337. if (task)
  2338. get_task_struct(task);
  2339. rcu_read_unlock();
  2340. if (!task)
  2341. goto out;
  2342. if (!same_thread_group(leader, task))
  2343. goto out_drop_task;
  2344. result = proc_task_instantiate(dir, dentry, task, NULL);
  2345. out_drop_task:
  2346. put_task_struct(task);
  2347. out:
  2348. put_task_struct(leader);
  2349. out_no_task:
  2350. return result;
  2351. }
  2352. /*
  2353. * Find the first tid of a thread group to return to user space.
  2354. *
  2355. * Usually this is just the thread group leader, but if the users
  2356. * buffer was too small or there was a seek into the middle of the
  2357. * directory we have more work todo.
  2358. *
  2359. * In the case of a short read we start with find_task_by_pid.
  2360. *
  2361. * In the case of a seek we start with the leader and walk nr
  2362. * threads past it.
  2363. */
  2364. static struct task_struct *first_tid(struct task_struct *leader,
  2365. int tid, int nr, struct pid_namespace *ns)
  2366. {
  2367. struct task_struct *pos;
  2368. rcu_read_lock();
  2369. /* Attempt to start with the pid of a thread */
  2370. if (tid && (nr > 0)) {
  2371. pos = find_task_by_pid_ns(tid, ns);
  2372. if (pos && (pos->group_leader == leader))
  2373. goto found;
  2374. }
  2375. /* If nr exceeds the number of threads there is nothing todo */
  2376. pos = NULL;
  2377. if (nr && nr >= get_nr_threads(leader))
  2378. goto out;
  2379. /* If we haven't found our starting place yet start
  2380. * with the leader and walk nr threads forward.
  2381. */
  2382. for (pos = leader; nr > 0; --nr) {
  2383. pos = next_thread(pos);
  2384. if (pos == leader) {
  2385. pos = NULL;
  2386. goto out;
  2387. }
  2388. }
  2389. found:
  2390. get_task_struct(pos);
  2391. out:
  2392. rcu_read_unlock();
  2393. return pos;
  2394. }
  2395. /*
  2396. * Find the next thread in the thread list.
  2397. * Return NULL if there is an error or no next thread.
  2398. *
  2399. * The reference to the input task_struct is released.
  2400. */
  2401. static struct task_struct *next_tid(struct task_struct *start)
  2402. {
  2403. struct task_struct *pos = NULL;
  2404. rcu_read_lock();
  2405. if (pid_alive(start)) {
  2406. pos = next_thread(start);
  2407. if (thread_group_leader(pos))
  2408. pos = NULL;
  2409. else
  2410. get_task_struct(pos);
  2411. }
  2412. rcu_read_unlock();
  2413. put_task_struct(start);
  2414. return pos;
  2415. }
  2416. static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  2417. struct task_struct *task, int tid)
  2418. {
  2419. char name[PROC_NUMBUF];
  2420. int len = snprintf(name, sizeof(name), "%d", tid);
  2421. return proc_fill_cache(filp, dirent, filldir, name, len,
  2422. proc_task_instantiate, task, NULL);
  2423. }
  2424. /* for the /proc/TGID/task/ directories */
  2425. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2426. {
  2427. struct dentry *dentry = filp->f_path.dentry;
  2428. struct inode *inode = dentry->d_inode;
  2429. struct task_struct *leader = NULL;
  2430. struct task_struct *task;
  2431. int retval = -ENOENT;
  2432. ino_t ino;
  2433. int tid;
  2434. unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
  2435. struct pid_namespace *ns;
  2436. task = get_proc_task(inode);
  2437. if (!task)
  2438. goto out_no_task;
  2439. rcu_read_lock();
  2440. if (pid_alive(task)) {
  2441. leader = task->group_leader;
  2442. get_task_struct(leader);
  2443. }
  2444. rcu_read_unlock();
  2445. put_task_struct(task);
  2446. if (!leader)
  2447. goto out_no_task;
  2448. retval = 0;
  2449. switch (pos) {
  2450. case 0:
  2451. ino = inode->i_ino;
  2452. if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
  2453. goto out;
  2454. pos++;
  2455. /* fall through */
  2456. case 1:
  2457. ino = parent_ino(dentry);
  2458. if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
  2459. goto out;
  2460. pos++;
  2461. /* fall through */
  2462. }
  2463. /* f_version caches the tgid value that the last readdir call couldn't
  2464. * return. lseek aka telldir automagically resets f_version to 0.
  2465. */
  2466. ns = filp->f_dentry->d_sb->s_fs_info;
  2467. tid = (int)filp->f_version;
  2468. filp->f_version = 0;
  2469. for (task = first_tid(leader, tid, pos - 2, ns);
  2470. task;
  2471. task = next_tid(task), pos++) {
  2472. tid = task_pid_nr_ns(task, ns);
  2473. if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
  2474. /* returning this tgid failed, save it as the first
  2475. * pid for the next readir call */
  2476. filp->f_version = (u64)tid;
  2477. put_task_struct(task);
  2478. break;
  2479. }
  2480. }
  2481. out:
  2482. filp->f_pos = pos;
  2483. put_task_struct(leader);
  2484. out_no_task:
  2485. return retval;
  2486. }
  2487. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  2488. {
  2489. struct inode *inode = dentry->d_inode;
  2490. struct task_struct *p = get_proc_task(inode);
  2491. generic_fillattr(inode, stat);
  2492. if (p) {
  2493. rcu_read_lock();
  2494. stat->nlink += get_nr_threads(p);
  2495. rcu_read_unlock();
  2496. put_task_struct(p);
  2497. }
  2498. return 0;
  2499. }
  2500. static const struct inode_operations proc_task_inode_operations = {
  2501. .lookup = proc_task_lookup,
  2502. .getattr = proc_task_getattr,
  2503. .setattr = proc_setattr,
  2504. };
  2505. static const struct file_operations proc_task_operations = {
  2506. .read = generic_read_dir,
  2507. .readdir = proc_task_readdir,
  2508. };