base.c 76 KB

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