base.c 76 KB

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