aboutsummaryrefslogtreecommitdiff
path: root/src/os/solaris/vm/attachListener_solaris.cpp
blob: a88f8861fbdb437daaa79e5ec3f3716ad005102b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

# include "incls/_precompiled.incl"
# include "incls/_attachListener_solaris.cpp.incl"

#include <door.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>

// stropts.h uses STR in stream ioctl defines
#undef STR
#include <stropts.h>
#undef STR
#define STR(a) #a

// The attach mechanism on Solaris is implemented using the Doors IPC
// mechanism. The first tool to attempt to attach causes the attach
// listener thread to startup. This thread creats a door that is
// associated with a function that enqueues an operation to the attach
// listener. The door is attached to a file in the file system so that
// client (tools) can locate it. To enqueue an operation to the VM the
// client calls through the door which invokes the enqueue function in
// this process. The credentials of the client are checked and if the
// effective uid matches this process then the operation is enqueued.
// When an operation completes the attach listener is required to send the
// operation result and any result data to the client. In this implementation
// the result is returned via a UNIX domain socket. A pair of connected
// sockets (socketpair) is created in the enqueue function and the file
// descriptor for one of the sockets is returned to the client as the
// return from the door call. The other end is retained in this process.
// When the operation completes the result is sent to the client and
// the socket is closed.

// forward reference
class SolarisAttachOperation;

class SolarisAttachListener: AllStatic {
 private:

  // the path to which we attach the door file descriptor
  static char _door_path[PATH_MAX+1];
  static volatile bool _has_door_path;

  // door descriptor returned by door_create
  static int _door_descriptor;

  static void set_door_path(char* path) {
    if (path == NULL) {
      _has_door_path = false;
    } else {
      strncpy(_door_path, path, PATH_MAX);
      _door_path[PATH_MAX] = '\0';      // ensure it's nul terminated
      _has_door_path = true;
    }
  }

  static void set_door_descriptor(int dd)               { _door_descriptor = dd; }

  // mutex to protect operation list
  static mutex_t _mutex;

  // semaphore to wakeup listener thread
  static sema_t _wakeup;

  static mutex_t* mutex()                               { return &_mutex; }
  static sema_t* wakeup()                               { return &_wakeup; }

  // enqueued operation list
  static SolarisAttachOperation* _head;
  static SolarisAttachOperation* _tail;

  static SolarisAttachOperation* head()                 { return _head; }
  static void set_head(SolarisAttachOperation* head)    { _head = head; }

  static SolarisAttachOperation* tail()                 { return _tail; }
  static void set_tail(SolarisAttachOperation* tail)    { _tail = tail; }

  // create the door
  static int create_door();

 public:
  enum {
    ATTACH_PROTOCOL_VER = 1                             // protocol version
  };
  enum {
    ATTACH_ERROR_BADREQUEST     = 100,                  // error code returned by
    ATTACH_ERROR_BADVERSION     = 101,                  // the door call
    ATTACH_ERROR_RESOURCE       = 102,
    ATTACH_ERROR_INTERNAL       = 103,
    ATTACH_ERROR_DENIED         = 104
  };

  // initialize the listener
  static int init();

  static bool has_door_path()                           { return _has_door_path; }
  static char* door_path()                              { return _door_path; }
  static int door_descriptor()                          { return _door_descriptor; }

  // enqueue an operation
  static void enqueue(SolarisAttachOperation* op);

  // dequeue an operation
  static SolarisAttachOperation* dequeue();
};


// SolarisAttachOperation is an AttachOperation that additionally encapsulates
// a socket connection to the requesting client/tool. SolarisAttachOperation
// can additionally be held in a linked list.

class SolarisAttachOperation: public AttachOperation {
 private:
  friend class SolarisAttachListener;

  // connection to client
  int _socket;

  // linked list support
  SolarisAttachOperation* _next;

  SolarisAttachOperation* next()                         { return _next; }
  void set_next(SolarisAttachOperation* next)            { _next = next; }

