void find_available_memory (void) { char map[256]; /* 0: available, 1: busy. */ #define AVAILABLE 0 #define BUSY 1 #define NULLTRAP 2 #define STACK 3 #define OSAVAILABLE 4 #define ACCURATEBUSY 5 //int fd; int fd_file; caddr_t addr; unsigned int i, j; unsigned int i1, j1; int begin, len; int deepsearch; set_sig_handler (); fd_file = open (TMP_FILE, O_RDWR | O_CREAT, 0666); if (fd_file == -1) buggy_os (); if (write (fd_file, "hello", 4) != 4) buggy_os (); /* Search for available area. */ for (i = 0; i < 256; i++) { if (check_for_ptr ((char *) (i << 24), fd_file) == 0) { map[i] = BUSY; SPAM2(2,"Quick Check 0x%08x-0x%08x\t===> Busy\n", i << 24, ((i + 1) << 24) - 1) } else {
int main(int argc, char **argv) { if (argc < 4) { printf("\n\tUsage: %s <host> <port> <tcp|udp>\n\n", argv[0]); return 0; } set_sig_handler(SIGINT, int_handler); set_sig_handler(SIGURG, urg_handler); char *host = argv[1]; char *port = argv[2]; char *protocol = argv[3]; int server_socket = create_socket(protocol); if (server_socket == -1) { perror("create_socket() error"); return 0; } if (bind_socket(server_socket, host, atoi(port)) == -1) { perror("bind_socket() error"); close(server_socket); return 0; } bool is_tcp = !strcmp(protocol, "tcp"); if (is_tcp) TcpServer(server_socket); else UdpServer(server_socket); close(server_socket); return 0; }
/** * 初期化処理 * * @return なし */ void cut_startup(void) { set_sig_handler(); /* バッファリングしない */ if (setvbuf(stdin, NULL, _IONBF, 0)) cut_notify("setvbuf: stdin(%d)", errno); if (setvbuf(stdout, NULL, _IONBF, 0)) cut_notify("setvbuf: stdout(%d)", errno); (void)memset(&server, 0, sizeof(testserver)); test_init_server(&server); /* リダイレクト */ redirect(STDERR_FILENO, "/dev/null"); }
/* 0: bad, 1: Ok */ int check_for_ptr (char *ptr, int fd_file) { int res; caddr_t addr; /* An available area must not be busy. */ res = write (fd_file, ptr, 8); if (res == -1) { if (errno != EFAULT) buggy_os (); } else { /* Success: this mean that the memory is already used. */ return 0; /* Don't touch. */ } /* An available area must be usuable. */ addr = anon_mmap (ptr, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED); if (addr == (caddr_t) -1) return 0; res = setjmp (env); if (res == 0) { /* Now, test it. */ *(volatile char *)addr += 1; munmap (addr, pagesize); return 1; } else { SPAM2(1, "Received signal %d at addr = 0x%08x\n", res, (unsigned)addr); munmap (addr, pagesize); set_sig_handler (); return 0; } return 1; }
/** * main関数 * * @param[in] argc 引数の数 * @param[in] argv コマンド引数・オプション引数 * @return ステータス */ int main(int argc, char *argv[]) { dbglog("start"); set_progname(argv[0]); /* シグナルハンドラ設定 */ set_sig_handler(); /* オプション引数 */ parse_args(argc, argv); /* バッファリングしない */ if (setvbuf(stdin, (char *)NULL, _IONBF, 0)) outlog("setvbuf: stdin"); if (setvbuf(stdout, (char *)NULL, _IONBF, 0)) outlog("setvbuf: stdout"); /* スレッド生成 */ create_threads(); exit(EXIT_SUCCESS); return EXIT_SUCCESS; }
int main (int argc, char **argv) { const char *progname = NULL; const char *script = NULL; int child_status = 0; size_t i; array_init (&new_argv); set_sig_handler (); /* discard runawk's own ARGV[0] */ --argc, ++argv; if (argc == 0) { usage (); return 30; } /* cwd */ if (!getcwd (cwd, sizeof (cwd))) { perror ("runawk: getcwd (3) failed"); die (32); } array_pushdup (&new_argv, NULL); /* will fill in progname later */ if (AWK2) { array_pushdup (&new_argv, AWK2); } /* need to parse a run-together shebang line? */ if (argc >= 2 && argv [0][0] == '-' && argv[1][0] != '-' && strchr(argv[0], ' ')) { shebang.argc = --argc; shebang.argv = ++argv; array_init (&shebang.array); char *p; char *token = argv[-1]; for (p = token; *p; ) { if (*p == ' ') { *p++ = 0; array_pushdup(&shebang.array, token); token = p; } else { p++; } } if (p > token) { array_pushdup(&shebang.array, token); } argc = shebang.array.size; argv = (char **) shebang.array.contents; } else { shebang.argc = 0; } /* parse options manually */ for (; argc && argv [0][0] == '-'; --argc, ++argv) { /* --help */ if (!strcmp (argv [0], "--help")) { usage (); die (0); } /* --version */ if (!strcmp (argv [0], "--version")) { version (); die (0); } /* --stdin */ if (!strcmp (argv [0], "--stdin")) { add_stdin = 1; continue; } /* -F <FS>*/ if (!strcmp (argv [0], "-F")) { if (argc == 1) { fprintf (stderr, "runawk: missing argument for -F option\n"); die (39); } array_pushdup (&new_argv, "-F"); array_pushdup (&new_argv, argv [1]); --argc; ++argv; continue; } /* -F<FS>*/ if (!strncmp (argv [0], "-F", 2)) { array_pushdup (&new_argv, "-F"); array_pushdup (&new_argv, argv [0]+2); continue; } /* -v|--assign <VAR=VALUE> */ if (!strcmp (argv [0], "-v") || !strcmp (argv [0], "--assign")) { if (argc == 1) { fprintf (stderr, "runawk: missing argument for -v option\n"); die (39); } array_pushdup (&new_argv, "-v"); array_pushdup (&new_argv, argv [1]); --argc; ++argv; continue; } /* -v<VAR=VALUE> */ if (!strncmp (argv [0], "-v", 2)) { array_pushdup (&new_argv, "-v"); array_pushdup (&new_argv, argv [0]+2); continue; } /* -f|--file <FILE> */ if (!strcmp (argv [0], "-f") || !strcmp (argv [0], "--file")) { if (argc == 1) { fprintf (stderr, "runawk: missing argument for -f option\n"); die (39); } add_file (cwd, argv [1], 0); --argc; ++argv; continue; } /* -f<FILE> */ if (!strncmp (argv [0], "-f", 2)) { add_file (cwd, argv [0]+2, 0); continue; } /* -e|--source <PROGRAM TEXT> */ if (!strcmp (argv [0], "-e") || !strcmp (argv [0], "--source")) { if (argc == 1) { fprintf (stderr, "runawk: missing argument for -e option\n"); die (39); } script = argv [1]; --argc; ++argv; continue; } /* -e<PROGRAM TEXT> */ if (!strncmp (argv [0], "-e", 2)) { script = argv [0]+2; continue; } if (!strcmp (argv [0], "--exec")) { if (shebang.argc) { if (script) { fprintf (stderr, "runawk: --exec conflicts with --source\n"); die (39); } execing = 1; // add_file(cwd, shebang.argv [0], 1); // ++shebang.argv; // --shebang.argc; --argc; ++argv; break; } if (argc == 1) { fprintf (stderr, "runawk: missing argument for --exec option\n"); die (39); } execing = 1; // add_file (cwd, argv [1], 1); --argc; ++argv; break; } /* -- */ if (!strcmp (argv [0], "--")) { --argc; ++argv; break; } /* --unknown */ if (!strncmp (argv [0], "--", 2)) { array_pushdup (&new_argv, argv [0]); continue; } /* -x etc */ if (argv[0][1]) { fprintf (stderr, "runawk: unknown option -%c\n", *(argv [0]+1)); die (1); } /* - */ break; } if (shebang.argc) { if (argc) { fprintf (stderr, "runawk: can't parse shebang line: %s\n", argv[0]); die (1); } array_free (&shebang.array); argv = shebang.argv; argc = shebang.argc; } progname = interpreter; if (script) { add_buffer (script, strlen (script)); } else { /* program_file */ if (argc < 1) { usage (); die (30); } --argc; add_file (cwd, *argv, execing); progname = *argv; #if 0 setprogname (*argv); setproctitle (*argv); #endif ++argv; } /* exec */ new_argv.contents [0] = xstrdup (progname); if (files && !execing) array_pushdup (&new_argv, "--"); for (i=0; i < (size_t) argc; ++i) { array_pushdup (&new_argv, argv [i]); } if (add_stdin) { array_pushdup (&new_argv, "/dev/stdin"); } array_pushdup (&new_argv, NULL); awk_pid = fork (); switch (awk_pid) { case -1: perror ("runawk: fork(2) failed"); die (42); break; case 0: /* child */ execvp (interpreter, (char *const *) new_argv.contents); fprintf (stderr, "runawk: running '%s' failed: %s\n", interpreter, strerror (errno)); exit (1); break; default: /* parent */ waitpid (-1, &child_status, 0); array_free (&new_argv); if (killing_sig) { die (0); } else if (WIFSIGNALED (child_status)) { die (128 + WTERMSIG (child_status)); } else if (WIFEXITED (child_status)) { die (WEXITSTATUS (child_status)); } else { die (200); } } die (0); return 0; /* this should not happen but fixes gcc warning */ }
/** * * @param argc * @param argv * @return */ int main(int argc, char** argv) { if (0 != getuid()) { printf("%s", INSUFF_PRIV); exit(EXIT_FAILURE); } start_time = time(NULL); char *host = NULL; int opt = 0; int longIndex = 0; memset(globals.routes, 0, ROUTES_MAX_COUNT); // curl_global_init(CURL_GLOBAL_ALL); // watchdog_stop_flag = false; sheduler_stop_flag = false; pbuffer_stop_flag = false; rotate_stop_flag = false; listen_stop_flag = false; /* Initialize arguments before we get to work. */ char * argument = NULL; argument = "/var/run/arewik.pid"; arguments.pidfile = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.pidfile, argument); argument = "/var/log/arewik"; arguments.logdir = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.logdir, argument); argument = "/var/arewik"; arguments.storagedir = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.storagedir, argument); argument = "/var/arewik/buffers"; arguments.bufferdir = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.bufferdir, argument); argument = "/etc/arewik/arewik.conf"; arguments.configfile = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.configfile, argument); argument = "arewik"; arguments.user = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.user, argument); argument = "arewik"; arguments.group = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.group, argument); argument = "/tmp"; arguments.wdir = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.wdir, argument); argument = DEFAULT_LISTEN_IP; arguments.listen_host = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(arguments.listen_host, argument); arguments.listen_port = DEFAULT_LISTEN_PORT; arguments.verbosity = 0; arguments.foreground = 0; arguments.debuginfo = 0; argument = "custom.log"; globals.custom_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(globals.custom_logfile_name, argument); argument = "debug.log"; globals.debug_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(globals.debug_logfile_name, argument); argument = "connections.log"; globals.connections_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(globals.connections_logfile_name, argument); argument = "access.log"; globals.access_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1); strcpy(globals.access_logfile_name, argument); globals.maxcon = 4096; // routes max * 64 globals.proxymode = false; //todo globals.routes_cnt = 1; globals.watchdog_interval = WATCHDOG_T; globals.ping_interval = 30; globals.autoreconfigure = false; globals.reconfigure_interval = 60; globals.use_resolver = 0; globals.use_syslog = 0; globals.socktimeout = 10000; globals.epolltimeout = EPOLL_RUN_TIMEOUT; globals.workers = POSSIBLE_FHNDL; sprintf(globals.identline, "%s", ident); // globals.modules[0].id = 0; globals.modules[0].enabled = true; globals.modules[0].name = "plain"; globals.modules[0].process_function = &process_plain; globals.modules[0].init_function = NULL; globals.modules[1].id = 1; globals.modules[1].enabled = false; globals.modules[1].name = "riak"; globals.modules[1].process_function = &process_riak; globals.modules[1].init_function = &init_riak_module; globals.modules[1].close_function = &close_riak; globals.modules[2].id = 2; globals.modules[2].enabled = false; globals.modules[2].name = "esearch"; globals.modules[2].process_function = &process_esearch; globals.modules[2].init_function = &init_esearch_module; globals.modules[2].close_function = &close_esearch; globals.modules[3].id = 3; globals.modules[3].enabled = false; globals.modules[3].name = "webhdfs"; globals.modules[3].process_function = &process_webhdfs; globals.modules[3].init_function = &init_webhdfs_module; globals.modules[3].close_function = &close_webhdfs; CPRESS_FUNCT[0].type = SNAPPY; CPRESS_FUNCT[0].compress_function = &compress_snappy; CPRESS_FUNCT[1].type = GZIP; CPRESS_FUNCT[1].compress_function = &compress_gzip; set_sig_handler(); static const char *optString = "s:D:B:u:g:p:l:c:H:P:vfhd?"; opt = getopt_long(argc, argv, optString, longOpts, &longIndex); while (opt != -1) { switch (opt) { case 'p': if (!is_valid_opt(optarg)) { print_usage(); } arguments.pidfile = xrealloc(arguments.pidfile, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.pidfile, optarg); break; case 'l': if (!is_valid_opt(optarg)) { print_usage(); } arguments.logdir = xrealloc(arguments.pidfile, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.logdir, optarg); break; case 'c': if (!is_valid_opt(optarg)) { print_usage(); } arguments.configfile = xrealloc(arguments.configfile, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.configfile, optarg); break; case 'H': if (!isValidIP(optarg)) { print_usage(); } if (NULL != (host = nslookup(optarg))) { arguments.listen_host = xrealloc(arguments.listen_host, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.listen_host, host); } else { printf("%s\n", hstrerror(h_errno)); exit(EXIT_FAILURE); } break; case 'u': if (!is_valid_opt(optarg)) { print_usage(); } arguments.user = xrealloc(arguments.user, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.user, optarg); break; case 'g': arguments.group = xrealloc(arguments.group, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.group, optarg); break; case 'D': if (!is_valid_opt(optarg)) { print_usage(); } arguments.wdir = xrealloc(arguments.wdir, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.wdir, optarg); break; case 's': if (!is_valid_opt(optarg)) { print_usage(); } arguments.storagedir = xrealloc(arguments.storagedir, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.storagedir, optarg); break; case 'B': if (!is_valid_opt(optarg)) { print_usage(); } arguments.bufferdir = xrealloc(arguments.bufferdir, (sizeof (char) * strlen(optarg) + 1)); strcpy(arguments.bufferdir, optarg); break; case 'P': arguments.listen_port = atoi(optarg); break; case 'v': arguments.verbosity++; break; case 'f': arguments.foreground++; break; case 'h': print_usage(); break; case 'd': arguments.debuginfo++; arguments.verbosity++; break; default: if (!is_valid_opt(optarg)) { print_usage(); } break; } opt = getopt_long(argc, argv, optString, longOpts, &longIndex); } if (false == DirectoryExists(arguments.storagedir)) { printf("Specified storage directory '%s' does not exists\n", arguments.storagedir); printf("Creating new one\n"); if (0 > mkdir(arguments.storagedir, 0755)) { printf("Can not create storage directory - %s\n", strerror(errno)); exit(EXIT_FAILURE); } else { setRightOwner(arguments.storagedir); } } setRightOwner(arguments.storagedir); if (false == DirectoryExists(arguments.bufferdir)) { printf("Specified buffer directory '%s' does not exists\n", arguments.bufferdir); printf("Creating new one\n"); if (0 > mkdir(arguments.bufferdir, 0755)) { printf("Can not create buffer directory - %s\n", strerror(errno)); exit(EXIT_FAILURE); } else { setRightOwner(arguments.bufferdir); } } setRightOwner(arguments.bufferdir); if (true == DirectoryExists(arguments.pidfile)) { printf("Specified pid file '%s' is a directory\n", arguments.pidfile); exit(EXIT_FAILURE); } if (false == DirectoryExists(arguments.wdir)) { printf("Specified working directory '%s' does not exists or not a directory\n", arguments.wdir); exit(EXIT_FAILURE); } if (arguments.foreground == 1) { printf("Running in foreground mode...\n"); setbuf(stdout, 0); setbuf(stdin, 0); setbuf(stderr, 0); } if (arguments.verbosity == 1) { printf("Verbosity enabled\n"); } if (false == FileExists(arguments.configfile)) { printf("Specified file '%s' does not exists\n", arguments.configfile); exit(EXIT_FAILURE); } else if (true == DirectoryExists(arguments.configfile)) { printf("Specified file '%s' is a directory\n", arguments.configfile); exit(EXIT_FAILURE); } else { setRightOwner(arguments.configfile); if (true != hasRightOwner(arguments.configfile)) { printf("The file '%s' has invalid owner/group\nMust be %s:%s\n", arguments.configfile, arguments.user, arguments.group); exit(EXIT_FAILURE); } } if (false == DirectoryExists(arguments.logdir)) { printf("Specified log directory '%s' does not exists\n", arguments.logdir); printf("Creating new one\n"); if (0 > mkdir(arguments.logdir, 0755)) { printf("Can not create log directory - %s\n", strerror(errno)); exit(EXIT_FAILURE); } else { setRightOwner(arguments.logdir); } } setRightOwner(arguments.logdir); if (true != hasRightOwner(arguments.logdir)) { printf("The file '%s' has invalid owner/group\nMust be %s:%s\n", arguments.logdir, arguments.user, arguments.group); exit(EXIT_FAILURE); } glob = readConfig(arguments.configfile); checkPid(); openCustomLog(); openDebugLog(); openConLog(); openAccessLog(); if (!(pwd = getpwnam(arguments.user))) { snprintf(TMP_MSG, MAXLINE, "No such user: %s. Quitting!", arguments.user); writeToCustomLog(TMP_MSG); exit(EXIT_FAILURE); } else my_uid = pwd->pw_uid; if (!(grp = getgrnam(arguments.group))) { snprintf(TMP_MSG, MAXLINE, "No such group: %s. Quitting!", arguments.group); writeToCustomLog(TMP_MSG); exit(EXIT_FAILURE); } else my_gid = grp->gr_gid; if (arguments.foreground == 0) { pid = fork(); if (pid < 0) { snprintf(TMP_MSG, MAXLINE, "%s", "Can not fork! [Invalid PID]"); writeToCustomLog(TMP_MSG); removePid(); exit(EXIT_FAILURE); } /* If we got a good PID, then we can exit the parent process. */ if (pid > 0) { exit(EXIT_SUCCESS); } // snprintf(TMP_MSG, MAXLINE, "%s", "Forked successfully"); writeToCustomLog(TMP_MSG); /* Change the file mode mask */ umask(027); /* Create a new SID for the child process */ sid = setsid(); if (sid < 0) { snprintf(TMP_MSG, MAXLINE, "%s", "Can not create child process"); writeToCustomLog(TMP_MSG); /* Log the failure */ removePid(); exit(EXIT_FAILURE); } snprintf(TMP_MSG, MAXLINE, "%s", "Daemonizing"); writeToCustomLog(TMP_MSG); /* Change the current working directory */ if ((chdir(arguments.wdir)) < 0) { snprintf(TMP_MSG, MAXLINE, "%s", "Can not change current directory"); writeToCustomLog(TMP_MSG); /* Log the failure */ exit(EXIT_FAILURE); } snprintf(TMP_MSG, MAXLINE, "%s", "Working directory changed"); writeToCustomLog(TMP_MSG); } savePid(); if ((setgid(my_gid)) < 0) { snprintf(TMP_MSG, MAXLINE, "ERROR: setgid(%d) failed: %s", my_gid, strerror(errno)); writeToCustomLog(TMP_MSG); halt(); } snprintf(TMP_MSG, MAXLINE, "Group ID changed to %d", my_gid); writeToCustomLog(TMP_MSG); if ((setuid(my_uid)) < 0) { snprintf(TMP_MSG, MAXLINE, "ERROR: setuid(%d) failed: %s", my_uid, strerror(errno)); writeToCustomLog(TMP_MSG); halt(); } snprintf(TMP_MSG, MAXLINE, "User ID changed to %d", my_gid); writeToCustomLog(TMP_MSG); if (arguments.foreground == 0) { snprintf(TMP_MSG, MAXLINE, "%s", "Closing descriptors & disabling verbosity.\nEnsure you have set right permissions for your log file.\nWatch your log file for further information\n"); VERBOSE(TMP_MSG); arguments.verbosity = 0; /* Close out the standard file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } //////////////////////////////////////////////////////////////////////////////// // init_arrays(); init_webhdfs_arrays(); init_active_c_table(); init_descriptors(); scan_connections(); // //////////////////////////////////////////////////////////////////////////////// spawn_threads(); do_listen(); //////////////////////////////////////////////////////////////////////////////// /* This never happens */ return 0; }
/** * 初期化処理 * * @return なし */ void cut_startup(void) { set_sig_handler(); }