src/hotspot/os/linux/attachListener_linux.cpp
author michaelm
Thu, 14 Nov 2019 12:40:13 +0000
branchunixdomainchannels
changeset 59079 7893d1012580
parent 55682 70fab3a8ff02
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
55682
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
     2
 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5410
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5410
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5410
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6965
diff changeset
    25
#include "precompiled.hpp"
49593
4dd58ecc9912 8200105: Remove cyclic dependency between oop.inline.hpp and collectedHeap.inline.hpp
stefank
parents: 49449
diff changeset
    26
#include "logging/log.hpp"
49449
ef5d5d343e2a 8199263: Split interfaceSupport.hpp to not require including .inline.hpp files
coleenp
parents: 47216
diff changeset
    27
#include "memory/allocation.inline.hpp"
ef5d5d343e2a 8199263: Split interfaceSupport.hpp to not require including .inline.hpp files
coleenp
parents: 47216
diff changeset
    28
#include "runtime/interfaceSupport.inline.hpp"
25468
5331df506290 8048241: Introduce umbrella header os.inline.hpp and clean up includes
goetz
parents: 18931
diff changeset
    29
#include "runtime/os.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6965
diff changeset
    30
#include "services/attachListener.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6965
diff changeset
    31
#include "services/dtraceAttacher.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
#include <unistd.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
#include <signal.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
#include <sys/types.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
#include <sys/socket.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
#include <sys/un.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
#include <sys/stat.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
    40
#ifndef UNIX_PATH_MAX
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
    41
#define UNIX_PATH_MAX   sizeof(((struct sockaddr_un *)0)->sun_path)
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
    42
#endif
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
    43
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
// The attach mechanism on Linux uses a UNIX domain socket. An attach listener
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
// thread is created at startup or is created on-demand via a signal from
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
// the client tool. The attach listener creates a socket and binds it to a file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
// in the filesystem. The attach listener then acts as a simple (single-
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
    48
