base.c 75 KB

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