base.c 76 KB

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