aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <frankhwei536@gmail.com>2015-12-28 13:55:12 -0500
committerFranklin Wei <frankhwei536@gmail.com>2015-12-28 13:55:12 -0500
commitd4e5d9fb0ecf45889d1b77c5acb9b549e4eea4f4 (patch)
tree314ee8088eebf3e227ecda64ccba49d95c403772
parenta75acdf938c2929d05c7d0d3c053c5cae52f791d (diff)
downloadnetcosm-d4e5d9fb0ecf45889d1b77c5acb9b549e4eea4f4.zip
netcosm-d4e5d9fb0ecf45889d1b77c5acb9b549e4eea4f4.tar.gz
netcosm-d4e5d9fb0ecf45889d1b77c5acb9b549e4eea4f4.tar.bz2
netcosm-d4e5d9fb0ecf45889d1b77c5acb9b549e4eea4f4.tar.xz
optimize
-rw-r--r--Makefile2
-rw-r--r--src/auth.c14
-rw-r--r--src/client.c29
-rw-r--r--src/netcosm.h10
-rw-r--r--src/server.c42
-rw-r--r--src/telnet.c6
-rw-r--r--src/util.c4
7 files changed, 45 insertions, 62 deletions
diff --git a/Makefile b/Makefile
index b2a693a..a523406 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ PLATFORM = unix
NETCOSM_OBJ = src/server.o src/client.o src/auth.o src/telnet.o src/util.o src/room.o worlds/test.o src/hash.o
-CFLAGS = -Og -g -I src/ -I target/$(PLATFORM) -Wall -Wextra -Wshadow -std=gnu99
+CFLAGS = -O3 -g -I src/ -I target/$(PLATFORM) -Wall -Wextra -Wshadow -std=gnu99
LDFLAGS = -lgcrypt
HEADERS = src/netcosm.h src/hash.h src/telnet.h
diff --git a/src/auth.c b/src/auth.c
index 5041ef4..5027e5f 100644
--- a/src/auth.c
+++ b/src/auth.c
@@ -172,7 +172,7 @@ bool auth_user_add(const char *user2, const char *pass2, int level)
char *pass = strdup(pass2);
remove_cruft(pass);
- printf("Add user '%s'\n", user);
+ debugf("Add user '%s'\n", user);
if(!valid_login_name(user))
{
@@ -220,20 +220,20 @@ static bool valid_login_name(const char *name)
void first_run_setup(void)
{
- printf("Welcome to NetCosm!\n");
- printf("Please set up the administrator account now.\n");
+ debugf("Welcome to NetCosm!\n");
+ debugf("Please set up the administrator account now.\n");
char *admin_name;
size_t len = 0;
do {
admin_name = NULL;
- printf("Admin account name: ");
+ debugf("Admin account name: ");
fflush(stdout);
getline(&admin_name, &len, stdin);
remove_cruft(admin_name);
} while(!valid_login_name(admin_name));
- printf("Admin password (_DO_NOT_ USE A VALUABLE PASSWORD): ");
+ debugf("Admin password (_DO_NOT_ USE A VALUABLE PASSWORD): ");
fflush(stdout);
char *admin_pass = NULL;
len = 0;
@@ -325,13 +325,13 @@ struct authinfo_t auth_check(const char *name2, const char *pass2)
free(line);
}
good:
- printf("Successful authentication.\n");
+ debugf("Successful authentication.\n");
fclose(f);
return ret;
bad:
sleep(2);
fclose(f);
- printf("Failed authentication.\n");
+ debugf("Failed authentication.\n");
return ret;
}
diff --git a/src/client.c b/src/client.c
index de39392..03ff259 100644
--- a/src/client.c
+++ b/src/client.c
@@ -115,8 +115,6 @@ tryagain:
if(read(client_fd, buf, BUFSZ - 1) < 0)
error("lost connection");
buf[BUFSZ - 1] = '\0';
-
- printf("Read '%s'\n", buf);
if(buf[0] & 0x80)
{
telnet_handle_command((unsigned char*)buf);
@@ -153,9 +151,7 @@ static void print_all(int fd)
{
unsigned char buf[MSG_MAX + 1];
do {
- sig_printf("Reading...\n");
ssize_t len = read(fd, &buf, MSG_MAX);
- sig_printf("Read %d bytes\n", len);
if(len <= 0)
break;
buf[MSG_MAX] = '\0';
@@ -167,19 +163,16 @@ void sig_rt_0_handler(int s, siginfo_t *info, void *v)
{
(void) s;
(void) v;
- sig_printf("PID %d got SIGRTMIN+1\n", getpid());
/* we only listen to requests from our parent */
if(info->si_pid != getppid())
{
- sig_printf("Unknown PID sent SIGRTMIN+1\n");
+ sig_debugf("Unknown PID sent SIGRTMIN+1\n");
return;
}
unsigned char cmd;
read(from_parent, &cmd, 1);
- sig_printf("Got command from parent.\n");
- unsigned char buf[MSG_MAX + 1];
switch(cmd)
{
case REQ_BCASTMSG:
@@ -203,19 +196,13 @@ void sig_rt_0_handler(int s, siginfo_t *info, void *v)
out("Cannot go that way.\n");
}
case REQ_NOP:
- sig_printf("NOP from parent.\n");
break;
default:
- sig_printf("WARNING: client process received unknown code %d\n", cmd);
- ssize_t len = read(from_parent, buf, MSG_MAX);
- if(len > 0)
- sig_printf("DATA %d ___'%s'___", len, buf);
- sleep(1);
- kill(getppid(), SIGSEGV);
+ sig_debugf("WARNING: client process received unknown code %d\n", cmd);
break;
}
- sig_printf("Client finishes handling request.\n");
+ sig_debugf("Client finishes handling request.\n");
request_complete = 1;
@@ -226,9 +213,7 @@ void sig_rt_0_handler(int s, siginfo_t *info, void *v)
static void client_change_state(int state)
{
- printf("Client requesting state transition\n");
send_master(REQ_CHANGESTATE, &state, sizeof(state));
- printf("State transition completed.\n");
}
static void client_change_user(const char *user)
@@ -306,8 +291,8 @@ void client_main(int fd, struct sockaddr_in *addr, int total, int to, int from)
telnet_init();
char *ip = inet_ntoa(addr->sin_addr);
- printf("New client %s\n", ip);
- printf("Total clients: %d\n", total);
+ debugf("New client %s\n", ip);
+ debugf("Total clients: %d\n", total);
auth:
@@ -367,7 +352,7 @@ auth:
client_change_state(STATE_LOGGEDIN);
/* authenticated */
- printf("Authenticated as %s\n", current_user);
+ debugf("Authenticated as %s\n", current_user);
client_change_user(current_user);
current_room = 0;
client_change_room(current_room);
@@ -498,7 +483,7 @@ auth:
sizeof(pidbuf) - sizeof(pid_t),
"You were kicked.\n");
send_master(REQ_KICK, pidbuf, len);
- printf("Success.\n");
+ debugf("Success.\n");
}
else
out("Usage: CLIENT KICK <PID>\n");
diff --git a/src/netcosm.h b/src/netcosm.h
index 5c308b5..31edb50 100644
--- a/src/netcosm.h
+++ b/src/netcosm.h
@@ -98,6 +98,14 @@
#define MAX(a,b) ((a>b)?(a):(b))
#define MIN(a,b) ((a<b)?(a):(b))
+#ifndef NDEBUG
+#define debugf(fmt,...) debugf_real(fmt, ##__VA_ARGS__)
+#define sig_debugf debugf
+#else
+#define debugf(fmt,...)
+#define sig_debugf debugf
+#endif
+
typedef int room_id;
struct authinfo_t {
@@ -205,5 +213,5 @@ void world_free(void);
/* utility functions */
void __attribute__((noreturn,format(printf,1,2))) error(const char *fmt, ...);
-void sig_printf(const char *fmt, ...);
+void debugf_real(const char *fmt, ...);
void remove_cruft(char*);
diff --git a/src/server.c b/src/server.c
index 3badaec..8ea4516 100644
--- a/src/server.c
+++ b/src/server.c
@@ -57,7 +57,7 @@ static void sigchld_handler(int s, siginfo_t *info, void *vp)
pid_t pid;
while((pid = waitpid(-1, NULL, WNOHANG)) > 0)
{
- sig_printf("Client disconnect.\n");
+ sig_debugf("Client disconnect.\n");
struct child_data *child = hash_lookup(child_map, &pid);
FD_CLR(child->readpipe[0], &active_fds);
@@ -83,7 +83,7 @@ static void *request_map = NULL;
static void serv_cleanup(void)
{
- sig_printf("Shutdown server.\n");
+ sig_debugf("Shutdown server.\n");
if(shutdown(server_socket, SHUT_RDWR) > 0)
error("shutdown");
close(server_socket);
@@ -161,13 +161,13 @@ static void req_change_state(unsigned char *data, size_t datalen,
if(datalen == sizeof(sender->state))
{
sender->state = *((int*)data);
- printf("State changed to %d\n", sender->state);
+ debugf("State changed to %d\n", sender->state);
}
else
{
- printf("State data is of the wrong size\n");
+ debugf("State data is of the wrong size\n");
for(size_t i = 0; i < datalen; ++i)
- printf("%02x\n", data[i]);
+ debugf("%02x\n", data[i]);
}
}
@@ -253,7 +253,7 @@ static void req_move_room(unsigned char *data, size_t datalen, struct child_data
room_user_del(sender->room, sender);
/* TODO: checking */
- sig_printf("Moving in direction %d\n", dir);
+ sig_debugf("Moving in direction %d\n", dir);
room_id new = current->adjacent[dir];
int status;
if(new != ROOM_NONE)
@@ -334,12 +334,10 @@ static bool handle_child_req(int in_fd)
if(read(in_fd, &sender_pid, sizeof(sender_pid)) != sizeof(sender_pid))
{
- sig_printf("Couldn't get sender PID\n");
+ sig_debugf("Couldn't get sender PID\n");
goto fail;
}
- sig_printf("PID %d sends a client request\n", sender_pid);
-
size_t msglen;
const struct child_request *req = NULL;
size_t datalen;
@@ -350,24 +348,24 @@ static bool handle_child_req(int in_fd)
if(!sender)
{
- sig_printf("WARNING: got data from unknown PID, ignoring.\n");
+ sig_debugf("WARNING: got data from unknown PID, ignoring.\n");
goto fail;
}
if(read(in_fd, &msglen, sizeof(msglen)) != sizeof(msglen))
{
- sig_printf("Couldn't read message length, dropping.\n");
+ sig_debugf("Couldn't read message length, dropping.\n");
goto fail;
}
if(msglen < 1)
{
- sig_printf("message too short to be valid, ignoring.\n");
+ sig_debugf("message too short to be valid, ignoring.\n");
goto fail;
}
else if(msglen > MSG_MAX)
{
- sig_printf("message too long, ignoring.\n");
+ sig_debugf("message too long, ignoring.\n");
goto fail;
}
@@ -378,7 +376,7 @@ static bool handle_child_req(int in_fd)
ssize_t ret = read(sender->readpipe[0], msgptr, msglen - have);
if(ret < 0)
{
- sig_printf("unexpected EOF\n");
+ sig_debugf("unexpected EOF\n");
goto fail;
}
msgptr += ret;
@@ -405,7 +403,7 @@ static bool handle_child_req(int in_fd)
if(!req)
{
- sig_printf("Unknown request.\n");
+ sig_debugf("Unknown request.\n");
goto fail;
}
@@ -434,7 +432,6 @@ static bool handle_child_req(int in_fd)
ptr = NULL;
if(!child)
break;
- sig_printf("Iterating over PID %d\n", *key);
if(child->pid == sender->pid)
continue;
@@ -458,22 +455,17 @@ finish:
if(sender)
sigqueue(sender->pid, SIGRTMIN, junk);
else
- sig_printf("Unknown PID send request.\n");
-
- sig_printf("Waiting for %d acks\n", num_acks_wanted);
+ sig_debugf("Unknown PID sent request.\n");
while(num_acks_recvd < MIN(num_clients,num_acks_wanted))
{
sigsuspend(&old);
- sig_printf("Got %d total acks\n", num_acks_recvd);
}
inc_acks = 0;
sigprocmask(SIG_SETMASK, &old, NULL);
- sig_printf("finished handling client request\n");
-
return true;
fail:
empty_pipe(in_fd);
@@ -484,11 +476,9 @@ static void master_ack_handler(int s, siginfo_t *info, void *v)
{
(void) s;
(void) v;
- sig_printf("Parent gets ACK\n");
if(inc_acks && hash_lookup(child_map, &info->si_pid))
{
++num_acks_recvd;
- sig_printf("%d acks now\n", num_acks_recvd);
}
}
@@ -618,7 +608,7 @@ int main(int argc, char *argv[])
hash_setfreedata_cb(child_map, free_child_data);
hash_setfreekey_cb(child_map, free);
- printf("Listening on port %d\n", port);
+ debugf("Listening on port %d\n", port);
FD_ZERO(&active_fds);
FD_SET(server_socket, &active_fds);
@@ -679,7 +669,7 @@ int main(int argc, char *argv[])
hash_free(child_map);
child_map = NULL;
- printf("Child with PID %d spawned\n", getpid());
+ debugf("Child with PID %d spawned\n", getpid());
server_socket = new_sock;
diff --git a/src/telnet.c b/src/telnet.c
index f984528..bd800ff 100644
--- a/src/telnet.c
+++ b/src/telnet.c
@@ -50,7 +50,7 @@ void telnet_handle_command(const unsigned char *buf)
{
if(c == commands[i].val)
{
- printf("%s ", commands[i].name);
+ debugf("%s ", commands[i].name);
cmd = true;
goto found;
}
@@ -62,14 +62,14 @@ void telnet_handle_command(const unsigned char *buf)
default:
break;
}
- printf("???: %d ", c);
+ debugf("???: %d ", c);
found:
++buf;
}
if(cmd)
- printf("\n");
+ debugf("\n");
}
void telnet_echo_off(void)
diff --git a/src/util.c b/src/util.c
index 8da1181..25063f6 100644
--- a/src/util.c
+++ b/src/util.c
@@ -25,10 +25,10 @@ void remove_cruft(char *str)
}
/**
- * WARNING: not totally signal-safe
+ * WARNING: not signal-safe
* TODO: rewrite to avoid calling *printf()
*/
-void sig_printf(const char *fmt, ...)
+void debugf_real(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);