base.c 75 KB

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