]> git.sven.stormbind.net Git - sven/jattach.git/blob - src/posix/jattach.c
releasing package jattach version 2.2-1
[sven/jattach.git] / src / posix / jattach.c
1 /*
2  * Copyright jattach authors
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, int print_output);
26 extern int jattach_hotspot(int pid, int nspid, int argc, char** argv, int print_output);
27
28 int mnt_changed = 0;
29
30
31 __attribute__((visibility("default")))
32 int jattach(int pid, int argc, char** argv, int print_output) {
33     uid_t my_uid = geteuid();
34     gid_t my_gid = getegid();
35     uid_t target_uid = my_uid;
36     gid_t target_gid = my_gid;
37     int nspid;
38     if (get_process_info(pid, &target_uid, &target_gid, &nspid) < 0) {
39         fprintf(stderr, "Process %d not found\n", pid);
40         return 1;
41     }
42
43     // Container support: switch to the target namespaces.
44     // Network and IPC namespaces are essential for OpenJ9 connection.
45     enter_ns(pid, "net");
46     enter_ns(pid, "ipc");
47     mnt_changed = enter_ns(pid, "mnt");
48
49     // In HotSpot, dynamic attach is allowed only for the clients with the same euid/egid.
50     // If we are running under root, switch to the required euid/egid automatically.
51     if ((my_gid != target_gid && setegid(target_gid) != 0) ||
52         (my_uid != target_uid && seteuid(target_uid) != 0)) {
53         perror("Failed to change credentials to match the target process");
54         return 1;
55     }
56
57     get_tmp_path(mnt_changed > 0 ? nspid : pid);
58
59     // Make write() return EPIPE instead of abnormal process termination
60     signal(SIGPIPE, SIG_IGN);
61
62     if (is_openj9_process(nspid)) {
63         return jattach_openj9(pid, nspid, argc, argv, print_output);
64     } else {
65         return jattach_hotspot(pid, nspid, argc, argv, print_output);
66     }
67 }
68
69 #ifdef JATTACH_VERSION
70
71 int main(int argc, char** argv) {
72     if (argc < 3) {
73         printf("jattach " JATTACH_VERSION " built on " __DATE__ "\n"
74                "\n"
75                "Usage: jattach <pid> <cmd> [args ...]\n"
76                "\n"
77                "Commands:\n"
78                "    load  threaddump   dumpheap  setflag    properties\n"
79                "    jcmd  inspectheap  datadump  printflag  agentProperties\n"
80                );
81         return 1;
82     }
83
84     int pid = atoi(argv[1]);
85     if (pid <= 0) {
86         fprintf(stderr, "%s is not a valid process ID\n", argv[1]);
87         return 1;
88     }
89
90     return jattach(pid, argc - 2, argv + 2, 1);
91 }
92
93 #endif // JATTACH_VERSION