Skip to content

Commit

Permalink
V35.HZ11 final
Browse files Browse the repository at this point in the history
- error out if -o or -w are not followed by a positive value
- modify get_version() to take argc & argv and report command-line
- modify socket_handler() to take argc & argv for use with get_version(
  • Loading branch information
HunterZ committed Nov 25, 2014
1 parent 882e8a5 commit 48ed782
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
18 changes: 13 additions & 5 deletions pixelserv.c
Expand Up @@ -68,7 +68,7 @@ void *get_in_addr(struct sockaddr *sa)
}
#endif

int main (int argc, char *argv[]) // program start
int main (int argc, char* argv[]) // program start
{
int sockfd = 0; // listen on sock_fd
int new_fd = 0; // new connection on new_fd
Expand Down Expand Up @@ -148,7 +148,7 @@ int main (int argc, char *argv[]) // program start
case 'o':
errno = 0;
select_timeout = strtol(argv[i], NULL, 10);
if (errno) {
if (errno || select_timeout <= 0) {
error = 1;
}
continue;
Expand All @@ -165,7 +165,13 @@ int main (int argc, char *argv[]) // program start
case 'u': user = argv[i]; continue;
#endif
#ifdef DEBUG
case 'w': warning_time = strtol(argv[i], NULL, 10); continue;
case 'w':
errno = 0;
warning_time = strtol(argv[i], NULL, 10);
if (errno || warning_time <= 0) {
error = 1;
}
continue;
#endif //DEBUG
default: error = 1; continue;
}
Expand Down Expand Up @@ -219,7 +225,7 @@ int main (int argc, char *argv[]) // program start
SET_LINE_NUMBER(__LINE__);

openlog("pixelserv", LOG_PID | LOG_CONS | LOG_PERROR, LOG_DAEMON);
version_string = get_version(argv[0]);
version_string = get_version(argc, argv);
if (version_string) {
syslog(LOG_INFO, "%s", version_string);
free(version_string);
Expand Down Expand Up @@ -549,7 +555,9 @@ int main (int argc, char *argv[]) // program start
printf("server: got connection from %s\n", ntop_buf);
#endif
// call handler function
socket_handler(new_fd
socket_handler(argc
,argv
,new_fd
,select_timeout
,pipefd[1]
,stats_url
Expand Down
8 changes: 5 additions & 3 deletions socket_handler.c
Expand Up @@ -387,7 +387,9 @@ void child_signal_handler(int sig)
# define TIME_CHECK(x,y...)
#endif //DEBUG

void socket_handler(const int new_fd
void socket_handler(int argc
,char* argv[]
,const int new_fd
,const time_t select_timeout
,const int pipefd
,const char* const stats_url
Expand Down Expand Up @@ -522,7 +524,7 @@ void socket_handler(const int new_fd
syslog(LOG_ERR, "client did not specify URL for GET request");
} else if (!strcmp(path, stats_url)) {
pipedata.status = SEND_STATS;
version_string = get_version(program_name);
version_string = get_version(argc, argv);
stat_string = get_stats(1, 0);
rsize = asprintf(&aspbuf,
"%s%d%s%s%s<br>%s%s",
Expand All @@ -538,7 +540,7 @@ void socket_handler(const int new_fd
response = aspbuf;
} else if (!strcmp(path, stats_text_url)) {
pipedata.status = SEND_STATSTEXT;
version_string = get_version(program_name);
version_string = get_version(argc, argv);
stat_string = get_stats(0, 1);
rsize = asprintf(&aspbuf,
"%s%d%s%s\n%s%s",
Expand Down
4 changes: 3 additions & 1 deletion socket_handler.h
Expand Up @@ -33,7 +33,9 @@ typedef struct {
double run_time;
} response_struct;

void socket_handler(const int new_fd
void socket_handler(int argc
,char* argv[]
,const int new_fd
,const time_t select_timeout
,const int pipefd
,const char* const stats_url
Expand Down
26 changes: 23 additions & 3 deletions util.c
Expand Up @@ -42,8 +42,11 @@ volatile sig_atomic_t hed = 0;
// private data
static struct timespec startup_time = {0, 0};

char* get_version(const char* const program_name) {
char* get_version(int argc, char* argv[]) {
char* retbuf = NULL;
char* optbuf = NULL;
unsigned int optlen = 0, i = 1;
unsigned int arglen[argc];

// capture startup_time if not yet set
if (!startup_time.tv_sec) {
Expand All @@ -53,8 +56,25 @@ char* get_version(const char* const program_name) {
}
}

// asprintf(&retbuf, "%s version: %s compiled: %s from %s", program_name, VERSION, __DATE__ " " __TIME__, __FILE__);
asprintf(&retbuf, "%s version: %s compiled: %s", program_name, VERSION, __DATE__ " " __TIME__);
// determine total size of all arguments
for (i = 1; i < argc; ++i)
{
arglen[i] = strlen(argv[i]);
optlen += arglen[i];
}
// allocate a buffer to hold all arguments
optbuf = malloc((optlen * sizeof(char)) + 1);
if (optbuf) {
for (i = 1, optlen = 0; i < argc; ++i) {
strncpy(optbuf + optlen, argv[i], arglen[i]);
optlen += arglen[i];
}
optbuf[optlen] = '\0';
asprintf(&retbuf, "%s version: %s compiled: %s options: %s", argv[0], VERSION, __DATE__ " " __TIME__, optbuf);
free(optbuf);
} else {
asprintf(&retbuf, "%s version: %s compiled: %s options: <malloc() error>", argv[0], VERSION, __DATE__ " " __TIME__);
}

return retbuf;
}
Expand Down
4 changes: 2 additions & 2 deletions util.h
Expand Up @@ -22,7 +22,7 @@
#include <time.h> // struct timespec, clock_gettime(), difftime()

// preprocessor defines
#define VERSION "V35.HZ11WIP13"
#define VERSION "V35.HZ11"

#define BACKLOG SOMAXCONN // how many pending connections queue will hold
#define CHAR_BUF_SIZE 4095 // surprising how big requests can be with cookies and lengthy yahoo url!
Expand Down Expand Up @@ -98,7 +98,7 @@ extern volatile sig_atomic_t hed;
// generate version string
// note that caller is expected to call free()
// on the return value when done using it
char* get_version(const char* const program_name);
char* get_version(int argc, char* argv[]);

// stats string generator
// NOTES:
Expand Down

0 comments on commit 48ed782

Please sign in to comment.