base.c 75 KB

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