base.c 76 KB

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