Skip to content

Commit

Permalink
Attempts to connect only if daemon defined.
Browse files Browse the repository at this point in the history
Some code fixing and additional functionality (--nan-fill-unavailable).
Parsing of user defineable connect timeout. Work on passing connect timeout to rrdc_connect_network.
  • Loading branch information
mariya-rightscale committed Jul 11, 2010
1 parent a9dfe4d commit 5e15724
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 151 deletions.
146 changes: 40 additions & 106 deletions src/rrd_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static int sd = -1;
static FILE *sh = NULL;
static char *sd_path = NULL; /* cache the path for sd */
int fake = 0; /* Flag. If true - generate fake data*/

/* get_path: Return a path name appropriate to be sent to the daemon.
*
Expand Down Expand Up @@ -498,13 +497,6 @@ static int rrdc_connect_unix (const char *path) /* {{{ */
struct sockaddr_un sa;
int status;

/* These are for timeout handling */
int valopt;
long arg;
fd_set myset;
struct timeval tv;
socklen_t lon;

assert (path != NULL);
assert (sd == -1);

Expand All @@ -514,60 +506,18 @@ static int rrdc_connect_unix (const char *path) /* {{{ */
status = errno;
return (status);
}
// Set non-blocking
arg = fcntl(sd, F_GETFL, NULL);
arg |= O_NONBLOCK;
fcntl(sd, F_SETFL, arg);

/* Set timeout with socket options did not work
int sock_val = 1;
setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &sock_val, sizeof(sock_val));
setsockopt(sd, SOL_SOCKET, SO_SNDTIMEO, &sock_val, sizeof(sock_val));
printf("Timeout set to 1");
*/

memset (&sa, 0, sizeof (sa));
sa.sun_family = AF_UNIX;
strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);

status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
// printf("Connected");
/*
if (status != 0)
{
status = errno;
close_connection ();
return (status);
}*/

// Trying to connect with timeout
if (status < 0) {
if (errno == EINPROGRESS) {
tv.tv_sec = 5;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET(sd, &myset);
if (select(sd+1, NULL, &myset, NULL, &tv) > 0) {
lon = sizeof(int);
getsockopt(sd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon);
if (valopt) {
fprintf(stderr, "Error in connection() %d - %s\n", valopt, strerror(valopt));
fake = 1;
}
} else {
fprintf(stderr, "Timeout or error() %d - %s\n", valopt, strerror(valopt));
fake = 1;
}
} else {
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
exit(0);
}
}

// Set to blocking mode again...
arg = fcntl(sd, F_GETFL, NULL);
arg &= (~O_NONBLOCK);
fcntl(sd, F_SETFL, arg);
}

sh = fdopen (sd, "r+");
if (sh == NULL)
Expand All @@ -580,7 +530,6 @@ static int rrdc_connect_unix (const char *path) /* {{{ */
return (0);
} /* }}} int rrdc_connect_unix */


static int rrdc_connect_network (const char *addr_orig) /* {{{ */
{
struct addrinfo ai_hints;
Expand All @@ -589,12 +538,11 @@ static int rrdc_connect_network (const char *addr_orig) /* {{{ */
char addr_copy[NI_MAXHOST];
char *addr;
char *port;
/* These are for timeout handling */
int valopt;
/* Connect with timeout. (Inspired by <http://www.developerweb.net/forum/showthread.php?p=13486>) */
long arg;
fd_set myset;
struct timeval tv;
socklen_t lon;

assert (addr_orig != NULL);
assert (sd == -1);

Expand Down Expand Up @@ -660,66 +608,59 @@ static int rrdc_connect_network (const char *addr_orig) /* {{{ */

for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
{
fake = 0;
// Create sockets
// Create sockets
sd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
printf("Socket descriptor %d\n", sd);
if (sd < 0)
{
status = errno;
sd = -1;
continue;
}
// Set non-blocking
arg = fcntl(sd, F_GETFL, NULL);
arg |= O_NONBLOCK;
// Set non-blocking
arg = fcntl(sd, F_GETFL, NULL);
arg |= O_NONBLOCK;
fcntl(sd, F_SETFL, arg);

/* Set timeout with socket options did not work
int sock_val = 1;
setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &sock_val, sizeof(sock_val) < 0);
setsockopt(sd, SOL_SOCKET, SO_SNDTIMEO, &sock_val, sizeof(sock_val) < 0);
printf("Timeout set to 1 sec\n");
*/

// Trying to connect with timeout
// Trying to connect with timeout
status = connect (sd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
/*
if (status != 0)
{
status = errno;
close_connection();
continue;
}*/
if (status < 0) {
if (errno == EINPROGRESS) {
tv.tv_sec = 3;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET(sd, &myset);
int cd; // connect descriptor
cd = select(sd+1, NULL, &myset, NULL, &tv);
if(cd > 0)
status = 0;
else if(status <= 0)
status = -1;

/*
if (status < 0) {
if (errno == EINPROGRESS) {
tv.tv_sec = 3;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET(sd, &myset);
if (select(sd+1, NULL, &myset, NULL, &tv) > 0) {
lon = sizeof(int);
getsockopt(sd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon);
if (valopt) {
fprintf(stderr, "Error in connection() %d - %s\n", valopt, strerror(valopt));
fake = 1;
lon = sizeof(int);
getsockopt(sd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon);
if (valopt) {
fprintf(stderr, "Error in connection\n");
}
} else {
fprintf(stderr, "Timeout or error() %d - %s\n", valopt, strerror(valopt));
fake = 1;
}
} else {
fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
exit(0);
}
// TODO Will this cause problems??? Does this really ensure that status will become 0 only if the server actually responded?
status = 0;
}else{
fprintf(stderr, "Timeout or error\n");
}
//status = 0;
}else{
fprintf(stderr, "Error connecting\n");
}
// TODO Will this cause problems??? Does this really ensure that status will become 0 only if the server actually responded?
} */

// Set to blocking mode again...
arg = fcntl(sd, F_GETFL, NULL);
arg &= (~O_NONBLOCK);
fcntl(sd, F_SETFL, arg);
// Set to blocking mode again...
arg = fcntl(sd, F_GETFL, NULL);
arg &= (~O_NONBLOCK);
fcntl(sd, F_SETFL, arg);

sh = fdopen (sd, "r+");
if (sh == NULL)
Expand All @@ -728,8 +669,8 @@ static int rrdc_connect_network (const char *addr_orig) /* {{{ */
close_connection ();
continue;
}

assert (status == 0);
// assert (status == 0);
break;
} /* for (ai_ptr) */

Expand Down Expand Up @@ -766,7 +707,6 @@ int rrdc_connect (const char *addr) /* {{{ */
status = rrdc_connect_unix (addr);
else
status = rrdc_connect_network(addr);

if (status == 0 && sd >= 0)
sd_path = strdup(addr);
else
Expand Down Expand Up @@ -948,12 +888,6 @@ int rrdc_fetch (const char *filename, /* {{{ */
size_t current_line;
time_t t;

if(fake)
{
status = 1;
return(status);
}

if ((filename == NULL) || (cf == NULL))
return (-1);

Expand Down
Loading

0 comments on commit 5e15724

Please sign in to comment.