base.c 58 KB

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