// threaded) server - it waits for a client to connect, reads the request,
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
// executes it, and returns the response to the client via the socket
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
// connection.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
// As the socket is a UNIX domain socket it means that only clients on the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
// local machine can connect. In addition there are two other aspects to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
// the security:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
// 1. The well known file that the socket is bound to has permission 400
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
// 2. When a client connect, the SO_PEERCRED socket option is used to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
//    obtain the credentials of client. We check that the effective uid
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
//    of the client matches this process.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
// forward reference
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
class LinuxAttachOperation;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
class LinuxAttachListener: AllStatic {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  // the path to which we bind the UNIX domain socket
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
    66
  static char _path[UNIX_PATH_MAX];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  static bool _has_path;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  // the file descriptor for the listening socket
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  static int _listener;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
55682
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    72
  static bool _atexit_registered;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  // reads a request from the given connected socket
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  static LinuxAttachOperation* read_request(int s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    ATTACH_PROTOCOL_VER = 1                     // protocol version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    ATTACH_ERROR_BADVERSION     = 101           // error codes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
55682
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    85
  static void set_path(char* path) {
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    86
    if (path == NULL) {
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    87
      _path[0] = '\0';
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    88
      _has_path = false;
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    89
    } else {
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    90
      strncpy(_path, path, UNIX_PATH_MAX);
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    91
      _path[UNIX_PATH_MAX-1] = '\0';
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    92
      _has_path = true;
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    93
    }
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    94
  }
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    95
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    96
  static void set_listener(int s)               { _listener = s; }
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
    97
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  // initialize the listener, returns 0 if okay
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  static int init();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  static char* path()                   { return _path; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  static bool has_path()                { return _has_path; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  static int listener()                 { return _listener; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  // write the given buffer to a socket
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  static int write_fully(int s, char* buf, int len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  static LinuxAttachOperation* dequeue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
class LinuxAttachOperation: public AttachOperation {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  // the connection to the client
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  int _socket;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  void complete(jint res, bufferedStream* st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  void set_socket(int s)                                { _socket = s; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  int socket() const                                    { return _socket; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  LinuxAttachOperation(char* name) : AttachOperation(name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    set_socket(-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
// statics
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   128
char LinuxAttachListener::_path[UNIX_PATH_MAX];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
bool LinuxAttachListener::_has_path;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
int LinuxAttachListener::_listener = -1;
55682
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   131
bool LinuxAttachListener::_atexit_registered = false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
// Supporting class to help split a buffer into individual components
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
class ArgumentIterator : public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  char* _pos;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  char* _end;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  ArgumentIterator(char* arg_buffer, size_t arg_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    _pos = arg_buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    _end = _pos + arg_size - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  char* next() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    if (*_pos == '\0') {
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   145
      // advance the iterator if possible (null arguments)
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   146
      if (_pos < _end) {
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   147
        _pos += 1;
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   148
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
      return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    char* res = _pos;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    char* next_pos = strchr(_pos, '\0');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    if (next_pos < _end)  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
      next_pos++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    _pos = next_pos;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
// atexit hook to stop listener and unlink the file that it is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
// bound too.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
extern "C" {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  static void listener_cleanup() {
55682
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   166
    int s = LinuxAttachListener::listener();
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   167
    if (s != -1) {
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   168
      LinuxAttachListener::set_listener(-1);
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   169
      ::shutdown(s, SHUT_RDWR);
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   170
      ::close(s);
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   171
    }
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   172
    if (LinuxAttachListener::has_path()) {
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   173
      ::unlink(LinuxAttachListener::path());
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   174
      LinuxAttachListener::set_path(NULL);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
// Initialization - create a listener socket and bind it to a file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
int LinuxAttachListener::init() {
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   182
  char path[UNIX_PATH_MAX];          // socket file
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   183
  char initial_path[UNIX_PATH_MAX];  // socket file during setup
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   184
  int listener;                      // listener socket (file descriptor)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  // register function to cleanup
55682
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   187
  if (!_atexit_registered) {
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   188
    _atexit_registered = true;
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   189
    ::atexit(listener_cleanup);
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   190
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   192
  int n = snprintf(path, UNIX_PATH_MAX, "%s/.java_pid%d",
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   193
                   os::get_temp_directory(), os::current_process_id());
6965
4ef36b2a6a3a 6989297: Integrate additional portability improvements
bobv
parents: 6170
diff changeset
   194
  if (n < (int)UNIX_PATH_MAX) {
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   195
    n = snprintf(initial_path, UNIX_PATH_MAX, "%s.tmp", path);
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   196
  }
6965
4ef36b2a6a3a 6989297: Integrate additional portability improvements
bobv
parents: 6170
diff changeset
   197
  if (n >= (int)UNIX_PATH_MAX) {
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   198
    return -1;
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   199
  }
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   200
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  // create the listener socket
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  listener = ::socket(PF_UNIX, SOCK_STREAM, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  if (listener == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   207
  // bind socket
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  struct sockaddr_un addr;
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   209
  memset((void *)&addr, 0, sizeof(addr));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  addr.sun_family = AF_UNIX;
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   211
  strcpy(addr.sun_path, initial_path);
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   212
  ::unlink(initial_path);
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   213
  int res = ::bind(listener, (struct sockaddr*)&addr, sizeof(addr));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  if (res == -1) {
18078
10417cf9967d 7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents: 7397
diff changeset
   215
    ::close(listener);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   219
  // put in listen mode, set permissions, and rename into place
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   220
  res = ::listen(listener, 5);
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   221
  if (res == 0) {
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   222
    RESTARTABLE(::chmod(initial_path, S_IREAD|S_IWRITE), res);
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   223
    if (res == 0) {
50067
88b76c19d8eb 8202650: Enforce group for attach listener file
clanger
parents: 49742
diff changeset
   224
      // make sure the file is owned by the effective user and effective group
88b76c19d8eb 8202650: Enforce group for attach listener file
clanger
parents: 49742
diff changeset
   225
      // e.g. the group could be inherited from the directory in case the s bit is set
88b76c19d8eb 8202650: Enforce group for attach listener file
clanger
parents: 49742
diff changeset
   226
      RESTARTABLE(::chown(initial_path, geteuid(), getegid()), res);
88b76c19d8eb 8202650: Enforce group for attach listener file
clanger
parents: 49742
diff changeset
   227
      if (res == 0) {
88b76c19d8eb 8202650: Enforce group for attach listener file
clanger
parents: 49742
diff changeset
   228
        res = ::rename(initial_path, path);
88b76c19d8eb 8202650: Enforce group for attach listener file
clanger
parents: 49742
diff changeset
   229
      }
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   230
    }
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   231
  }
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   232
  if (res == -1) {
18078
10417cf9967d 7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents: 7397
diff changeset
   233
    ::close(listener);
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   234
    ::unlink(initial_path);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  }
6170
82aea418ab2d 6649594: Intermittent IOExceptions during dynamic attach on linux and solaris
alanb
parents: 5547
diff changeset
   237
  set_path(path);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  set_listener(listener);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
// Given a socket that is connected to a peer we read the request and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
// create an AttachOperation. As the socket is blocking there is potential
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
// for a denial-of-service if the peer does not response. However this happens
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
// after the peer credentials have been checked and in the worst case it just
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
// means that the attach listener thread is blocked.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
LinuxAttachOperation* LinuxAttachListener::read_request(int s) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  char ver_str[8];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  sprintf(ver_str, "%d", ATTACH_PROTOCOL_VER);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  // The request is a sequence of strings so we first figure out the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  // expected count and the maximum possible length of the request.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  // The request is:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  //   <ver>0<cmd>0<arg>0<arg>0<arg>0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  // where <ver> is the protocol version (1), <cmd> is the command
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  // name ("load", "datadump", ...), and <arg> is an argument
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  int expected_str_count = 2 + AttachOperation::arg_count_max;
223
5c3b023117d9 6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents: 1
diff changeset
   260
  const int max_len = (sizeof(ver_str) + 1) + (AttachOperation::name_length_max + 1) +
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
    AttachOperation::arg_count_max*(AttachOperation::arg_length_max + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  char buf[max_len];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  int str_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  // Read until all (expected) strings have been read, the buffer is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  // full, or EOF.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  int off = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  int left = max_len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
    int n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
    RESTARTABLE(read(s, buf+off, left), n);
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 25468
diff changeset
   275
    assert(n <= left, "buffer was too small, impossible!");
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 25468
diff changeset
   276
    buf[max_len - 1] = '\0';
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
    if (n == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
      return NULL;      // reset by peer or other error
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
    if (n == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
    for (int i=0; i<n; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
      if (buf[off+i] == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
        // EOS found
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
        str_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
        // The first string is <ver> so check it now to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
        // check for protocol mis-match
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
        if (str_count == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
          if ((strlen(buf) != strlen(ver_str)) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
              (atoi(buf) != ATTACH_PROTOCOL_VER)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
            char msg[32];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
            sprintf(msg, "%d\n", ATTACH_ERROR_BADVERSION);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
            write_fully(s, msg, strlen(msg));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
            return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
    off += n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    left -= n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  } while (left > 0 && str_count < expected_str_count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  if (str_count != expected_str_count) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    return NULL;        // incomplete request
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  // parse request
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  ArgumentIterator args(buf, (max_len)-left);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  // version already checked
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  char* v = args.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  char* name = args.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  if (name == NULL || strlen(name) > AttachOperation::name_length_max) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  LinuxAttachOperation* op = new LinuxAttachOperation(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  for (int i=0; i<AttachOperation::arg_count_max; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
    char* arg = args.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    if (arg == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
      op->set_arg(i, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
      if (strlen(arg) > AttachOperation::arg_length_max) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
        delete op;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
        return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
      op->set_arg(i, arg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
  op->set_socket(s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  return op;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
// Dequeue an operation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
// In the Linux implementation there is only a single operation and clients
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
// cannot queue commands (except at the socket level).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
LinuxAttachOperation* LinuxAttachListener::dequeue() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    int s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    // wait for client to connect
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    struct sockaddr addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
    socklen_t len = sizeof(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    RESTARTABLE(::accept(listener(), &addr, &len), s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
    if (s == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
      return NULL;      // log a warning?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    // get the credentials of the peer and check the effective uid/guid
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    struct ucred cred_info;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    socklen_t optlen = sizeof(cred_info);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    if (::getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void*)&cred_info, &optlen) == -1) {
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   362
      log_debug(attach)("Failed to get socket option SO_PEERCRED");
18078
10417cf9967d 7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents: 7397
diff changeset
   363
      ::close(s);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
50333
7cea35f78b50 8197387: jcmd started by "root" must be allowed to access all VM processes
dtitov
parents: 50067
diff changeset
   367
    if (!os::Posix::matches_effective_uid_and_gid_or_root(cred_info.uid, cred_info.gid)) {
7cea35f78b50 8197387: jcmd started by "root" must be allowed to access all VM processes
dtitov
parents: 50067
diff changeset
   368
      log_debug(attach)("euid/egid check failed (%d/%d vs %d/%d)",
7cea35f78b50 8197387: jcmd started by "root" must be allowed to access all VM processes
dtitov
parents: 50067
diff changeset
   369
              cred_info.uid, cred_info.gid, geteuid(), getegid());
18078
10417cf9967d 7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents: 7397
diff changeset
   370
      ::close(s);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
    // peer credential look okay so we read the request
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    LinuxAttachOperation* op = read_request(s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    if (op == NULL) {
18078
10417cf9967d 7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents: 7397
diff changeset
   377
      ::close(s);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
      return op;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
// write the given buffer to the socket
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
int LinuxAttachListener::write_fully(int s, char* buf, int len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    int n = ::write(s, buf, len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    if (n == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
      if (errno != EINTR) return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
      buf += n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
      len -= n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  while (len > 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
// Complete an operation by sending the operation result and any result
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
// output to the client. At this time the socket is in blocking mode so
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
// potentially we can block if there is a lot of data and the client is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
// non-responsive. For most operations this is a non-issue because the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
// default send buffer is sufficient to buffer everything. In the future
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
// if there are operations that involves a very big reply then it the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
// socket could be made non-blocking and a timeout could be used.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
void LinuxAttachOperation::complete(jint result, bufferedStream* st) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  JavaThread* thread = JavaThread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  ThreadBlockInVM tbivm(thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  thread->set_suspend_equivalent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  // cleared by handle_special_suspend_equivalent_condition() or
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
  // java_suspend_self() via check_and_wait_while_suspended()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  // write operation result
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  char msg[32];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  sprintf(msg, "%d\n", result);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  int rc = LinuxAttachListener::write_fully(this->socket(), msg, strlen(msg));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  // write any result data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
  if (rc == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
    LinuxAttachListener::write_fully(this->socket(), (char*) st->base(), st->size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
    ::shutdown(this->socket(), 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  // done
18078
10417cf9967d 7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents: 7397
diff changeset
   428
  ::close(this->socket());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  // were we externally suspended while we were waiting?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  thread->check_and_wait_while_suspended();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  delete this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
// AttachListener functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
AttachOperation* AttachListener::dequeue() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
  JavaThread* thread = JavaThread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  ThreadBlockInVM tbivm(thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
  thread->set_suspend_equivalent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
  // cleared by handle_special_suspend_equivalent_condition() or
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  // java_suspend_self() via check_and_wait_while_suspended()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  AttachOperation* op = LinuxAttachListener::dequeue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  // were we externally suspended while we were waiting?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  thread->check_and_wait_while_suspended();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
  return op;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
18931
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   455
// Performs initialization at vm startup
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   456
// For Linux we remove any stale .java_pid file which could cause
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   457
// an attaching process to think we are ready to receive on the
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   458
// domain socket before we are properly initialized
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   459
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   460
void AttachListener::vm_start() {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   461
  char fn[UNIX_PATH_MAX];
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   462
  struct stat64 st;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   463
  int ret;
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   464
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   465
  int n = snprintf(fn, UNIX_PATH_MAX, "%s/.java_pid%d",
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   466
           os::get_temp_directory(), os::current_process_id());
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   467
  assert(n < (int)UNIX_PATH_MAX, "java_pid file name buffer overflow");
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   468
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   469
  RESTARTABLE(::stat64(fn, &st), ret);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   470
  if (ret == 0) {
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   471
    ret = ::unlink(fn);
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   472
    if (ret == -1) {
40625
6b45fb9188f9 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 33794
diff changeset
   473
      log_debug(attach)("Failed to remove stale attach pid file at %s", fn);
18931
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   474
    }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   475
  }
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   476
}
5de3638104b4 7162400: Intermittent java.io.IOException: Bad file number during HotSpotVirtualMachine.executeCommand
allwin
parents: 18078
diff changeset
   477
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
int AttachListener::pd_init() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
  JavaThread* thread = JavaThread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
  ThreadBlockInVM tbivm(thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  thread->set_suspend_equivalent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  // cleared by handle_special_suspend_equivalent_condition() or
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  // java_suspend_self() via check_and_wait_while_suspended()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
  int ret_code = LinuxAttachListener::init();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  // were we externally suspended while we were waiting?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  thread->check_and_wait_while_suspended();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  return ret_code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
55682
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   494
bool AttachListener::check_socket_file() {
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   495
  int ret;
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   496
  struct stat64 st;
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   497
  ret = stat64(LinuxAttachListener::path(), &st);
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   498
  if (ret == -1) { // need to restart attach listener.
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   499
    log_debug(attach)("Socket file %s does not exist - Restart Attach Listener",
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   500
                      LinuxAttachListener::path());
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   501
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   502
    listener_cleanup();
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   503
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   504
    // wait to terminate current attach listener instance...
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   505
    while (AttachListener::transit_state(AL_INITIALIZING,
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   506
                                         AL_NOT_INITIALIZED) != AL_NOT_INITIALIZED) {
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   507
      os::naked_yield();
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   508
    }
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   509
    return is_init_trigger();
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   510
  }
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   511
  return false;
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   512
}
70fab3a8ff02 8225690: Multiple AttachListener threads can be created
ysuenaga
parents: 50333
diff changeset
   513
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
// Attach Listener is started lazily except in the case when
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
// +ReduseSignalUsage is used
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
bool AttachListener::init_at_startup() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  if (ReduceSignalUsage) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
// If the file .attach_pid<pid> exists in the working directory
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
// or /tmp then this is the trigger to start the attach mechanism
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
bool AttachListener::is_init_trigger() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
  if (init_at_startup() || is_initialized()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
    return false;               // initialized at startup or already initialized
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
  }
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   530
  char fn[PATH_MAX + 1];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
  int ret;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  struct stat64 st;
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   533
  sprintf(fn, ".attach_pid%d", os::current_process_id());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
  RESTARTABLE(::stat64(fn, &st), ret);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
  if (ret == -1) {
40625
6b45fb9188f9 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 33794
diff changeset
   536
    log_trace(attach)("Failed to find attach file: %s, trying alternate", fn);
5237
aab592fd4f44 6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents: 670
diff changeset
   537
    snprintf(fn, sizeof(fn), "%s/.attach_pid%d",
aab592fd4f44 6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents: 670
diff changeset
   538
             os::get_temp_directory(), os::current_process_id());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
    RESTARTABLE(::stat64(fn, &st), ret);
40625
6b45fb9188f9 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 33794
diff changeset
   540
    if (ret == -1) {
6b45fb9188f9 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 33794
diff changeset
   541
      log_debug(attach)("Failed to find attach file: %s", fn);
6b45fb9188f9 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 33794
diff changeset
   542
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
  if (ret == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
    // simple check to avoid starting the attach mechanism when
50333
7cea35f78b50 8197387: jcmd started by "root" must be allowed to access all VM processes
dtitov
parents: 50067
diff changeset
   546
    // a bogus non-root user creates the file
7cea35f78b50 8197387: jcmd started by "root" must be allowed to access all VM processes
dtitov
parents: 50067
diff changeset
   547
    if (os::Posix::matches_effective_uid_or_root(st.st_uid)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
      init();
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   549
      log_trace(attach)("Attach triggered by %s", fn);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
      return true;
40625
6b45fb9188f9 8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents: 33794
diff changeset
   551
    } else {
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 49593
diff changeset
   552
      log_debug(attach)("File %s has wrong user id %d (vs %d). Attach is not triggered", fn, st.st_uid, geteuid());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
// if VM aborts then remove listener
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
void AttachListener::abort() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
  listener_cleanup();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
void AttachListener::pd_data_dump() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
  os::signal_notify(SIGQUIT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* n) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
  out->print_cr("flag '%s' cannot be changed", op->arg(0));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
  return JNI_ERR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
void AttachListener::pd_detachall() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
  // do nothing for now
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
}