jdk/src/jdk.hprof.agent/windows/native/libhprof/hprof_md.c
author sla
Tue, 26 Aug 2014 07:55:08 +0200
changeset 26201 40a873d21081
parent 25859 jdk/src/demo/windows/jvmti/hprof/hprof_md.c@3317bb8137f4
permissions -rw-r--r--
8043936: Drop HPROF as demo, keep as HPROF agent shipped with JDK Reviewed-by: erikj, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20484
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9035
diff changeset
    40
7999
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
    41
// To ensure winsock2.h is used, it has to be included ahead of
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
    42
// windows.h, which includes winsock.h by default.
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
    43
#include <winsock2.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include <mmsystem.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include <process.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
md_getpid(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static int pid = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    if ( pid >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    pid = getpid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    return pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
md_sleep(unsigned seconds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    Sleep((DWORD)seconds*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
md_init(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
md_connect(char *hostname, unsigned short port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    struct hostent *hentry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    struct sockaddr_in s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /* find remote host's addr from name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    if ((hentry = gethostbyname(hostname)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    (void)memset((char *)&s, 0, sizeof(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* set remote host's addr; its already in network byte order */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    (void)memcpy(&s.sin_addr.s_addr, *(hentry->h_addr_list),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
           (int)sizeof(s.sin_addr.s_addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /* set remote host's port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    s.sin_port = htons(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    s.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
20484
5ded5b92c173 8012923: [parfait] File Descriptor Leak in jdk/src/windows/demo/jvmti/hprof/hprof_md.c
allwin
parents: 16726
diff changeset
    97
    /* create a socket */
5ded5b92c173 8012923: [parfait] File Descriptor Leak in jdk/src/windows/demo/jvmti/hprof/hprof_md.c
allwin
parents: 16726
diff changeset
    98
    fd = (int)socket(AF_INET, SOCK_STREAM, 0);
5ded5b92c173 8012923: [parfait] File Descriptor Leak in jdk/src/windows/demo/jvmti/hprof/hprof_md.c
allwin
parents: 16726
diff changeset
    99
    if (INVALID_SOCKET == fd) {
5ded5b92c173 8012923: [parfait] File Descriptor Leak in jdk/src/windows/demo/jvmti/hprof/hprof_md.c
allwin
parents: 16726
diff changeset
   100
        return 0;
5ded5b92c173 8012923: [parfait] File Descriptor Leak in jdk/src/windows/demo/jvmti/hprof/hprof_md.c
allwin
parents: 16726
diff changeset
   101
    }
5ded5b92c173 8012923: [parfait] File Descriptor Leak in jdk/src/windows/demo/jvmti/hprof/hprof_md.c
allwin
parents: 16726
diff changeset
   102
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /* now try connecting */
20484
5ded5b92c173 8012923: [parfait] File Descriptor Leak in jdk/src/windows/demo/jvmti/hprof/hprof_md.c
allwin
parents: 16726
diff changeset
   104
    if (SOCKET_ERROR == connect(fd, (struct sockaddr*)&s, sizeof(s))) {
5ded5b92c173 8012923: [parfait] File Descriptor Leak in jdk/src/windows/demo/jvmti/hprof/hprof_md.c
allwin
parents: 16726
diff changeset
   105
        closesocket(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
md_recv(int f, char *buf, int len, int option)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    return recv(f, buf, len, option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
md_shutdown(int filedes, int option)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    return shutdown(filedes, option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
md_open(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    return open(filename, O_RDONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
md_open_binary(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    return open(filename, O_RDONLY|O_BINARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
md_creat(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    return open(filename, O_CREAT | O_WRONLY | O_TRUNC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                             _S_IREAD | _S_IWRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
md_creat_binary(const char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    return open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                            _S_IREAD | _S_IWRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
md_seek(int filedes, jlong pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    jlong new_pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    if ( pos == (jlong)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        new_pos = _lseeki64(filedes, 0L, SEEK_END);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        new_pos = _lseeki64(filedes, pos, SEEK_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    return new_pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
md_close(int filedes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
{
7999
059b8737ff55 6983248: net/net001 and net/net003 fail on WinXP with JDK7-B108
zgu
parents: 5506
diff changeset
   165
    (void)closesocket(filedes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
md_send(int s, const char *msg, int len, int flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    return send(s, msg, len, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
md_read(int filedes, void *buf, int nbyte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    return read(filedes, buf, nbyte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
md_write(int filedes, const void *buf, int nbyte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    return write(filedes, buf, nbyte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
md_get_microsecs(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    return (jlong)(timeGetTime())*(jlong)1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
#define FT2JLONG(ft) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        ((jlong)(ft).dwHighDateTime << 32 | (jlong)(ft).dwLowDateTime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
md_get_timemillis(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    static jlong fileTime_1_1_70 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    SYSTEMTIME st0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    FILETIME   ft0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    if (fileTime_1_1_70 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        /* Initialize fileTime_1_1_70 -- the Win32 file time of midnight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         * 1/1/70.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        memset(&st0, 0, sizeof(st0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        st0.wYear  = 1970;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        st0.wMonth = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        st0.wDay   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        SystemTimeToFileTime(&st0, &ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        fileTime_1_1_70 = FT2JLONG(ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    GetSystemTime(&st0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    SystemTimeToFileTime(&st0, &ft0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    return (FT2JLONG(ft0) - fileTime_1_1_70) / 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
md_get_thread_cpu_timemillis(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    return md_get_timemillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
HINSTANCE hJavaInst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
static int nError = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
BOOL WINAPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    WSADATA wsaData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    switch (reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        case DLL_PROCESS_ATTACH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            hJavaInst = hinst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            nError = WSAStartup(MAKEWORD(2,0), &wsaData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        case DLL_PROCESS_DETACH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            WSACleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            hJavaInst = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
md_get_prelude_path(char *path, int path_len, char *filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    char libdir[FILENAME_MAX+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    char *lastSlash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    GetModuleFileName(hJavaInst, libdir, FILENAME_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /* This is actually in the bin directory, so move above bin for lib */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    lastSlash = strrchr(libdir, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    if ( lastSlash != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        *lastSlash = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    lastSlash = strrchr(libdir, '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    if ( lastSlash != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        *lastSlash = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    (void)md_snprintf(path, path_len, "%s\\lib\\%s", libdir, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
md_vsnprintf(char *s, int n, const char *format, va_list ap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    return _vsnprintf(s, n, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
md_snprintf(char *s, int n, const char *format, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    va_list ap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    va_start(ap, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    ret = md_vsnprintf(s, n, format, ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    va_end(ap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
md_system_error(char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    long errval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    errval = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    buf[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    if (errval != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                              NULL, errval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                              0, buf, len, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (n > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            /* Drop final '.', CR, LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (buf[n - 1] == '\n') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (buf[n - 1] == '\r') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (buf[n - 1] == '.') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
md_htons(unsigned short s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    return htons(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
md_htonl(unsigned l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    return htonl(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
md_ntohs(unsigned short s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    return ntohs(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
md_ntohl(unsigned l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    return ntohl(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
get_last_error_string(char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    long errval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    errval = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    if (errval != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        /* DOS error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                              NULL, errval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                              0, buf, len, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (n > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            /* Drop final '.', CR, LF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (buf[n - 1] == '\n') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (buf[n - 1] == '\r') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (buf[n - 1] == '.') n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    if (errno != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        /* C runtime error that has no corresponding DOS error code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        const char *s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        int         n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        s = strerror(errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        n = (int)strlen(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (n >= len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            n = len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        (void)strncpy(buf, s, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        buf[n] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   374
static void dll_build_name(char* buffer, size_t buflen,
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   375
                           const char* paths, const char* fname) {
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   376
    char *path, *paths_copy, *next_token;
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   377
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   378
    paths_copy = strdup(paths);
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   379
    if (paths_copy == NULL) {
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   380
        return;
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   381
    }
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   382
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   383
    next_token = NULL;
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   384
    path = strtok_s(paths_copy, ";", &next_token);
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   385
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   386
    while (path != NULL) {
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   387
        _snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   388
        if (_access(buffer, 0) == 0) {
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   389
            break;
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   390
        }
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   391
        *buffer = '\0';
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   392
        path = strtok_s(NULL, ";", &next_token);
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   393
    }
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   394
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   395
    free(paths_copy);
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   396
}
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   397
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
/* Build a machine dependent library name out of a path and file name.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
void
16726
f76b2e6bd199 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
sla
parents: 16057
diff changeset
   400
md_build_library_name(char *holder, int holderlen, const char *pname, const char *fname)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    int   pnamelen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    pnamelen = pname ? (int)strlen(pname) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   406
    *holder = '\0';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /* Quietly truncates on buffer overflow. Should be an error. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    if (pnamelen + strlen(fname) + 10 > (unsigned int)holderlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    if (pnamelen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        sprintf(holder, "%s.dll", fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    } else {
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 10292
diff changeset
   415
      dll_build_name(holder, holderlen, pname, fname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
md_load_library(const char * name, char *err_buf, int err_buflen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    void *result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    result = LoadLibrary(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    if (result == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        /* Error message is pretty lame, try to make a better guess. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        long errcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        errcode = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (errcode == ERROR_MOD_NOT_FOUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            strncpy(err_buf, "Can't find dependent libraries", err_buflen-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            err_buf[err_buflen-1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            get_last_error_string(err_buf, err_buflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
md_unload_library(void *handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    FreeLibrary(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
md_find_library_entry(void *handle, const char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    return GetProcAddress(handle, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
}