base.c 61 KB

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