 public:
  void complete(jint res, bufferedStream* st);

  int socket() const                                     { return _socket; }
  void set_socket(int s)                                 { _socket = s; }

  SolarisAttachOperation(char* name) : AttachOperation(name) {
    set_socket(-1);
    set_next(NULL);
  }
};

// statics
char SolarisAttachListener::_door_path[PATH_MAX+1];
volatile bool SolarisAttachListener::_has_door_path;
int SolarisAttachListener::_door_descriptor = -1;
mutex_t SolarisAttachListener::_mutex;
sema_t SolarisAttachListener::_wakeup;
SolarisAttachOperation* SolarisAttachListener::_head = NULL;
SolarisAttachOperation* SolarisAttachListener::_tail = NULL;

// Supporting class to help split a buffer into individual components
class ArgumentIterator : public StackObj {
 private:
  char* _pos;
  char* _end;
 public:
  ArgumentIterator(char* arg_buffer, size_t arg_size) {
    _pos = arg_buffer;
    _end = _pos + arg_size - 1;
  }
  char* next() {
    if (*_pos == '\0') {
      return NULL;
    }
    char* res = _pos;
    char* next_pos = strchr(_pos, '\0');
    if (next_pos < _end)  {
      next_pos++;
    }
    _pos = next_pos;
    return res;
  }
};

// Calls from the door function to check that the client credentials
// match this process. Returns 0 if credentials okay, otherwise -1.
static int check_credentials() {
  door_cred_t cred_info;

  // get client credentials
  if (door_cred(&cred_info) == -1) {
    return -1; // unable to get them
  }

  // get our euid/eguid (probably could cache these)
  uid_t euid = geteuid();
  gid_t egid = getegid();

  // check that the effective uid/gid matches - discuss this with Jeff.
  if (cred_info.dc_euid == euid && cred_info.dc_egid == egid) {
    return 0;  // okay
  } else {
    return -1; // denied
  }
}


// Parses the argument buffer to create an AttachOperation that we should
// enqueue to the attach listener.
// The buffer is expected to be formatted as follows:
// <ver>0<cmd>0<arg>0<arg>0<arg>0
// where <ver> is the version number (must be "1"), <cmd> is the command
// name ("load, "datadump", ...) and <arg> is an argument.
//
static SolarisAttachOperation* create_operation(char* argp, size_t arg_size, int* err) {
  // assume bad request until parsed
  *err = SolarisAttachListener::ATTACH_ERROR_BADREQUEST;

  if (arg_size < 2 || argp[arg_size-1] != '\0') {
    return NULL;   // no ver or not null terminated
  }

  // Use supporting class to iterate over the buffer
  ArgumentIterator args(argp, arg_size);

  // First check the protocol version
  char* ver = args.next();
  if (ver == NULL) {
    return NULL;
  }
  if (atoi(ver) != SolarisAttachListener::ATTACH_PROTOCOL_VER) {
    *err = SolarisAttachListener::ATTACH_ERROR_BADVERSION;
    return NULL;
  }

  // Get command name and create the operation
  char* name = args.next();
  if (name == NULL || strlen(name) > AttachOperation::name_length_max) {
    return NULL;
  }
  SolarisAttachOperation* op = new SolarisAttachOperation(name);

  // Iterate over the arguments
  for (int i=0; i<AttachOperation::arg_count_max; i++) {
    char* arg = args.next();
    if (arg == NULL) {
      op->set_arg(i, NULL);
    } else {
      if (strlen(arg) > AttachOperation::arg_length_max) {
        delete op;
        return NULL;
      }
      op->set_arg(i, arg);
    }
  }

  // return operation
  *err = 0;
  return op;
}

// create special operation to indicate all clients have detached
static SolarisAttachOperation* create_detachall_operation() {
  return new SolarisAttachOperation(AttachOperation::detachall_operation_name());
}

