base.c 76 KB

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