base.c 75 KB

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