fix bug in socket.c

This commit is contained in:
eddyem 2017-04-17 17:02:13 +03:00
parent 91f3b192cd
commit b24eaf360b
2 changed files with 20 additions and 14 deletions

14
main.c
View File

@ -55,7 +55,17 @@ int main(int argc, char **argv){
imstorage *img = NULL; imstorage *img = NULL;
imsubframe *F = NULL; imsubframe *F = NULL;
// daemonize @ start
#if defined DAEMON || defined CLIENT
#ifndef EBUG
if(!G->once){
green("Daemonize\n");
if(daemon(1, 0)){
ERR("daemon()");
}
}
#endif
#endif
#ifndef CLIENT #ifndef CLIENT
if(G->splist){ if(G->splist){
list_speeds(); list_speeds();
@ -88,7 +98,7 @@ int main(int argc, char **argv){
img->timestamp = G->timestamp; img->timestamp = G->timestamp;
#endif #endif
#ifndef DAEMON #ifndef DAEMON
img->imname = G->outpfname; img->imname = strdup(G->outpfname);
img->exposetime = time(NULL); img->exposetime = time(NULL);
if(!chk_storeimg(img, G->imstoretype, G->imformat)) return 1; if(!chk_storeimg(img, G->imstoretype, G->imformat)) return 1;
#endif #endif

View File

@ -224,7 +224,7 @@ void *handle_socket(void *asock){
int sock = *((int*)asock); int sock = *((int*)asock);
int webquery = 0; // whether query is web or regular int webquery = 0; // whether query is web or regular
char buff[BUFLEN]; char buff[BUFLEN];
ssize_t readed; ssize_t _read;
while(1){ while(1){
if(!waittoread(sock)){ // no data incoming if(!waittoread(sock)){ // no data incoming
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
@ -241,14 +241,14 @@ void *handle_socket(void *asock){
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
continue; continue;
} }
if(!(readed = read(sock, buff, BUFLEN))) continue; _read = read(sock, buff, BUFLEN);
DBG("Got %zd bytes", readed); if(_read < 0){ // error or disconnect
if(readed < 0){ // error or disconnect DBG("Nothing to read from fd %d (ret: %zd)", sock, _read);
DBG("Nothing to read from fd %d (ret: %zd)", sock, readed);
break; break;
} }
DBG("Got %zd bytes", _read);
// add trailing zero to be on the safe side // add trailing zero to be on the safe side
buff[readed] = 0; buff[_read] = 0;
// now we should check what do user want // now we should check what do user want
char *got, *found = buff; char *got, *found = buff;
if((got = stringscan(buff, "GET")) || (got = stringscan(buff, "POST"))){ // web query if((got = stringscan(buff, "GET")) || (got = stringscan(buff, "POST"))){ // web query
@ -334,8 +334,8 @@ static void daemon_(imstorage *img, int sock){
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
if(copyima(img)){ if(copyima(img)){
++imctr; ++imctr;
if(img->imtype != IMTYPE_DARK) if(img->imtype != IMTYPE_DARK)
save_histo(NULL, img); // calculate next optimal exposition save_histo(NULL, img); // calculate next optimal exposition
} }
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
} }
@ -426,10 +426,6 @@ void daemonize(imstorage *img, char *hostname, char *port){
FNAME(); FNAME();
#ifndef EBUG #ifndef EBUG
if(!img->once){ if(!img->once){
green("Daemonize\n");
if(daemon(1, 0)){
ERR("daemon()");
}
while(1){ // guard for dead processes while(1){ // guard for dead processes
pid_t childpid = fork(); pid_t childpid = fork();
if(childpid){ if(childpid){