base.c 53 KB

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