diff --git a/Makefile b/Makefile index 2fb4527..e74be84 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,17 @@ .PHONY: clean install -CS_PATH=/usr +CS_PATH ?= /usr MY_CFLAGS+=-I$(CS_PATH)/include/ClearSilver $(CFLAGS) MY_CFLAGS+=$(shell Wand-config --cflags --cppflags) MY_LDFLAGS+=$(shell Wand-config --ldflags --libs) $(LDFLAGS) MY_CFLAGS+=$(shell pkg-config --cflags json) MY_LDFLAGS+=$(shell pkg-config --libs json) +MY_LDFLAGS+=-ljpeg OBJS=obj/main.o obj/util.o obj/galleries.o obj/gallery.o obj/varray.o \ obj/photo.o obj/hdf-json.o obj/jpeg.o \ - $(CS_PATH)/lib/libneo_cgi.a $(CS_PATH)/lib/libneo_cs.a \ - $(CS_PATH)/lib/libneo_utl.a + $(CS_PATH)/lib64/libneo_cgi.a $(CS_PATH)/lib64/libneo_cs.a \ + $(CS_PATH)/lib64/libneo_utl.a VERSIONSTAMP=$(shell if [ -d .git ]; then git describe; else cat version; fi) COMPILESTAMP=$(shell date "+%Y-%m-%d %H:%M:%S %z" ) @@ -23,7 +24,7 @@ obj: obj/%.o: src/%.c include/%.h obj $(CC) -DCOMPILESTAMP='"$(COMPILESTAMP)"' \ -DVERSIONSTAMP='"$(VERSIONSTAMP)"' \ - $(MY_CFLAGS) -I include -o $@ -c $< + $(MY_CFLAGS) -I include $(CPPFLAGS) -o $@ -c $< clean: rm -fr obj gallery.cgi diff --git a/include/util.h b/include/util.h index f42e192..2918534 100644 --- a/include/util.h +++ b/include/util.h @@ -179,4 +179,14 @@ void sort_str_array( char** array, size_t length, int case_insensitive ); void sort_array( void** array, size_t length, int (*compare)( const void*, const void* ) ); +/*! + * Pause if the server load is too high. + * This causes the CGI script to sleep for a second if the load average is + * above MAX_CPU_LOAD. + */ +void wait_cpu_load(); +#ifndef MAX_CPU_LOAD +#define MAX_CPU_LOAD (2.0) +#endif + #endif diff --git a/src/photo.c b/src/photo.c index 796b101..f933617 100644 --- a/src/photo.c +++ b/src/photo.c @@ -463,21 +463,25 @@ char* resize_photo( struct photo_meta* dest, } } +#ifndef NO_SEMAPHORE /* * Check for semaphore... if it exists, wait for it to disappear. * For this, we use the open() syscall with O_CREAT|O_EXCL, which * will fail if the file already exists. */ - char* sem_file = construct_path( "s/s/s", get_cgidir(), - CACHEDIR, "semaphore" ); + char* sem_file = construct_path( "s/ss", get_cgidir(), + cache_file, ".sem" ); int fd = open( sem_file, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR ); while( fd == -1 ) { sleep(1); fd = open( sem_file, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR ); + fprintf( stderr, "resize_photo: waiting for semaphore %s (fd: %d)\n", + sem_file, fd ); dprintf( "resize_photo: waiting for semaphore %s (fd: %d)\n", sem_file, fd ); } close(fd); +#endif MagickWand* wand = NewMagickWand(); dprintf( "resize_photo: wand created at %p\n", wand); @@ -486,11 +490,12 @@ char* resize_photo( struct photo_meta* dest, char* description=MagickGetException(wand,&severity); dprintf("%s %s %ld %s\n",GetMagickModule(),description); gcfree( cache_file ); - + +#ifndef NO_SEMAPHORE /* Get rid of the semaphore */ unlink( sem_file ); gcfree( sem_file ); - +#endif return NULL; } @@ -521,9 +526,11 @@ char* resize_photo( struct photo_meta* dest, char* description=MagickGetException(wand,&severity); dprintf("%s %s %ld %s\n",GetMagickModule(),description); +#ifndef NO_SEMAPHORE /* Get rid of the semaphore */ unlink( sem_file ); gcfree( sem_file ); +#endif gcfree( cache_file ); cache_file = NULL; @@ -531,9 +538,11 @@ char* resize_photo( struct photo_meta* dest, DestroyMagickWand( wand ); +#ifndef NO_SEMAPHORE /* Get rid of the semaphore */ unlink( sem_file ); gcfree( sem_file ); +#endif gcfree( cache_path ); dprintf( "resize_photo: resized photo at %s (%p)\n", cache_file, cache_file ); diff --git a/src/util.c b/src/util.c index 364fab2..81d0a41 100644 --- a/src/util.c +++ b/src/util.c @@ -6,6 +6,8 @@ #include #include +#include + /* Memory management -- helpers */ #ifdef MEMALLOC_TRACK struct mem_chunk* gc_find_chunk( void* ptr ) { @@ -678,3 +680,19 @@ void sort_str_array( char** array, size_t length, int case_insensitive ) { else quicksort_main( (void**)array, 0, length-1, &void_strcmp ); } + + +void wait_load() { +#ifndef NO_MAX_LOAD + double avg; + if (getloadavg(&avg,1) < 0) + return; + while(avg >= MAX_CPU_LOAD) { + dprintf("Waiting for CPU load %f to drop below %f\n", + avg, MAX_CPU_LOAD); + sleep(1); + if (getloadavg(&avg,1) < 0) + return; + } +#endif +}