diff options
| author | 2024-01-10 19:37:40 +0100 | |
|---|---|---|
| committer | 2024-01-10 19:37:40 +0100 | |
| commit | fdb32864aa36e3e420083da8bfae45efa9eb743e (patch) | |
| tree | f51baa1a4eb33105d7b3aaa954beb51295b8186c /httpd.c | |
| parent | Updated RELEASE_NOTES. (diff) | |
| download | snac2-fdb32864aa36e3e420083da8bfae45efa9eb743e.tar.gz snac2-fdb32864aa36e3e420083da8bfae45efa9eb743e.tar.xz snac2-fdb32864aa36e3e420083da8bfae45efa9eb743e.zip | |
New command-line option 'state'.
Diffstat (limited to 'httpd.c')
| -rw-r--r-- | httpd.c | 81 |
1 files changed, 75 insertions, 6 deletions
| @@ -22,13 +22,13 @@ | |||
| 22 | 22 | ||
| 23 | #include <sys/resource.h> // for getrlimit() | 23 | #include <sys/resource.h> // for getrlimit() |
| 24 | 24 | ||
| 25 | #include <sys/mman.h> | ||
| 26 | |||
| 25 | #ifdef USE_POLL_FOR_SLEEP | 27 | #ifdef USE_POLL_FOR_SLEEP |
| 26 | #include <poll.h> | 28 | #include <poll.h> |
| 27 | #endif | 29 | #endif |
| 28 | 30 | ||
| 29 | /** server stat **/ | 31 | /** server state **/ |
| 30 | |||
| 31 | srv_state s_state = {0}; | ||
| 32 | srv_state *p_state = NULL; | 32 | srv_state *p_state = NULL; |
| 33 | 33 | ||
| 34 | 34 | ||
| @@ -622,6 +622,74 @@ void term_handler(int s) | |||
| 622 | } | 622 | } |
| 623 | 623 | ||
| 624 | 624 | ||
| 625 | srv_state *srv_state_op(xs_str **fname, int op) | ||
| 626 | /* opens or deletes the shared memory object */ | ||
| 627 | { | ||
| 628 | int fd; | ||
| 629 | srv_state *ss = NULL; | ||
| 630 | |||
| 631 | if (*fname == NULL) | ||
| 632 | *fname = xs_fmt("/%s_snac_state", xs_dict_get(srv_config, "host")); | ||
| 633 | |||
| 634 | switch (op) { | ||
| 635 | case 0: /* open for writing */ | ||
| 636 | if ((fd = shm_open(*fname, O_CREAT | O_RDWR, 0666)) != -1) { | ||
| 637 | ftruncate(fd, sizeof(*ss)); | ||
| 638 | |||
| 639 | if ((ss = mmap(0, sizeof(*ss), PROT_READ | PROT_WRITE, | ||
| 640 | MAP_SHARED, fd, 0)) == MAP_FAILED) | ||
| 641 | ss = NULL; | ||
| 642 | |||
| 643 | close(fd); | ||
| 644 | } | ||
| 645 | |||
| 646 | if (ss == NULL) { | ||
| 647 | /* shared memory error: just create a plain structure */ | ||
| 648 | srv_log(xs_fmt("warning: shm object error (%s)", strerror(errno))); | ||
| 649 | ss = malloc(sizeof(*ss)); | ||
| 650 | } | ||
| 651 | |||
| 652 | /* init structure */ | ||
| 653 | *ss = (srv_state){0}; | ||
| 654 | ss->s_size = sizeof(*ss); | ||
| 655 | |||
| 656 | break; | ||
| 657 | |||
| 658 | case 1: /* open for reading */ | ||
| 659 | if ((fd = shm_open(*fname, O_RDONLY, 0666)) != -1) { | ||
| 660 | if ((ss = mmap(0, sizeof(*ss), PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) | ||
| 661 | ss = NULL; | ||
| 662 | |||
| 663 | close(fd); | ||
| 664 | } | ||
| 665 | |||
| 666 | if (ss == NULL) { | ||
| 667 | /* shared memory error */ | ||
| 668 | srv_log(xs_fmt("error: shm object error (%s) server not running?", strerror(errno))); | ||
| 669 | } | ||
| 670 | else | ||
| 671 | if (ss->s_size != sizeof(*ss)) { | ||
| 672 | srv_log(xs_fmt("error: struct size mismatch (%d != %d)", | ||
| 673 | ss->s_size, sizeof(*ss))); | ||
| 674 | |||
| 675 | munmap(ss, sizeof(*ss)); | ||
| 676 | |||
| 677 | ss = NULL; | ||
| 678 | } | ||
| 679 | |||
| 680 | break; | ||
| 681 | |||
| 682 | case 2: /* unlink */ | ||
| 683 | if (*fname) | ||
| 684 | shm_unlink(*fname); | ||
| 685 | |||
| 686 | break; | ||
| 687 | } | ||
| 688 | |||
| 689 | return ss; | ||
| 690 | } | ||
| 691 | |||
| 692 | |||
| 625 | void httpd(void) | 693 | void httpd(void) |
| 626 | /* starts the server */ | 694 | /* starts the server */ |
| 627 | { | 695 | { |
| @@ -631,12 +699,11 @@ void httpd(void) | |||
| 631 | pthread_t threads[MAX_THREADS] = {0}; | 699 | pthread_t threads[MAX_THREADS] = {0}; |
| 632 | int n; | 700 | int n; |
| 633 | xs *sem_name = NULL; | 701 | xs *sem_name = NULL; |
| 702 | xs *shm_name = NULL; | ||
| 634 | sem_t anon_job_sem; | 703 | sem_t anon_job_sem; |
| 635 | 704 | ||
| 636 | /* setup the server stat structure */ | 705 | /* setup the server stat structure */ |
| 637 | { | 706 | p_state = srv_state_op(&shm_name, 0); |
| 638 | p_state = &s_state; | ||
| 639 | } | ||
| 640 | 707 | ||
| 641 | p_state->srv_start_time = time(NULL); | 708 | p_state->srv_start_time = time(NULL); |
| 642 | 709 | ||
| @@ -736,6 +803,8 @@ void httpd(void) | |||
| 736 | sem_close(job_sem); | 803 | sem_close(job_sem); |
| 737 | sem_unlink(sem_name); | 804 | sem_unlink(sem_name); |
| 738 | 805 | ||
| 806 | srv_state_op(&shm_name, 2); | ||
| 807 | |||
| 739 | xs *uptime = xs_str_time_diff(time(NULL) - p_state->srv_start_time); | 808 | xs *uptime = xs_str_time_diff(time(NULL) - p_state->srv_start_time); |
| 740 | 809 | ||
| 741 | srv_log(xs_fmt("httpd%s stop %s:%s (run time: %s)", | 810 | srv_log(xs_fmt("httpd%s stop %s:%s (run time: %s)", |