// This is door function which the client executes via a door_call.
extern "C" {
  static void enqueue_proc(void* cookie, char* argp, size_t arg_size,
                           door_desc_t* dt, uint_t n_desc)
  {
    int return_fd = -1;
    SolarisAttachOperation* op = NULL;

    // no listener
    jint res = 0;
    if (!AttachListener::is_initialized()) {
      // how did we get here?
      debug_only(warning("door_call when not enabled"));
      res = (jint)SolarisAttachListener::ATTACH_ERROR_INTERNAL;
    }

    // check client credentials
    if (res == 0) {
      if (check_credentials() != 0) {
        res = (jint)SolarisAttachListener::ATTACH_ERROR_DENIED;
      }
    }

    // if we are stopped at ShowMessageBoxOnError then maybe we can
    // load a diagnostic library
    if (res == 0 && is_error_reported()) {
      if (ShowMessageBoxOnError) {
        // TBD - support loading of diagnostic library here
      }

      // can't enqueue operation after fatal error
      res = (jint)SolarisAttachListener::ATTACH_ERROR_RESOURCE;
    }

    // create the operation
    if (res == 0) {
      int err;
      op = create_operation(argp, arg_size, &err);
      res = (op == NULL) ? (jint)err : 0;
    }

    // create a pair of connected sockets. Store the file descriptor
    // for one end in the operation and enqueue the operation. The
    // file descriptor for the other end wil be returned to the client.
    if (res == 0) {
      int s[2];
      if (socketpair(PF_UNIX, SOCK_STREAM, 0, s) < 0) {
        delete op;
        res = (jint)SolarisAttachListener::ATTACH_ERROR_RESOURCE;
      } else {
        op->set_socket(s[0]);
        return_fd = s[1];
        SolarisAttachListener::enqueue(op);
      }
    }

    // Return 0 (success) + file descriptor, or non-0 (error)
    if (res == 0) {
      door_desc_t desc;
      desc.d_attributes = DOOR_DESCRIPTOR;
      desc.d_data.d_desc.d_descriptor = return_fd;
      door_return((char*)&res, sizeof(res), &desc, 1);
    } else {
      door_return((char*)&res, sizeof(res), NULL, 0);
    }
  }
}

// atexit hook to detach the door and remove the file
extern "C" {
  static void listener_cleanup() {
    static int cleanup_done;
    if (!cleanup_done) {
      cleanup_done = 1;
      int dd = SolarisAttachListener::door_descriptor();
      if (dd >= 0) {
        ::close(dd);
      }
      if (SolarisAttachListener::has_door_path()) {
        char* path = SolarisAttachListener::door_path();
        ::fdetach(path);
        ::unlink(path);
      }
    }
  }
}

// Create the door
int SolarisAttachListener::create_door() {
  char door_path[PATH_MAX+1];
  int fd, res;

  // register exit function
  ::atexit(listener_cleanup);

  // create the door descriptor
  int dd = ::door_create(enqueue_proc, NULL, 0);
  if (dd < 0) {
    return -1;
  }

  snprintf(door_path, sizeof(door_path), "%s/.java_pid%d",
           os::get_temp_directory(), os::current_process_id());
  RESTARTABLE(::creat(door_path, S_IRUSR | S_IWUSR), fd);

  if (fd == -1) {
    debug_only(warning("attempt to create %s failed", door_path));
    return -1;
  }
  assert(fd >= 0, "bad file descriptor");
  set_door_path(door_path);
  RESTARTABLE(::close(fd), res);

  // attach the door descriptor to the file
  if ((res = ::fattach(dd, door_path)) == -1) {
    // if busy then detach and try again
    if (errno == EBUSY) {
      ::fdetach(door_path);
      res = ::fattach(dd, door_path);
    }
    if (res == -1) {
      ::door_revoke(dd);
      dd = -1;
    }
  }
  if (dd >= 0) {
    set_door_descriptor(dd);
  } else {
    // unable to create door or attach it to the file
    ::unlink(door_path);
    set_door_path(NULL);
    return -1;
  }

  return 0;
}

