1 | |
---|
2 | #ifdef HAVE_CONFIG_H |
---|
3 | # include <config.h> |
---|
4 | #endif |
---|
5 | |
---|
6 | #include <stdio.h> |
---|
7 | |
---|
8 | #include <sys/socket.h> |
---|
9 | #include <sys/un.h> |
---|
10 | #include <unistd.h> |
---|
11 | #include <string.h> |
---|
12 | #include <assert.h> |
---|
13 | #include <stdarg.h> |
---|
14 | #include <errno.h> |
---|
15 | #include <time.h> |
---|
16 | |
---|
17 | #define CHAR_BUF 256 |
---|
18 | |
---|
19 | void log_msg(const char* address, const char *suffix, const char *fmt, ...) |
---|
20 | { |
---|
21 | FILE * fd; |
---|
22 | char path[CHAR_BUF] = ""; |
---|
23 | char text[CHAR_BUF] = ""; |
---|
24 | int n; |
---|
25 | |
---|
26 | if( snprintf(path,CHAR_BUF, "%s.%s", address, suffix) < 28 ) /* /tmp/drmaa_socket_XXXXXXX.err */ |
---|
27 | { |
---|
28 | fprintf(stderr,"Can't create err file address"); /* nobody will see it... */ |
---|
29 | assert(0); |
---|
30 | } |
---|
31 | if((fd = fopen(path,"a")) != NULL) |
---|
32 | { |
---|
33 | va_list args; |
---|
34 | va_start (args,fmt); |
---|
35 | n = vsnprintf(text,CHAR_BUF, fmt, args); |
---|
36 | va_end( args ); |
---|
37 | if( 0 <= n && (size_t)n < CHAR_BUF ) |
---|
38 | { |
---|
39 | fprintf(fd,"%d - %s", time(NULL), text); |
---|
40 | fclose(fd); |
---|
41 | } |
---|
42 | else |
---|
43 | { |
---|
44 | fprintf(stderr,"Can't parse error text"); /* nobody will see it... */ |
---|
45 | assert(0); |
---|
46 | } |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | int main(int argc, char **argv) |
---|
51 | { |
---|
52 | /*LL_DRMAA_BIN_DIR is defined in config.h that was generated during ./configure process */ |
---|
53 | if(argc == 5) |
---|
54 | { |
---|
55 | struct sockaddr_un address; |
---|
56 | size_t address_length; |
---|
57 | int socket_fd = -1; |
---|
58 | int nbytes; |
---|
59 | char buffer[CHAR_BUF]; |
---|
60 | |
---|
61 | socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); |
---|
62 | if(socket_fd < 0) |
---|
63 | { |
---|
64 | log_msg(argv[2], "err", "socket() failed\n"); |
---|
65 | return 1; |
---|
66 | } |
---|
67 | |
---|
68 | address.sun_family = AF_UNIX; |
---|
69 | address_length = sizeof(address.sun_family) + sprintf(address.sun_path, argv[2]) + 1; |
---|
70 | |
---|
71 | if(connect(socket_fd, (struct sockaddr *) &address, address_length) != 0) |
---|
72 | { |
---|
73 | log_msg(argv[2], "err", "connect() failed. Address: %s (%d)\n", address.sun_path, errno); |
---|
74 | return 1; |
---|
75 | } |
---|
76 | |
---|
77 | nbytes = snprintf(buffer,CHAR_BUF, "%s %s %s\n", argv[3], argv[1], argv[4]); /* JOB_xxxxxx job_id status*/ |
---|
78 | #ifdef DEBUGGING |
---|
79 | log_msg(argv[2], "debug", "Sending message: %s", buffer); |
---|
80 | #endif |
---|
81 | |
---|
82 | if(write(socket_fd, buffer, nbytes) <= 0) |
---|
83 | { |
---|
84 | log_msg(argv[2], "err", "Can't write data to socket: %s",buffer); |
---|
85 | } |
---|
86 | |
---|
87 | close(socket_fd); |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | log_msg(argv[2],"err", "Not enough arguments\n"); |
---|
92 | return 1; |
---|
93 | } |
---|
94 | return 0; |
---|
95 | } |
---|