]> git.sven.stormbind.net Git - sven/jattach.git/blob - src/posix/jattach.c
Update upstream source from tag 'upstream/2.0'
[sven/jattach.git] / src / posix / jattach.c
1 /*
2  * Copyright 2021 Andrei Pangin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <signal.h>
20 #include <unistd.h>
21 #include "psutil.h"
22
23
24 extern int is_openj9_process(int pid);
25 extern int jattach_openj9(int pid, int nspid, int argc, char** argv);
26 extern int jattach_hotspot(int pid, int nspid, int argc, char** argv);
27
28
29 __attribute__((visibility("default")))
30 int jattach(int pid, int argc, char** argv) {
31     uid_t my_uid = geteuid();
32     gid_t my_gid = getegid();
33     uid_t target_uid = my_uid;
34     gid_t target_gid = my_gid;
35     int nspid;
36     if (get_process_info(pid, &target_uid, &target_gid, &nspid) < 0) {
37         fprintf(stderr, "Process %d not found\n", pid);
38         return 1;
39     }
40
41     // Container support: switch to the target namespaces.
42     // Network and IPC namespaces are essential for OpenJ9 connection.
43     enter_ns(pid, "net");
44     enter_ns(pid, "ipc");
45     int mnt_changed = enter_ns(pid, "mnt");
46
47     // In HotSpot, dynamic attach is allowed only for the clients with the same euid/egid.
48     // If we are running under root, switch to the required euid/egid automatically.
49     if ((my_gid != target_gid && setegid(target_gid) != 0) ||
50         (my_uid != target_uid && seteuid(target_uid) != 0)) {
51         perror("Failed to change credentials to match the target process");
52         return 1;
53     }
54
55     get_tmp_path(mnt_changed > 0 ? nspid : pid);
56
57     // Make write() return EPIPE instead of abnormal process termination
58     signal(SIGPIPE, SIG_IGN);
59
60     if (is_openj9_process(nspid)) {
61         return jattach_openj9(pid, nspid, argc, argv);
62     } else {
63         return jattach_hotspot(pid, nspid, argc, argv);
64     }
65 }
66
67 int main(int argc, char** argv) {
68     if (argc < 3) {
69         printf("jattach " JATTACH_VERSION " built on " __DATE__ "\n"
70                "Copyright 2021 Andrei Pangin\n"
71                "\n"
72                "Usage: jattach <pid> <cmd> [args ...]\n"
73                "\n"
74                "Commands:\n"
75                "    load  threaddump   dumpheap  setflag    properties\n"
76                "    jcmd  inspectheap  datadump  printflag  agentProperties\n"
77                );
78         return 1;
79     }
80
81     int pid = atoi(argv[1]);
82     if (pid <= 0) {
83         fprintf(stderr, "%s is not a valid process ID\n", argv[1]);
84         return 1;
85     }
86
87     return jattach(pid, argc - 2, argv + 2);
88 }