// Initialization - create the door, locks, and other initialization
int SolarisAttachListener::init() {
  if (create_door()) {
    return -1;
  }

  int status = os::Solaris::mutex_init(&_mutex);
  assert_status(status==0, status, "mutex_init");

  status = ::sema_init(&_wakeup, 0, NULL, NULL);
  assert_status(status==0, status, "sema_init");

  set_head(NULL);
  set_tail(NULL);

  return 0;
}

// Dequeue an operation
SolarisAttachOperation* SolarisAttachListener::dequeue() {
  for (;;) {
    int res;

    // wait for somebody to enqueue something
    while ((res = ::sema_wait(wakeup())) == EINTR)
      ;
    if (res) {
      warning("sema_wait failed: %s", strerror(res));
      return NULL;
    }

    // lock the list
    res = os::Solaris::mutex_lock(mutex());
    assert(res == 0, "mutex_lock failed");

    // remove the head of the list
    SolarisAttachOperation* op = head();
    if (op != NULL) {
      set_head(op->next());
      if (head() == NULL) {
        set_tail(NULL);
      }
    }

    // unlock
    os::Solaris::mutex_unlock(mutex());

    // if we got an operation when return it.
    if (op != NULL) {
      return op;
    }
  }
}

// Enqueue an operation
void SolarisAttachListener::enqueue(SolarisAttachOperation* op) {
  // lock list
  int res = os::Solaris::mutex_lock(mutex());
  assert(res == 0, "mutex_lock failed");

  // enqueue at tail
  op->set_next(NULL);
  if (head() == NULL) {
    set_head(op);
  } else {
    tail()->set_next(op);
  }
  set_tail(op);

  // wakeup the attach listener
  RESTARTABLE(::sema_post(wakeup()), res);
  assert(res == 0, "sema_post failed");

  // unlock
  os::Solaris::mutex_unlock(mutex());
}


// support function - writes the (entire) buffer to a socket
static int write_fully(int s, char* buf, int len) {
  do {
    int n = ::write(s, buf, len);
    if (n == -1) {
      if (errno != EINTR) return -1;
    } else {
      buf += n;
      len -= n;
    }
  }
  while (len > 0);
  return 0;
}

// Complete an operation by sending the operation result and any result
// output to the client. At this time the socket is in blocking mode so
// potentially we can block if there is a lot of data and the client is
// non-responsive. For most operations this is a non-issue because the
// default send buffer is sufficient to buffer everything. In the future
// if there are operations that involves a very big reply then it the
// socket could be made non-blocking and a timeout could be used.

void SolarisAttachOperation::complete(jint res, bufferedStream* st) {
  if (this->socket() >= 0) {
    JavaThread* thread = JavaThread::current();
    ThreadBlockInVM tbivm(thread);

    thread->set_suspend_equivalent();
    // cleared by handle_special_suspend_equivalent_condition() or
    // java_suspend_self() via check_and_wait_while_suspended()

    // write operation result
    char msg[32];
    sprintf(msg, "%d\n", res);
    int rc = write_fully(this->socket(), msg, strlen(msg));

    // write any result data
    if (rc == 0) {
      write_fully(this->socket(), (char*) st->base(), st->size());
      ::shutdown(this->socket(), 2);
    }

    // close socket and we're done
    RESTARTABLE(::close(this->socket()), rc);

    // were we externally suspended while we were waiting?
    thread->check_and_wait_while_suspended();
  }
  delete this;
}


// AttachListener functions

AttachOperation* AttachListener::dequeue() {
  JavaThread* thread = JavaThread::current();
  ThreadBlockInVM tbivm(thread);

  thread->set_suspend_equivalent();
  // cleared by handle_special_suspend_equivalent_condition() or
  // java_suspend_self() via check_and_wait_while_suspended()

  AttachOperation* op = SolarisAttachListener::dequeue();

  // were we externally suspended while we were waiting?
  thread->check_and_wait_while_suspended();

  return op;
}

