Permalink
Browse files

detachuj sie, opcja do wylaczenia tego, pozniej.

  • Loading branch information...
darkjames
darkjames committed Oct 22, 2008
1 parent 26ca475 commit 471f7a6fed27ae46a0edd07d54e05fef3e9943ed
Showing with 50 additions and 6 deletions.
  1. +13 −6 protocol.c
  2. +37 −0 usg.c
@@ -437,25 +437,27 @@ static int gg_login80_handler(client_t *c, void *data, uint32_t len) {
}

static int gg_notify_handler(client_t *c, void *data, uint32_t len) {
struct gg_notify *n = data;
int i;

if (c->state != STATE_LOGIN_OK) {
printf("gg_notify_handler() c->state = %d\n", c->state);
return -3;
}

printf("received notify list from %d\n", c->uin);

for (i = 0; i < len / sizeof(*n); i++) {
while (len >= sizeof(struct gg_notify)) {
struct gg_notify *n = data;
friend_t *f;

f = xmalloc(sizeof(friend_t));
f->uin = n->uin;
f->flags = n->dunno1;

f->uin = n[i].uin;
f->flags = n[i].dunno1;
list_add(&c->friends, f);

len -= sizeof(struct gg_notify);
data += sizeof(struct gg_notify);
}

return 0;
}

@@ -542,6 +544,11 @@ static int gg_new_status_handler(client_t *c, void *data, uint32_t len) {
struct gg_new_status *s = (struct gg_new_status *) data;
int status;

if (c->state != STATE_LOGIN_OK) {
printf("gg_new_status_handler() c->state = %d\n", c->state);
return -3;
}

status = s->status;

/* XXX, sprawdzac protokol? */
37 usg.c
@@ -20,9 +20,12 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/poll.h>
#include <arpa/inet.h>
#include <errno.h>
#include <time.h>
#include <string.h>
@@ -149,6 +152,11 @@ static int handle_connection(client_t *c)
return 0;
}

if (sin_len == sizeof(struct sockaddr_in) && ((struct sockaddr_in *) &sin)->sin_family == AF_INET)
printf("accepted new connection from: %s:%d (%d)\n", inet_ntoa(((struct sockaddr_in *) &sin)->sin_addr), ((struct sockaddr_in *) &sin)->sin_port, fd);
else
printf("accepted new connection (%d)\n", fd);

if (ioctl(fd, FIONBIO, &nb) == -1) {
perror("ioctl");
close(fd);
@@ -271,6 +279,8 @@ int main(int argc, char **argv)
int sock, opt = 1;
client_t *cl;

int detach = 1;

srand(time(NULL));

if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
@@ -297,6 +307,33 @@ int main(int argc, char **argv)
return 1;
}

if (detach) {
int pid;

if ((pid = fork()) < 0)
perror("fork");

if (pid > 0) {
/* parent */
exit(0);
}
if (pid == 0) {
int fd_null, fd_log;
setsid();

fd_null = open("/dev/null", O_RDWR);
if ((fd_log = open("usg.log", O_CREAT | O_WRONLY | O_APPEND, 0600)) == -1)
fd_log = fd_null;

dup2(fd_null, 0);
dup2(fd_log, 1);
dup2(fd_log, 2);

close(fd_null);
close(fd_log);
}
}

cl = malloc(sizeof(client_t));
memset(cl, 0, sizeof(client_t));

0 comments on commit 471f7a6

Please sign in to comment.