base.c 76 KB

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