int AttachListener::pd_init() {
  JavaThread* thread = JavaThread::current();
  ThreadBlockInVM tbivm(thread);

  thread->set_suspend_equivalent();
  // cleared by handle_special_suspend_equivalent_condition() or
  // java_suspend_self()

  int ret_code = SolarisAttachListener::init();

  // were we externally suspended while we were waiting?
  thread->check_and_wait_while_suspended();

  return ret_code;
}

// Attach Listener is started lazily except in the case when
// +ReduseSignalUsage is used
bool AttachListener::init_at_startup() {
  if (ReduceSignalUsage) {
    return true;
  } else {
    return false;
  }
}

// If the file .attach_pid<pid> exists in the working directory
// or /tmp then this is the trigger to start the attach mechanism
bool AttachListener::is_init_trigger() {
  if (init_at_startup() || is_initialized()) {
    return false;               // initialized at startup or already initialized
  }
  char fn[PATH_MAX+1];
  sprintf(fn, ".attach_pid%d", os::current_process_id());
  int ret;
  struct stat64 st;
  RESTARTABLE(::stat64(fn, &st), ret);
  if (ret == -1) {
    snprintf(fn, sizeof(fn), "%s/.attach_pid%d",
             os::get_temp_directory(), os::current_process_id());
    RESTARTABLE(::stat64(fn, &st), ret);
  }
  if (ret == 0) {
    // simple check to avoid starting the attach mechanism when
    // a bogus user creates the file
    if (st.st_uid == geteuid()) {
      init();
      return true;
    }
  }
  return false;
}

// if VM aborts then detach/cleanup
void AttachListener::abort() {
  listener_cleanup();
}

void AttachListener::pd_data_dump() {
  os::signal_notify(SIGQUIT);
}

static jint enable_dprobes(AttachOperation* op, outputStream* out) {
  const char* probe = op->arg(0);
  if (probe == NULL || probe[0] == '\0') {
    out->print_cr("No probe specified");
    return JNI_ERR;
  } else {
    int probe_typess = atoi(probe);
    if (errno) {
      out->print_cr("invalid probe type");
      return JNI_ERR;
    } else {
      DTrace::enable_dprobes(probe_typess);
      return JNI_OK;
    }
  }
}

// platform specific operations table
static AttachOperationFunctionInfo funcs[] = {
  { "enabledprobes", enable_dprobes },
  { NULL, NULL }
};

AttachOperationFunctionInfo* AttachListener::pd_find_operation(const char* name) {
  int i;
  for (i = 0; funcs[i].name != NULL; i++) {
    if (strcmp(funcs[i].name, name) == 0) {
      return &funcs[i];
    }
  }
  return NULL;
}

// Solaris specific global flag set. Currently, we support only
// changing ExtendedDTraceProbes flag.
jint AttachListener::pd_set_flag(AttachOperation* op, outputStream* out) {
  const char* name = op->arg(0);
  assert(name != NULL, "flag name should not be null");
  bool flag = true;
  const char* arg1;
  if ((arg1 = op->arg(1)) != NULL) {
    flag = (atoi(arg1) != 0);
    if (errno) {
      out->print_cr("flag value has to be an integer");
      return JNI_ERR;
    }
  }

  if (strcmp(name, "ExtendedDTraceProbes") == 0) {
    DTrace::set_extended_dprobes(flag);
    return JNI_OK;
  }

  if (strcmp(name, "DTraceMonitorProbes") == 0) {
    DTrace::set_monitor_dprobes(flag);
    return JNI_OK;
  }

  out->print_cr("flag '%s' cannot be changed", name);
  return JNI_ERR;
}

void AttachListener::pd_detachall() {
  DTrace::detach_all_clients();
}