1 | /* $Id: RTThreadGetNativeState-linux.cpp 25638 2010-01-04 16:08:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTThreadGetNativeState, linux implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #define LOG_GROUP RTLOGGROUP_PROCESS
|
---|
36 | #include <iprt/thread.h>
|
---|
37 | #include "internal/iprt.h"
|
---|
38 |
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/ctype.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 |
|
---|
44 | #include "internal/thread.h"
|
---|
45 |
|
---|
46 | #include <unistd.h>
|
---|
47 | #include <sys/fcntl.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | RTDECL(RTTHREADNATIVESTATE) RTThreadGetNativeState(RTTHREAD hThread)
|
---|
51 | {
|
---|
52 | RTTHREADNATIVESTATE enmRet = RTTHREADNATIVESTATE_INVALID;
|
---|
53 | PRTTHREADINT pThread = rtThreadGet(hThread);
|
---|
54 | if (pThread)
|
---|
55 | {
|
---|
56 | enmRet = RTTHREADNATIVESTATE_UNKNOWN;
|
---|
57 |
|
---|
58 | char szName[512];
|
---|
59 | RTStrPrintf(szName, sizeof(szName), "/proc/self/task/%u/stat", pThread->tid);
|
---|
60 | int fd = open(szName, O_RDONLY, 0);
|
---|
61 | if (fd >= 0)
|
---|
62 | {
|
---|
63 | ssize_t cch = read(fd, szName, sizeof(szName) - 1);
|
---|
64 | close(fd);
|
---|
65 | if (cch > 0)
|
---|
66 | {
|
---|
67 | szName[cch] = '\0';
|
---|
68 |
|
---|
69 | /* skip the pid, the (comm name) and stop at the status char. */
|
---|
70 | const char *psz = szName;
|
---|
71 | while ( *psz
|
---|
72 | && ( *psz != ')'
|
---|
73 | || !RT_C_IS_SPACE(psz[1])
|
---|
74 | || !RT_C_IS_ALPHA(psz[2])
|
---|
75 | || !RT_C_IS_SPACE(psz[3])
|
---|
76 | )
|
---|
77 | )
|
---|
78 | psz++;
|
---|
79 | if (*psz == ')')
|
---|
80 | {
|
---|
81 | switch (psz[2])
|
---|
82 | {
|
---|
83 | case 'R': /* running */
|
---|
84 | enmRet = RTTHREADNATIVESTATE_RUNNING;
|
---|
85 | break;
|
---|
86 |
|
---|
87 | case 'S': /* sleeping */
|
---|
88 | case 'D': /* disk sleeping */
|
---|
89 | enmRet = RTTHREADNATIVESTATE_BLOCKED;
|
---|
90 | break;
|
---|
91 |
|
---|
92 | case 'T': /* stopped or tracking stop */
|
---|
93 | enmRet = RTTHREADNATIVESTATE_SUSPENDED;
|
---|
94 | break;
|
---|
95 |
|
---|
96 | case 'Z': /* zombie */
|
---|
97 | case 'X': /* dead */
|
---|
98 | enmRet = RTTHREADNATIVESTATE_TERMINATED;
|
---|
99 | break;
|
---|
100 |
|
---|
101 | default:
|
---|
102 | AssertMsgFailed(("state=%c\n", psz[2]));
|
---|
103 | enmRet = RTTHREADNATIVESTATE_UNKNOWN;
|
---|
104 | break;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | else
|
---|
108 | AssertMsgFailed(("stat='%s'\n", szName));
|
---|
109 | }
|
---|
110 | }
|
---|
111 | rtThreadRelease(pThread);
|
---|
112 | }
|
---|
113 | return enmRet;
|
---|
114 | }
|
---|
115 |
|
---|