]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/fhandler_process.cc
Remove unneeded header files from source files throughout.
[newlib-cygwin.git] / winsup / cygwin / fhandler_process.cc
1 /* fhandler_process.cc: fhandler for /proc/<pid> virtual filesystem
2
3 Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include "winsup.h"
12 #include <stdlib.h>
13 #include <sys/cygwin.h>
14 #include "cygerrno.h"
15 #include "security.h"
16 #include "path.h"
17 #include "fhandler.h"
18 #include "pinfo.h"
19 #include "shared_info.h"
20 #include "dtable.h"
21 #include "cygheap.h"
22 #include "ntdll.h"
23 #include "cygtls.h"
24 #include "tls_pbuf.h"
25 #include <sys/param.h>
26 #include <ctype.h>
27 #include <psapi.h>
28
29 #define _COMPILING_NEWLIB
30 #include <dirent.h>
31
32 static const int PROCESS_PPID = 2;
33 static const int PROCESS_WINPID = 3;
34 static const int PROCESS_WINEXENAME = 4;
35 static const int PROCESS_STATUS = 5;
36 static const int PROCESS_UID = 6;
37 static const int PROCESS_GID = 7;
38 static const int PROCESS_PGID = 8;
39 static const int PROCESS_SID = 9;
40 static const int PROCESS_CTTY = 10;
41 static const int PROCESS_STAT = 11;
42 static const int PROCESS_STATM = 12;
43 static const int PROCESS_CMDLINE = 13;
44 static const int PROCESS_MAPS = 14;
45 static const int PROCESS_FD = 15;
46 static const int PROCESS_EXENAME = 16;
47 /* Keep symlinks always the last entries. */
48 static const int PROCESS_ROOT = 17;
49 static const int PROCESS_EXE = 18;
50 static const int PROCESS_CWD = 19;
51
52 /* The position of "root" defines the beginning of symlik entries. */
53 #define is_symlink(nr) ((nr) >= PROCESS_ROOT)
54
55 static const char * const process_listing[] =
56 {
57 ".",
58 "..",
59 "ppid",
60 "winpid",
61 "winexename",
62 "status",
63 "uid",
64 "gid",
65 "pgid",
66 "sid",
67 "ctty",
68 "stat",
69 "statm",
70 "cmdline",
71 "maps",
72 "fd",
73 "exename",
74 /* Keep symlinks always the last entries. */
75 "root",
76 "exe",
77 "cwd",
78 NULL
79 };
80
81 static const int PROCESS_LINK_COUNT =
82 (sizeof (process_listing) / sizeof (const char *)) - 1;
83
84 static _off64_t format_process_maps (_pinfo *p, char *&destbuf, size_t maxsize);
85 static _off64_t format_process_stat (_pinfo *p, char *destbuf, size_t maxsize);
86 static _off64_t format_process_status (_pinfo *p, char *destbuf, size_t maxsize);
87 static _off64_t format_process_statm (_pinfo *p, char *destbuf, size_t maxsize);
88 static int get_process_state (DWORD dwProcessId);
89 static bool get_mem_values (DWORD dwProcessId, unsigned long *vmsize,
90 unsigned long *vmrss, unsigned long *vmtext,
91 unsigned long *vmdata, unsigned long *vmlib,
92 unsigned long *vmshare);
93
94 /* Returns 0 if path doesn't exist, >0 if path is a directory,
95 * -1 if path is a file, -2 if path is a symlink, -3 if path is a pipe,
96 * -4 if path is a socket.
97 */
98 int
99 fhandler_process::exists ()
100 {
101 const char *path = get_name ();
102 debug_printf ("exists (%s)", path);
103 path += proc_len + 1;
104 while (*path != 0 && !isdirsep (*path))
105 path++;
106 if (*path == 0)
107 return 2;
108
109 for (int i = 0; process_listing[i]; i++)
110 if (pathmatch (path + 1, process_listing[i]))
111 {
112 fileid = i;
113 return is_symlink (i) ? -2 : (i == PROCESS_FD) ? 1 : -1;
114 }
115 if (pathnmatch (strchr (path, '/') + 1, "fd/", 3))
116 {
117 fileid = PROCESS_FD;
118 if (fill_filebuf ())
119 return -2;
120 /* Check for nameless device entries. */
121 path = strrchr (path, '/');
122 if (path && *++path)
123 {
124 if (!strncmp (path, "pipe:[", 6))
125 return -3;
126 else if (!strncmp (path, "socket:[", 8))
127 return -4;
128 }
129 }
130 return 0;
131 }
132
133 fhandler_process::fhandler_process ():
134 fhandler_proc ()
135 {
136 }
137
138 int
139 fhandler_process::fstat (struct __stat64 *buf)
140 {
141 const char *path = get_name ();
142 int file_type = exists ();
143 fhandler_base::fstat (buf);
144 path += proc_len + 1;
145 pid = atoi (path);
146 pinfo p (pid);
147 if (!p)
148 {
149 set_errno (ENOENT);
150 return -1;
151 }
152
153 buf->st_mode &= ~_IFMT & NO_W;
154
155 switch (file_type)
156 {
157 case 0:
158 set_errno (ENOENT);
159 return -1;
160 case 1:
161 case 2:
162 buf->st_ctime = buf->st_mtime = buf->st_birthtime = p->start_time;
163 buf->st_ctim.tv_nsec = buf->st_mtim.tv_nsec
164 = buf->st_birthtim.tv_nsec = 0;
165 time_as_timestruc_t (&buf->st_atim);
166 buf->st_uid = p->uid;
167 buf->st_gid = p->gid;
168 buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
169 if (file_type == 1)
170 buf->st_nlink = 2;
171 else
172 buf->st_nlink = 3;
173 return 0;
174 case -2:
175 buf->st_uid = p->uid;
176 buf->st_gid = p->gid;
177 buf->st_mode = S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
178 return 0;
179 case -3:
180 buf->st_uid = p->uid;
181 buf->st_gid = p->gid;
182 buf->st_mode = S_IFIFO | S_IRUSR | S_IWUSR;
183 return 0;
184 case -4:
185 buf->st_uid = p->uid;
186 buf->st_gid = p->gid;
187 buf->st_mode = S_IFSOCK | S_IRUSR | S_IWUSR;
188 return 0;
189 case -1:
190 default:
191 buf->st_uid = p->uid;
192 buf->st_gid = p->gid;
193 buf->st_mode |= S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
194 return 0;
195 }
196 }
197
198 DIR *
199 fhandler_process::opendir (int fd)
200 {
201 DIR *dir = fhandler_virtual::opendir (fd);
202 if (dir && fileid == PROCESS_FD)
203 fill_filebuf ();
204 return dir;
205 }
206
207 int
208 fhandler_process::readdir (DIR *dir, dirent *de)
209 {
210 int res = ENMFILE;
211 if (fileid == PROCESS_FD)
212 {
213 if (dir->__d_position >= 2 + filesize / sizeof (int))
214 goto out;
215 }
216 else if (dir->__d_position >= PROCESS_LINK_COUNT)
217 goto out;
218 if (fileid == PROCESS_FD && dir->__d_position > 1)
219 {
220 int *p = (int *) filebuf;
221 __small_sprintf (de->d_name, "%d", p[dir->__d_position++ - 2]);
222 }
223 else
224 strcpy (de->d_name, process_listing[dir->__d_position++]);
225 dir->__flags |= dirent_saw_dot | dirent_saw_dot_dot;
226 res = 0;
227 out:
228 syscall_printf ("%d = readdir (%p, %p) (%s)", res, dir, de, de->d_name);
229 return res;
230 }
231
232 int
233 fhandler_process::open (int flags, mode_t mode)
234 {
235 int process_file_no = -1;
236
237 int res = fhandler_virtual::open (flags, mode);
238 if (!res)
239 goto out;
240
241 nohandle (true);
242
243 const char *path;
244 path = get_name () + proc_len + 1;
245 pid = atoi (path);
246 while (*path != 0 && !isdirsep (*path))
247 path++;
248
249 if (*path == 0)
250 {
251 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
252 {
253 set_errno (EEXIST);
254 res = 0;
255 goto out;
256 }
257 else if (flags & O_WRONLY)
258 {
259 set_errno (EISDIR);
260 res = 0;
261 goto out;
262 }
263 else
264 {
265 flags |= O_DIROPEN;
266 goto success;
267 }
268 }
269
270 process_file_no = -1;
271 for (int i = 0; process_listing[i]; i++)
272 {
273 if (path_prefix_p
274 (process_listing[i], path + 1, strlen (process_listing[i])))
275 process_file_no = i;
276 }
277 if (process_file_no == -1)
278 {
279 if (flags & O_CREAT)
280 {
281 set_errno (EROFS);
282 res = 0;
283 goto out;
284 }
285 else
286 {
287 set_errno (ENOENT);
288 res = 0;
289 goto out;
290 }
291 }
292 if (process_file_no == PROCESS_FD)
293 {
294 flags |= O_DIROPEN;
295 goto success;
296 }
297 if (flags & O_WRONLY)
298 {
299 set_errno (EROFS);
300 res = 0;
301 goto out;
302 }
303
304 fileid = process_file_no;
305 if (!fill_filebuf ())
306 {
307 res = 0;
308 goto out;
309 }
310
311 if (flags & O_APPEND)
312 position = filesize;
313 else
314 position = 0;
315
316 success:
317 res = 1;
318 set_flags ((flags & ~O_TEXT) | O_BINARY);
319 set_open_status ();
320 out:
321 syscall_printf ("%d = fhandler_proc::open (%p, %d)", res, flags, mode);
322 return res;
323 }
324
325 bool
326 fhandler_process::fill_filebuf ()
327 {
328 const char *path;
329 path = get_name () + proc_len + 1;
330 if (!pid)
331 pid = atoi (path);
332
333 pinfo p (pid);
334
335 if (!p)
336 {
337 set_errno (ENOENT);
338 return false;
339 }
340
341 switch (fileid)
342 {
343 case PROCESS_FD:
344 {
345 size_t fs;
346 char *fdp = strrchr (path, '/');
347 if (!fdp || *++fdp == 'f') /* The "fd" directory itself. */
348 {
349 if (filebuf)
350 cfree (filebuf);
351 filebuf = p->fds (fs);
352 }
353 else
354 {
355 if (filebuf)
356 cfree (filebuf);
357 int fd = atoi (fdp);
358 if (fd < 0 || (fd == 0 && !isdigit (*fdp)))
359 {
360 set_errno (ENOENT);
361 return false;
362 }
363 filebuf = p->fd (fd, fs);
364 if (!filebuf || !*filebuf)
365 {
366 set_errno (ENOENT);
367 return false;
368 }
369 }
370 filesize = fs;
371 break;
372 }
373 case PROCESS_UID:
374 case PROCESS_GID:
375 case PROCESS_PGID:
376 case PROCESS_SID:
377 case PROCESS_CTTY:
378 case PROCESS_PPID:
379 {
380 filebuf = (char *) crealloc_abort (filebuf, bufalloc = 40);
381 int num;
382 switch (fileid)
383 {
384 case PROCESS_PPID:
385 num = p->ppid;
386 break;
387 case PROCESS_UID:
388 num = p->uid;
389 break;
390 case PROCESS_PGID:
391 num = p->pgid;
392 break;
393 case PROCESS_SID:
394 num = p->sid;
395 break;
396 case PROCESS_GID:
397 num = p->gid;
398 break;
399 case PROCESS_CTTY:
400 num = p->ctty;
401 break;
402 default: // what's this here for?
403 num = 0;
404 break;
405 }
406 __small_sprintf (filebuf, "%d\n", num);
407 filesize = strlen (filebuf);
408 break;
409 }
410 case PROCESS_ROOT:
411 case PROCESS_CWD:
412 case PROCESS_CMDLINE:
413 {
414 if (filebuf)
415 {
416 cfree (filebuf);
417 filebuf = NULL;
418 }
419 size_t fs;
420 switch (fileid)
421 {
422 case PROCESS_ROOT:
423 filebuf = p->root (fs);
424 break;
425 case PROCESS_CWD:
426 filebuf = p->cwd (fs);
427 break;
428 case PROCESS_CMDLINE:
429 filebuf = p->cmdline (fs);
430 break;
431 }
432 filesize = fs;
433 if (!filebuf || !*filebuf)
434 {
435 filebuf = cstrdup ("<defunct>");
436 filesize = strlen (filebuf) + 1;
437 }
438 break;
439 }
440 case PROCESS_EXENAME:
441 case PROCESS_EXE:
442 {
443 filebuf = (char *) crealloc_abort (filebuf, bufalloc = NT_MAX_PATH);
444 if (p->process_state & PID_EXITED)
445 strcpy (filebuf, "<defunct>");
446 else
447 {
448 mount_table->conv_to_posix_path (p->progname, filebuf, 1);
449 /* If transparent_exe isn't set, the link keeps its suffix so that
450 an open(2) call will succeed on /proc/$PID/exe. */
451 if (transparent_exe)
452 {
453 int len = strlen (filebuf);
454 if (len > 4)
455 {
456 char *s = filebuf + len - 4;
457 if (ascii_strcasematch (s, ".exe"))
458 *s = 0;
459 }
460 }
461 }
462 filesize = strlen (filebuf);
463 break;
464 }
465 case PROCESS_WINPID:
466 {
467 filebuf = (char *) crealloc_abort (filebuf, bufalloc = 40);
468 __small_sprintf (filebuf, "%d\n", p->dwProcessId);
469 filesize = strlen (filebuf);
470 break;
471 }
472 case PROCESS_WINEXENAME:
473 {
474 int len = strlen (p->progname);
475 filebuf = (char *) crealloc_abort (filebuf, bufalloc = (len + 2));
476 strcpy (filebuf, p->progname);
477 filebuf[len] = '\n';
478 filesize = len + 1;
479 break;
480 }
481 case PROCESS_STATUS:
482 {
483 filebuf = (char *) crealloc_abort (filebuf, bufalloc = 2048);
484 filesize = format_process_status (*p, filebuf, bufalloc);
485 break;
486 }
487 case PROCESS_STAT:
488 {
489 filebuf = (char *) crealloc_abort (filebuf, bufalloc = 2048);
490 filesize = format_process_stat (*p, filebuf, bufalloc);
491 break;
492 }
493 case PROCESS_STATM:
494 {
495 filebuf = (char *) crealloc_abort (filebuf, bufalloc = 2048);
496 filesize = format_process_statm (*p, filebuf, bufalloc);
497 break;
498 }
499 case PROCESS_MAPS:
500 {
501 filebuf = (char *) crealloc_abort (filebuf, bufalloc = 2048);
502 filesize = format_process_maps (*p, filebuf, bufalloc);
503 break;
504 }
505 }
506
507 return true;
508 }
509
510 static _off64_t
511 format_process_maps (_pinfo *p, char *&destbuf, size_t maxsize)
512 {
513 HANDLE proc = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
514 FALSE,
515 p->dwProcessId);
516 if (!proc)
517 return 0;
518
519 _off64_t len = 0;
520 HMODULE *modules;
521 DWORD needed, i;
522 DWORD_PTR wset_size;
523 DWORD_PTR *workingset = NULL;
524 MODULEINFO info;
525
526 tmp_pathbuf tp;
527 PWCHAR modname = tp.w_get ();
528 char *posix_modname = tp.c_get ();
529
530 if (!EnumProcessModules (proc, NULL, 0, &needed))
531 {
532 __seterrno ();
533 len = -1;
534 goto out;
535 }
536 modules = (HMODULE*) alloca (needed);
537 if (!EnumProcessModules (proc, modules, needed, &needed))
538 {
539 __seterrno ();
540 len = -1;
541 goto out;
542 }
543
544 QueryWorkingSet (proc, (void *) &wset_size, sizeof wset_size);
545 if (GetLastError () == ERROR_BAD_LENGTH)
546 {
547 workingset = (DWORD_PTR *) alloca (sizeof (DWORD_PTR) * ++wset_size);
548 if (!QueryWorkingSet (proc, (void *) workingset,
549 sizeof (DWORD_PTR) * wset_size))
550 workingset = NULL;
551 }
552 for (i = 0; i < needed / sizeof (HMODULE); i++)
553 if (GetModuleInformation (proc, modules[i], &info, sizeof info)
554 && GetModuleFileNameExW (proc, modules[i], modname, NT_MAX_PATH))
555 {
556 char access[5];
557 strcpy (access, "r--p");
558 struct __stat64 st;
559 if (mount_table->conv_to_posix_path (modname, posix_modname, 0))
560 sys_wcstombs (posix_modname, NT_MAX_PATH, modname);
561 if (stat64 (posix_modname, &st))
562 {
563 st.st_dev = 0;
564 st.st_ino = 0;
565 }
566 if (len + strlen (posix_modname) + 62 > maxsize - 1)
567 destbuf = (char *) crealloc_abort (destbuf, maxsize += 2048);
568 if (workingset)
569 for (unsigned i = 1; i <= wset_size; ++i)
570 {
571 DWORD_PTR addr = workingset[i] & 0xfffff000UL;
572 if ((char *)addr >= info.lpBaseOfDll
573 && (char *)addr < (char *)info.lpBaseOfDll + info.SizeOfImage)
574 {
575 access[0] = (workingset[i] & 0x5) ? 'r' : '-';
576 access[1] = (workingset[i] & 0x4) ? 'w' : '-';
577 access[2] = (workingset[i] & 0x2) ? 'x' : '-';
578 access[3] = (workingset[i] & 0x100) ? 's' : 'p';
579 }
580 }
581 int written = __small_sprintf (destbuf + len,
582 "%08lx-%08lx %s %08lx %04x:%04x %U ",
583 info.lpBaseOfDll,
584 (unsigned long)info.lpBaseOfDll
585 + info.SizeOfImage,
586 access,
587 info.EntryPoint,
588 st.st_dev >> 16,
589 st.st_dev & 0xffff,
590 st.st_ino);
591 while (written < 62)
592 destbuf[len + written++] = ' ';
593 len += written;
594 len += __small_sprintf (destbuf + len, "%s\n", posix_modname);
595 }
596 out:
597 CloseHandle (proc);
598 return len;
599 }
600
601 static _off64_t
602 format_process_stat (_pinfo *p, char *destbuf, size_t maxsize)
603 {
604 char cmd[NAME_MAX + 1];
605 int state = 'R';
606 unsigned long fault_count = 0UL,
607 utime = 0UL, stime = 0UL,
608 start_time = 0UL,
609 vmsize = 0UL, vmrss = 0UL, vmmaxrss = 0UL;
610 int priority = 0;
611 if (p->process_state & PID_EXITED)
612 strcpy (cmd, "<defunct>");
613 else
614 {
615 char *last_slash = strrchr (p->progname, '\\');
616 strcpy (cmd, last_slash ? last_slash + 1 : p->progname);
617 int len = strlen (cmd);
618 if (len > 4)
619 {
620 char *s = cmd + len - 4;
621 if (ascii_strcasematch (s, ".exe"))
622 *s = 0;
623 }
624 }
625 /*
626 * Note: under Windows, a _process_ is always running - it's only _threads_
627 * that get suspended. Therefore the default state is R (runnable).
628 */
629 if (p->process_state & PID_EXITED)
630 state = 'Z';
631 else if (p->process_state & PID_STOPPED)
632 state = 'T';
633 else
634 state = get_process_state (p->dwProcessId);
635 start_time = (GetTickCount () / 1000 - time (NULL) + p->start_time) * HZ;
636
637 NTSTATUS ret;
638 HANDLE hProcess;
639 VM_COUNTERS vmc;
640 KERNEL_USER_TIMES put;
641 PROCESS_BASIC_INFORMATION pbi;
642 QUOTA_LIMITS ql;
643 SYSTEM_TIME_OF_DAY_INFORMATION stodi;
644 SYSTEM_PROCESSOR_TIMES spt;
645 hProcess = OpenProcess (PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
646 FALSE, p->dwProcessId);
647 if (hProcess != NULL)
648 {
649 ret = NtQueryInformationProcess (hProcess,
650 ProcessVmCounters,
651 (PVOID) &vmc,
652 sizeof vmc, NULL);
653 if (ret == STATUS_SUCCESS)
654 ret = NtQueryInformationProcess (hProcess,
655 ProcessTimes,
656 (PVOID) &put,
657 sizeof put, NULL);
658 if (ret == STATUS_SUCCESS)
659 ret = NtQueryInformationProcess (hProcess,
660 ProcessBasicInformation,
661 (PVOID) &pbi,
662 sizeof pbi, NULL);
663 if (ret == STATUS_SUCCESS)
664 ret = NtQueryInformationProcess (hProcess,
665 ProcessQuotaLimits,
666 (PVOID) &ql,
667 sizeof ql, NULL);
668 CloseHandle (hProcess);
669 }
670 else
671 {
672 DWORD error = GetLastError ();
673 __seterrno_from_win_error (error);
674 debug_printf ("OpenProcess: ret %d", error);
675 return 0;
676 }
677 if (ret == STATUS_SUCCESS)
678 ret = NtQuerySystemInformation (SystemTimeOfDayInformation,
679 (PVOID) &stodi,
680 sizeof stodi, NULL);
681 if (ret == STATUS_SUCCESS)
682 ret = NtQuerySystemInformation (SystemProcessorTimes,
683 (PVOID) &spt,
684 sizeof spt, NULL);
685 if (ret != STATUS_SUCCESS)
686 {
687 __seterrno_from_nt_status (ret);
688 debug_printf ("NtQueryInformationProcess: ret %d, Dos(ret) %E", ret);
689 return 0;
690 }
691 fault_count = vmc.PageFaultCount;
692 utime = put.UserTime.QuadPart * HZ / 10000000ULL;
693 stime = put.KernelTime.QuadPart * HZ / 10000000ULL;
694 #if 0
695 if (stodi.CurrentTime.QuadPart > put.CreateTime.QuadPart)
696 start_time = (spt.KernelTime.QuadPart + spt.UserTime.QuadPart -
697 stodi.CurrentTime.QuadPart + put.CreateTime.QuadPart) * HZ / 10000000ULL;
698 else
699 /*
700 * sometimes stodi.CurrentTime is a bit behind
701 * Note: some older versions of procps are broken and can't cope
702 * with process start times > time(NULL).
703 */
704 start_time = (spt.KernelTme.QuadPart + spt.UserTime.QuadPart) * HZ / 10000000ULL;
705 #endif
706 priority = pbi.BasePriority;
707 unsigned page_size = getsystempagesize ();
708 vmsize = vmc.PagefileUsage;
709 vmrss = vmc.WorkingSetSize / page_size;
710 vmmaxrss = ql.MaximumWorkingSetSize / page_size;
711
712 return __small_sprintf (destbuf, "%d (%s) %c "
713 "%d %d %d %d %d "
714 "%lu %lu %lu %lu %lu %lu %lu "
715 "%ld %ld %ld %ld %ld %ld "
716 "%lu %lu "
717 "%ld "
718 "%lu",
719 p->pid, cmd,
720 state,
721 p->ppid, p->pgid, p->sid, makedev (FH_TTYS, p->ctty),
722 -1, 0, fault_count, fault_count, 0, 0, utime, stime,
723 utime, stime, priority, 0, 0, 0,
724 start_time, vmsize,
725 vmrss, vmmaxrss
726 );
727 }
728
729 static _off64_t
730 format_process_status (_pinfo *p, char *destbuf, size_t maxsize)
731 {
732 char cmd[NAME_MAX + 1];
733 int state = 'R';
734 const char *state_str = "unknown";
735 unsigned long vmsize = 0UL, vmrss = 0UL, vmdata = 0UL, vmlib = 0UL, vmtext = 0UL,
736 vmshare = 0UL;
737 if (p->process_state & PID_EXITED)
738 strcpy (cmd, "<defunct>");
739 else
740 {
741 char *last_slash = strrchr (p->progname, '\\');
742 strcpy (cmd, last_slash ? last_slash + 1 : p->progname);
743 int len = strlen (cmd);
744 if (len > 4)
745 {
746 char *s = cmd + len - 4;
747 if (ascii_strcasematch (s, ".exe"))
748 *s = 0;
749 }
750 }
751 /*
752 * Note: under Windows, a _process_ is always running - it's only _threads_
753 * that get suspended. Therefore the default state is R (runnable).
754 */
755 if (p->process_state & PID_EXITED)
756 state = 'Z';
757 else if (p->process_state & PID_STOPPED)
758 state = 'T';
759 else
760 state = get_process_state (p->dwProcessId);
761 switch (state)
762 {
763 case 'O':
764 state_str = "running";
765 break;
766 case 'D':
767 case 'S':
768 state_str = "sleeping";
769 break;
770 case 'R':
771 state_str = "runnable";
772 break;
773 case 'Z':
774 state_str = "zombie";
775 break;
776 case 'T':
777 state_str = "stopped";
778 break;
779 }
780 if (!get_mem_values (p->dwProcessId, &vmsize, &vmrss, &vmtext, &vmdata,
781 &vmlib, &vmshare))
782 return 0;
783 unsigned page_size = getsystempagesize ();
784 vmsize *= page_size; vmrss *= page_size; vmdata *= page_size;
785 vmtext *= page_size; vmlib *= page_size;
786 // The real uid value for *this* process is stored at cygheap->user.real_uid
787 // but we can't get at the real uid value for any other process, so
788 // just fake it as p->uid. Similar for p->gid.
789 return __small_sprintf (destbuf, "Name:\t%s\n"
790 "State:\t%c (%s)\n"
791 "Tgid:\t%d\n"
792 "Pid:\t%d\n"
793 "PPid:\t%d\n"
794 "Uid:\t%d %d %d %d\n"
795 "Gid:\t%d %d %d %d\n"
796 "VmSize:\t%8d kB\n"
797 "VmLck:\t%8d kB\n"
798 "VmRSS:\t%8d kB\n"
799 "VmData:\t%8d kB\n"
800 "VmStk:\t%8d kB\n"
801 "VmExe:\t%8d kB\n"
802 "VmLib:\t%8d kB\n"
803 "SigPnd:\t%016x\n"
804 "SigBlk:\t%016x\n"
805 "SigIgn:\t%016x\n",
806 cmd,
807 state, state_str,
808 p->pgid,
809 p->pid,
810 p->ppid,
811 p->uid, p->uid, p->uid, p->uid,
812 p->gid, p->gid, p->gid, p->gid,
813 vmsize >> 10, 0, vmrss >> 10, vmdata >> 10, 0,
814 vmtext >> 10, vmlib >> 10,
815 0, 0, _my_tls.sigmask
816 );
817 }
818
819 static _off64_t
820 format_process_statm (_pinfo *p, char *destbuf, size_t maxsize)
821 {
822 unsigned long vmsize = 0UL, vmrss = 0UL, vmtext = 0UL, vmdata = 0UL,
823 vmlib = 0UL, vmshare = 0UL;
824 if (!get_mem_values (p->dwProcessId, &vmsize, &vmrss, &vmtext, &vmdata,
825 &vmlib, &vmshare))
826 return 0;
827 return __small_sprintf (destbuf, "%ld %ld %ld %ld %ld %ld %ld",
828 vmsize, vmrss, vmshare, vmtext, vmlib, vmdata, 0);
829 }
830
831 static int
832 get_process_state (DWORD dwProcessId)
833 {
834 /*
835 * This isn't really heavy magic - just go through the processes'
836 * threads one by one and return a value accordingly
837 * Errors are silently ignored.
838 */
839 NTSTATUS ret;
840 SYSTEM_PROCESSES *sp;
841 ULONG n = 0x1000;
842 PULONG p = new ULONG[n];
843 int state =' ';
844 while (STATUS_INFO_LENGTH_MISMATCH ==
845 (ret = NtQuerySystemInformation (SystemProcessesAndThreadsInformation,
846 (PVOID) p,
847 n * sizeof *p, NULL)))
848 delete [] p, p = new ULONG[n *= 2];
849 if (ret != STATUS_SUCCESS)
850 {
851 debug_printf ("NtQuerySystemInformation: ret %d, Dos(ret) %d",
852 ret, RtlNtStatusToDosError (ret));
853 goto out;
854 }
855 state = 'Z';
856 sp = (SYSTEM_PROCESSES *) p;
857 for (;;)
858 {
859 if (sp->ProcessId == dwProcessId)
860 {
861 SYSTEM_THREADS *st;
862 if (wincap.has_process_io_counters ())
863 /*
864 * Windows 2000 and XP have an extra member in SYSTEM_PROCESSES
865 * which means the offset of the first SYSTEM_THREADS entry is
866 * different on these operating systems compared to NT 4.
867 */
868 st = &sp->Threads[0];
869 else
870 /*
871 * 136 is the offset of the first SYSTEM_THREADS entry on
872 * Windows NT 4.
873 */
874 st = (SYSTEM_THREADS *) ((char *) sp + 136);
875 state = 'S';
876 for (unsigned i = 0; i < sp->ThreadCount; i++)
877 {
878 if (st->State == StateRunning ||
879 st->State == StateReady)
880 {
881 state = 'R';
882 goto out;
883 }
884 st++;
885 }
886 break;
887 }
888 if (!sp->NextEntryDelta)
889 break;
890 sp = (SYSTEM_PROCESSES *) ((char *) sp + sp->NextEntryDelta);
891 }
892 out:
893 delete [] p;
894 return state;
895 }
896
897 static bool
898 get_mem_values (DWORD dwProcessId, unsigned long *vmsize, unsigned long *vmrss,
899 unsigned long *vmtext, unsigned long *vmdata,
900 unsigned long *vmlib, unsigned long *vmshare)
901 {
902 bool res = true;
903 NTSTATUS ret;
904 HANDLE hProcess;
905 VM_COUNTERS vmc;
906 MEMORY_WORKING_SET_LIST *mwsl;
907 ULONG n = 0x1000, length;
908 PULONG p = (PULONG) malloc (sizeof (ULONG) * n);
909 unsigned page_size = getsystempagesize ();
910 hProcess = OpenProcess (PROCESS_QUERY_INFORMATION,
911 FALSE, dwProcessId);
912 if (hProcess == NULL)
913 {
914 DWORD error = GetLastError ();
915 __seterrno_from_win_error (error);
916 debug_printf ("OpenProcess: ret %d", error);
917 return false;
918 }
919 while ((ret = NtQueryVirtualMemory (hProcess, 0,
920 MemoryWorkingSetList,
921 (PVOID) p,
922 n * sizeof *p, &length)),
923 (ret == STATUS_SUCCESS || ret == STATUS_INFO_LENGTH_MISMATCH) &&
924 length >= (n * sizeof (*p)))
925 p = (PULONG) realloc (p, n *= (2 * sizeof (ULONG)));
926
927 if (ret != STATUS_SUCCESS)
928 {
929 debug_printf ("NtQueryVirtualMemory: ret %d, Dos(ret) %d",
930 ret, RtlNtStatusToDosError (ret));
931 res = false;
932 goto out;
933 }
934 mwsl = (MEMORY_WORKING_SET_LIST *) p;
935 for (unsigned long i = 0; i < mwsl->NumberOfPages; i++)
936 {
937 ++*vmrss;
938 unsigned flags = mwsl->WorkingSetList[i] & 0x0FFF;
939 if (flags & (WSLE_PAGE_EXECUTE | WSLE_PAGE_SHAREABLE) == (WSLE_PAGE_EXECUTE | WSLE_PAGE_SHAREABLE))
940 ++*vmlib;
941 else if (flags & WSLE_PAGE_SHAREABLE)
942 ++*vmshare;
943 else if (flags & WSLE_PAGE_EXECUTE)
944 ++*vmtext;
945 else
946 ++*vmdata;
947 }
948 ret = NtQueryInformationProcess (hProcess, ProcessVmCounters, (PVOID) &vmc,
949 sizeof vmc, NULL);
950 if (ret != STATUS_SUCCESS)
951 {
952 debug_printf ("NtQueryInformationProcess: ret %d, Dos(ret) %d",
953 ret, RtlNtStatusToDosError (ret));
954 res = false;
955 goto out;
956 }
957 *vmsize = vmc.PagefileUsage / page_size;
958 out:
959 free (p);
960 CloseHandle (hProcess);
961 return res;
962 }
This page took 0.080442 seconds and 5 git commands to generate.