base.c 76 KB

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