684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
if (extra) {
fprintf(output, "%s\n", extra);
}
fprintf(output, "Usage: filed [<options>]\n");
fprintf(output, " Options:\n");
fprintf(output, " -h, --help\n");
fprintf(output, " -b <address>, --bind <address>\n");
fprintf(output, " -p <port>, --port <port>\n");
fprintf(output, " -t <count>, --threads <count>\n");
fprintf(output, " -c <entries>, --cache <entries>\n");
fprintf(output, " -u <user>, --user <user>\n");
fprintf(output, " -r <directory>, --root <directory>\n");
if (long_help) {
fprintf(output, "\n");
fprintf(output, " Usage:\n");
fprintf(output, " -h (or --help) prints this usage information\n");
fprintf(output, "\n");
fprintf(output, " -b (or --bind) specifies the address to listen for incoming HTTP\n");
fprintf(output, " requests on. The default value is \"%s\".\n", BIND_ADDR);
fprintf(output, "\n");
fprintf(output, " -p (or --port) specifies the TCP port number to listen for incoming HTTP\n");
fprintf(output, " requests on. The default is %u.\n", (unsigned int) PORT);
fprintf(output, "\n");
|
>
>
>
>
|
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
|
if (extra) {
fprintf(output, "%s\n", extra);
}
fprintf(output, "Usage: filed [<options>]\n");
fprintf(output, " Options:\n");
fprintf(output, " -h, --help\n");
fprintf(output, " -d, --daemon\n");
fprintf(output, " -b <address>, --bind <address>\n");
fprintf(output, " -p <port>, --port <port>\n");
fprintf(output, " -t <count>, --threads <count>\n");
fprintf(output, " -c <entries>, --cache <entries>\n");
fprintf(output, " -u <user>, --user <user>\n");
fprintf(output, " -r <directory>, --root <directory>\n");
if (long_help) {
fprintf(output, "\n");
fprintf(output, " Usage:\n");
fprintf(output, " -h (or --help) prints this usage information\n");
fprintf(output, "\n");
fprintf(output, " -d (or --daemon) instructs filed to become a daemon after initializing\n");
fprintf(output, " the listening TCP socket and log files.\n");
fprintf(output, "\n");
fprintf(output, " -b (or --bind) specifies the address to listen for incoming HTTP\n");
fprintf(output, " requests on. The default value is \"%s\".\n", BIND_ADDR);
fprintf(output, "\n");
fprintf(output, " -p (or --port) specifies the TCP port number to listen for incoming HTTP\n");
fprintf(output, " requests on. The default is %u.\n", (unsigned int) PORT);
fprintf(output, "\n");
|
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
|
return(1);
}
*user_id = user_id_check;
return(0);
}
/* Run process */
int main(int argc, char **argv) {
struct option options[8];
const char *bind_addr = BIND_ADDR, *newroot = NULL;
uid_t user = 0;
int port = PORT, thread_count = THREAD_COUNT;
int cache_size = CACHE_SIZE;
int init_ret, chroot_ret, setuid_ret, lookup_ret, chdir_ret;
int setuid_enabled = 0;
int ch;
int fd;
/* Process arguments */
filed_getopt_long_setopt(&options[0], "port", required_argument, 'p');
filed_getopt_long_setopt(&options[1], "threads", required_argument, 't');
filed_getopt_long_setopt(&options[2], "cache", required_argument, 'c');
filed_getopt_long_setopt(&options[3], "bind", required_argument, 'b');
filed_getopt_long_setopt(&options[4], "user", required_argument, 'u');
filed_getopt_long_setopt(&options[5], "root", required_argument, 'r');
filed_getopt_long_setopt(&options[6], "help", no_argument, 'h');
filed_getopt_long_setopt(&options[7], NULL, 0, 0);
while ((ch = getopt_long(argc, argv, "p:t:c:b:u:r:h", options, NULL)) != -1) {
switch(ch) {
case 'p':
port = atoi(optarg);
break;
case 't':
thread_count = atoi(optarg);
break;
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
>
|
|
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
|
return(1);
}
*user_id = user_id_check;
return(0);
}
/* Daemonize */
static int filed_daemonize(void) {
pid_t setsid_ret, fork_ret;
int chdir_ret, dup2_ret;
int fd_in, fd_out;
chdir_ret = chdir("/");
if (chdir_ret != 0) {
return(1);
}
fork_ret = fork();
if (fork_ret < 0) {
return(1);
}
if (fork_ret > 0) {
/* Parent */
waitpid(fork_ret, NULL, 0);
exit(EXIT_SUCCESS);
}
/* Child */
if (fork() != 0) {
/* Child */
exit(EXIT_SUCCESS);
}
/* Grand child */
setsid_ret = setsid();
if (setsid_ret == ((pid_t) -1)) {
return(1);
}
fd_in = open("/dev/null", O_RDONLY);
fd_out = open("/dev/null", O_WRONLY);
if (fd_in < 0 || fd_out < 0) {
return(1);
}
dup2_ret = dup2(fd_in, STDIN_FILENO);
if (dup2_ret != STDIN_FILENO) {
return(1);
}
dup2_ret = dup2(fd_out, STDOUT_FILENO);
if (dup2_ret != STDOUT_FILENO) {
return(1);
}
dup2_ret = dup2(fd_out, STDERR_FILENO);
if (dup2_ret != STDERR_FILENO) {
return(1);
}
close(fd_in);
close(fd_out);
return(0);
}
/* Run process */
int main(int argc, char **argv) {
struct option options[9];
const char *bind_addr = BIND_ADDR, *newroot = NULL;
uid_t user = 0;
int port = PORT, thread_count = THREAD_COUNT;
int cache_size = CACHE_SIZE;
int init_ret, chroot_ret, setuid_ret, lookup_ret, chdir_ret;
int setuid_enabled = 0, daemon_enabled = 0;
int ch;
int fd;
/* Process arguments */
filed_getopt_long_setopt(&options[0], "port", required_argument, 'p');
filed_getopt_long_setopt(&options[1], "threads", required_argument, 't');
filed_getopt_long_setopt(&options[2], "cache", required_argument, 'c');
filed_getopt_long_setopt(&options[3], "bind", required_argument, 'b');
filed_getopt_long_setopt(&options[4], "user", required_argument, 'u');
filed_getopt_long_setopt(&options[5], "root", required_argument, 'r');
filed_getopt_long_setopt(&options[6], "help", no_argument, 'h');
filed_getopt_long_setopt(&options[7], "daemon", no_argument, 'd');
filed_getopt_long_setopt(&options[8], NULL, 0, 0);
while ((ch = getopt_long(argc, argv, "p:t:c:b:u:r:hd", options, NULL)) != -1) {
switch(ch) {
case 'p':
port = atoi(optarg);
break;
case 't':
thread_count = atoi(optarg);
break;
|