Exemplo n.º 1
0
/* Debug Helpers */
inline void flex_debug_out(flex_t flex){
    log_success("BEGIN DEBUG");
    log_warn("index length -[%ld]", flex->index_length);
    log_warn("data length --[%ld]", flex->last_data_size);
    log_warn("num super ----[%ld]", flex->num_super_blocks);
    log_warn("userstuff ----[%ld, %ld]", flex->num_user_elements_inserted, flex->usable_data_blocks);
    log_warn("index --------[%ld, %ld]", flex->last_index_occup, flex->index_length);
    log_warn("super --------[%ld, %ld]", flex->last_super_occup, flex->last_super_size);
    log_success("END DEBUG");
}
Exemplo n.º 2
0
int command_list_db(int argc, char **argv, int optind, int flags) {
  struct DBItem dbi = DBITEM_NULL;
  struct DBItem *e = NULL;
  char *p = NULL;
  int do_free_p = 0;
  int i = 0;

  for(i = optind; i < argc; ++i) {
    DBITEM_SET_NULL(dbi);
    e = NULL;
    if(access(argv[i], F_OK | R_OK) != 0 || DB_read(argv[i], &dbi) != 0) {
      log_failure(argv[i], "doesn't exist or isn't a valid database");
      continue;
    }
    if(dbi.kbuf == NULL) {
      log_info(argv[i], "is empty");
      continue;
    }
    e = &dbi;
    do {
      p = e->kbuf;
      if(flags & USE_REALPATH)
        p = get_realpath((const char*) e->kbuf, &do_free_p);
      log_success(argv[i], "%s -> %08X", p, e->crc);
      if(do_free_p == 1) {
        free(p);
        do_free_p = 0;
      }
    } while((e = e->next) != NULL);
    if(dbi.next != NULL)
      DB_item_free(dbi.next);
  }

  return EXIT_SUCCESS;
}
Exemplo n.º 3
0
struct daemon *
daemon_remove (struct daemon *l, struct daemon *d) {
    struct daemon   *tmp;

    /* FIXME: if we daemon_remove a non-existing daemon, I think we're
    screwed */

    pthread_mutex_lock (&nb_daemons.lock);
    log_success (log_file, "DAEMON--");
    --nb_daemons.count;
    pthread_mutex_unlock (&nb_daemons.lock);

    if (!l)
        return NULL;
    for (tmp = l; tmp != d; tmp = tmp->next);

    /* This was the last element */
    if (tmp->prev == NULL && tmp->next == NULL) {
        return NULL;
    }

    /* Was the first element */
    if (!tmp->prev) {
        tmp->next->prev = NULL;
        return tmp->next;
    }

    tmp->prev->next = tmp->next;
    return l;
}
Exemplo n.º 4
0
int command_check_hexstring(int argc, char **argv, int optind,
    int flags) {
  int ret = EXIT_SUCCESS;
  int i = optind-1;
  uint32_t strcrc, crc;
  long int cast;

  if(flags & CALC_PRINT_NUMERICAL)
    cast = (uint32_t) strtol(hexarg, NULL, 10);
  else
    cast = (uint32_t) strtol(hexarg, NULL, 16);
  if((cast == LONG_MIN || cast == LONG_MAX) &&
    errno == ERANGE)
    cast = 0;
  strcrc = (uint32_t) cast;

  while(argv[++i]) {
    if(check_access_flags_v(argv[i], F_OK | R_OK, 1) != 0) {
      log_failure(argv[i], "not accessible, not a file or doesn't exist");
      ret = EXIT_FAILURE;
      continue;
    }
    if((crc = compute_crc32(argv[i])) == strcrc)
      log_success(argv[i], "match");
    else 
      log_failure(argv[i], "mismatch: %08X is really %08X", strcrc, crc);
  }

  return ret;
}
Exemplo n.º 5
0
void *
handle_daemon (void *arg) {
    struct daemon           *d;
    struct daemon_request   *tmp;

    d    = (struct daemon *)arg;
    if (!d)
        goto out;

    sem_wait (&daemons_lock);
    daemons = daemon_add (daemons, d);
    sem_post (&daemons_lock);
    if (!daemons) {
        daemon_free (d);
        goto out;
    }

    log_success (log_file, "BEGIN   daemon %s", d->addr);
    handle_requests (d);
    log_success (log_file, "END     daemon %s", d->addr);

    sem_wait (&daemons_lock);
    daemons = daemon_remove (daemons, d);
    sem_post (&daemons_lock);

    /* Let's clean all remaining requests for this daemon */
    sem_wait (&d->req_lock);
    for (tmp = d->requests; tmp; tmp = tmp->next) {
        /* Either it's been assigned to a thread in the pool */
        if (tmp->assigned)
            pool_kill (tmp->pool, tmp->tid);
        /* Or it's still in the queue */
        else
            pool_flush_by_arg (tmp->pool, tmp);
        daemon_request_free (tmp);
    }
    sem_post (&d->req_lock);

    daemon_free (d);

out:
    sem_wait (&nb_daemons.lock);
    --nb_daemons.count;
    sem_post (&nb_daemons.lock);

    return NULL;
}
Exemplo n.º 6
0
int logWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QWidget::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        switch (_id) {
        case 0: log_success(); break;
        case 1: appendText((*reinterpret_cast< const char*(*)>(_a[1]))); break;
        case 2: crossClicked(); break;
        default: ;
        }
        _id -= 3;
    }
    return _id;
}
Exemplo n.º 7
0
struct daemon *
daemon_add (struct daemon *l, struct daemon *d) {
    pthread_mutex_lock (&nb_daemons.lock);
    log_success (log_file, "DAEMON++");
    ++nb_daemons.count;
    pthread_mutex_unlock (&nb_daemons.lock);

    if (!d) {
        return l;
    }
    if (!l) {
        return d;
    }
    d->prev = NULL;
    d->next = l;
    l->prev = d;

    return d;
}
Exemplo n.º 8
0
int command_check(int argc, char **argv, int optind, int flags) {
  int ret = EXIT_SUCCESS;
  int i = optind-1;
  int  ci, ti;
  uint32_t compcrc, matchcrc;
  char *string;
  char results[9];
  regmatch_t rmatch;
  regex_t regex;

  while(argv[++i]) {
    if(check_access_flags_v(argv[i], F_OK | R_OK, 1) != 0) {
      log_info(argv[i], "Inaccessbile file, skipping.");
      continue;
    }
    string = get_basename((char*)argv[i]);
    compile_regex(&regex, crcregex, REG_ICASE);
    switch(regexec((const regex_t*) &regex, string, 1, &rmatch, 0)) {
      case 0:
        for(ci = rmatch.rm_so, ti = 0; ci < rmatch.rm_eo; ++ci)
            results[ti++] = string[ci];
        results[ti] = '\0';
        break;
      case REG_NOMATCH:
        log_info(argv[i], "Does not contain a hexstring, ignoring.");
        continue;
    }
    regfree(&regex);
    compcrc = compute_crc32(argv[i]);
    matchcrc = (uint32_t) strtol(results, NULL, 16);
    if(compcrc == matchcrc)
      log_success(argv[i], "OK");
    else {
      log_failure(argv[i], "Mismatch: %08X is really %08X", matchcrc, compcrc);
      ret = EXIT_FAILURE;
    }
  } /* while */

  return ret;
}
Exemplo n.º 9
0
void*
daemon_request_ready (void* arg) {
    struct daemon_request   *r;
    // Parse elements
    char                    *key, *delay, *ip, *port, *proto, *begin, *end;
/* cmd version: */
    int                     argc;
    char                    **argv;
    int                     dl_sock;
    struct sockaddr_in      dl_addr;
    struct file_cache       *file;
    int                     local_file;
    int                     nb_received;
    file_size_t             nb_received_sum;
    char                    buffer[BUFFSIZE];
    int                     nb_written;
    char                    *full_path;
    char                    error_buffer[BUFFSIZE];

    r = (struct daemon_request *) arg;
    if (!r)
        return NULL;

    /*
     * cmd is supposedly:
     * ready KEY DELAY IP PORT PROTOCOL BEGINNING END
     */
    argv = cmd_to_argc_argv (r->cmd, &argc);
    if (argc < 8) {
        cmd_free (argv);
        sprintf (error_buffer, 
                 "error %s: Invalid number of arguments",
                 __FUNCTION__);
        daemon_send (r->daemon, error_buffer);
        return NULL;
    }
    key = argv[1];
    delay = argv[2];
    ip = argv[3];
    port = argv[4];
    proto = argv[5];
    begin = argv[6];
    end = argv[7];

    /* TODO&FIXME: We should use all above arguments */
    if ((dl_sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
        log_failure (log_file, "daemon_request_ready (): socket () failed");
        return NULL;
    }

    dl_addr.sin_family = AF_INET;
    if (inet_pton (AF_INET, ip, &dl_addr.sin_addr) < 1) {
        log_failure (log_file, "dr_ready: inet_pton () failed");
        return NULL;
    }
    // TODO: Verifications on port
    dl_addr.sin_port = htons (atoi (port));

    file = file_cache_get_by_key (file_cache, key);
    if (!file) {
        log_failure (log_file, "dr_ready: file_cache_get_by_key () failed");
        return NULL;
    }

    // TODO: Check if we actually asked for that file

    // TODO: Check if file already exists

    /* + 2 for '/' and '\0' */
    full_path = (char *)malloc ((strlen (prefs->shared_folder)
                                + strlen (file->filename)
                                + 2) * sizeof (char));
    sprintf (full_path, "%s/%s", prefs->shared_folder, file->filename);
    // FIXME: We should not truncate the file when downloading it by blocks
    local_file = open (full_path,
                        O_WRONLY | O_TRUNC | O_CREAT,
                        (mode_t)0644);
    //free (full_path);
    if (local_file < 0) {
        log_failure (log_file,
                    "dr_ready: open () failed, error: %s",
                    strerror (errno));
        return NULL;
    }

    if (connect (dl_sock, (struct sockaddr *)&dl_addr, sizeof (dl_addr)) < 0) {
        log_failure (log_file,
                    "dr_ready: connect () failed, error: %s",
                    strerror (errno));
        return NULL;
    }

    /*
     * Downloading the file
     */

    /* Let's upload the download queue */
    struct dl_file *f;
    
    f = dl_file_new (full_path, file->size);
    if (!f) {
        log_failure (log_file, "struct dl_file is NULL :(");
        goto out;
    }

    sem_wait (&downloads_lock);
    downloads = dl_file_add (downloads, f);
    if (!downloads) {
        log_failure (log_file, 
                    "Could not add the file to the download queue\n");
        goto out;
    }
    sem_post (&downloads_lock);

    nb_received_sum = 0;
    // FIXME: nb_received_sum should be compared to end - begin
    sleep (2);
    while (nb_received_sum < file->size) {
        log_failure (log_file,
                     "DBG %d %d",
                     nb_received_sum,
                     file->size);
                     
        nb_received = recv (dl_sock, buffer, BUFFSIZE, 0);
        if (nb_received < 0) {
            log_failure (log_file,
                        "dr_ready: recv () failed");
            return NULL;
        }
        nb_received_sum += nb_received;
        while (nb_received) {
            nb_written = write (local_file, buffer, nb_received);
            if (nb_written < 0) {
                log_failure (log_file,
                            "dr_ready: write () failed");
                return NULL;
            }
            nb_received -= nb_written;
        }
    }

    /* 
     * Releasing the file from the download queue 
     */
    sem_wait (&downloads_lock);
    downloads = dl_file_remove (downloads, f);
    dl_file_free (f);
    sem_post (&downloads_lock);

    log_success (log_file, 
                "dr_ready: Received block completely %s",
                full_path);

    close (dl_sock);
    close (local_file);


out:
    if (full_path)
        free (full_path);
    if (argv)
        cmd_free (argv);
    return NULL;
}
Exemplo n.º 10
0
static void
server_stop (int sig) {
    struct client   *c;
    struct daemon   *d;

    pool_destroy (slow_pool);
    pool_destroy (fast_pool);
    pool_destroy (clients_pool);
    pool_destroy (daemons_pool);

    (void) sig;
    log_failure (log_file, "Ok, received a signal");
    sleep (2);

    sem_destroy (&clients_lock);
    sem_destroy (&daemons_lock);
    sem_destroy (&file_cache_lock);
    sem_destroy (&downloads_lock);

    if (clients) {
        while (clients) {
            c = clients->next;
            client_free (clients);
            clients = c;
        }
        log_success (log_file,
                     "%s : all clients have been freed",
                     __func__);
    }

    if (daemons) {
        while (daemons) {
            d = daemons->next;
            daemon_send (daemons, "quit\n");
            daemon_free (daemons);
            daemons = d;
        }
        log_success (log_file,
                     "%s : all daemons have been freed",
                     __func__);
    }

    if (file_cache) {
        file_cache_free (file_cache);
        log_success (log_file,
                     "%s : file cache has been freed",
                     __func__);
    }

    if (unlink (prefs->lock_file) < 0)
        if (log_file)
            log_failure (log_file, "Could not destroy the lock file");
    if (prefs) {
        conf_free (prefs);
        log_success (log_file,
                     "%s : Preferences have been freed.",
                     __func__);
    }

    if (log_file) {
        log_success (log_file, "Stopping server, waiting for SIGKILL");
        fclose (log_file);
    }

    exit (EXIT_SUCCESS);
}
Exemplo n.º 11
0
static void
start_server (void) {
    int                 client_sd;
    int                 daemon_sd;
    int                 connected_sd;
    struct sockaddr_in  client_sa;
    struct sockaddr_in  daemon_sa;
    struct sockaddr_in  connected_sa;
    socklen_t           size;
    fd_set              socket_set;
    int                 nfds;
    struct ifreq        if_info;
    struct sockaddr_in  *if_addr;
    char                addr[INET_ADDRSTRLEN];
    struct client       *c;
    struct daemon       *d;
    struct parsed_cmd   *pcmd = NULL;
    char                *ident_msg;
    int                 port;
    char                *colon;


    /* Prepare all the threads */
    slow_pool = NULL;
    fast_pool = NULL;
    clients_pool = NULL;
    daemons_pool = NULL;

    ABORT_IF (!(slow_pool = pool_create (prefs->nb_proc)),
        "Unable to create slow_pool")

    ABORT_IF (!(fast_pool = pool_create (prefs->nb_proc)),
        "Unable to create fast_pool")

    ABORT_IF (!(clients_pool = pool_create (prefs->max_clients)),
        "Unable to create clients_pool")

    ABORT_IF (!(daemons_pool = pool_create (prefs->max_daemons)),
        "Unable to create daemons_pool")

    /* Create the shared directory if it does not exist already */
    ABORT_IF (create_dir (prefs->shared_folder, (mode_t)0755) < 0,
                "Unable to create shared directory")

    /* Initialize global pointers and their semaphores */
    clients = NULL;
    ABORT_IF (sem_init (&clients_lock, 0, 1) < 0,
        "Unable to sem_init clients_lock")

    daemons = NULL;
    ABORT_IF (sem_init (&daemons_lock, 0, 1) < 0,
        "Unable to sem_init daemons_lock")

    file_cache = NULL;
    ABORT_IF (sem_init (&file_cache_lock, 0, 1) < 0,
        "Unable to sem_init file_cache_lock")

    list_client = NULL;
    ABORT_IF (sem_init (&list_lock, 0, 1) < 0,
        "Unable to sem_init list_lock")

    downloads = NULL;
    ABORT_IF (sem_init (&downloads_lock, 0, 1) < 0,
        "Unable to sem_init download_queue_lock")

    client_sa.sin_family        = AF_INET;
    client_sa.sin_addr.s_addr   = INADDR_ANY;
    client_sa.sin_port          = htons (prefs->client_port);
    client_sd = socket_init (&client_sa);
    ABORT_IF (client_sd < 0,
        "Unable to socket_init client_sd")

    daemon_sa.sin_family        = AF_INET;
    daemon_sa.sin_addr.s_addr   = INADDR_ANY;
    daemon_sa.sin_port          = htons (prefs->daemon_port);
    daemon_sd = socket_init (&daemon_sa);
    ABORT_IF (daemon_sd < 0,
        "Unable to socket_init daemon_sd")

#if 1
    /* We get our ip */
    memcpy (if_info.ifr_name, prefs->interface, strlen (prefs->interface) + 1);
    if (ioctl (daemon_sd, SIOCGIFADDR, &if_info) == -1) {
        log_failure (log_file, "Can't get my ip from interface");
        log_failure (log_file, "LOL ERRNO : %s\n", strerror (errno));
        goto abort;
    }
    if_addr = (struct sockaddr_in *)&if_info.ifr_addr;
    inet_ntop (AF_INET, &if_addr->sin_addr, my_ip, INET_ADDRSTRLEN);
    log_success (log_file, "Found my IP : %s", my_ip);
#endif

    /* socket_set contains both client_sd and daemon_sd */
    FD_ZERO (&socket_set);
    size = sizeof (connected_sa);
    nfds = NFDS (client_sd, daemon_sd);
    for (;;) {
        /*
         * It is VERY important to FD_SET at each loop, because select
         * will FD_UNSET the socket descriptors
         */
        FD_SET (client_sd, &socket_set);
        FD_SET (daemon_sd, &socket_set);

        /* Block until a socket is ready to accept */
        if (select (nfds, &socket_set, NULL, NULL, NULL) < 0) {
            log_failure (log_file, "main () : select failed");
        }

        if (FD_ISSET (client_sd, &socket_set)) {
            if ((connected_sd = (accept (client_sd,
                                        (struct sockaddr *) &connected_sa,
                                        &size))) < 0) {
                log_failure (log_file, "Failed to accept incoming connection.");
                break;
            }

            /* Can we handle this client? */
            if (client_count () > prefs->max_clients) {
                socket_sendline (connected_sd, " < Too many clients\n");
                goto close_socket;
            }

            /* Then, let's handle him */
            if (!inet_ntop (AF_INET, &connected_sa.sin_addr,
                            addr, INET_ADDRSTRLEN)) {
                socket_sendline (connected_sd, " < Oops\n");
                goto close_socket;
            }
            if (!(c = client_new (connected_sd, addr))) {
                socket_sendline (connected_sd, " < Sorry pal :(\n");
            }

            pool_queue (clients_pool, handle_client, c);
        }
        else if (FD_ISSET (daemon_sd, &socket_set)) {
            if ((connected_sd = (accept (daemon_sd,
                                        (struct sockaddr *) &connected_sa,
                                        &size))) < 0) {
                log_failure (log_file, "Failed to accept incoming connection.");
                break;
            }

            /* Can we handle this daemon? */
            if (daemon_count () > prefs->max_daemons) {
                socket_sendline (connected_sd, " < Too many daemons\n");
                goto close_socket;
            }

            /* Let's identify him first */
            ident_msg = socket_try_getline (connected_sd,
                                            IDENTIFICATION_TIMEOUT);
            if (!ident_msg) {
                socket_sendline (connected_sd,
                                "error: identification timed out\n");
                goto close_socket;
            }
            if (cmd_parse_failed ((pcmd = cmd_parse (ident_msg, NULL)))) {
                pcmd = NULL;
                goto close_socket;
            }
            if (pcmd->argc < 2)
                goto close_socket;
            if (strcmp (pcmd->argv[0], "neighbour") != 0)
                goto close_socket;
            if (!(colon = strchr (pcmd->argv[1], ':')))
                goto close_socket;
            port = atoi (colon + 1);

            free (ident_msg);
            cmd_parse_free (pcmd);
            pcmd = NULL;

            if (!inet_ntop (AF_INET, &connected_sa.sin_addr,
                            addr, INET_ADDRSTRLEN)) {
                socket_sendline (connected_sd, " < Oops\n");
                goto close_socket;
            }

            /* Now we've got his port, let him go in */
            if (!(d = daemon_new (connected_sd, addr, port))) {
                socket_sendline (connected_sd, " < Sorry pal :(\n");
                goto close_socket;
            }

            pool_queue (daemons_pool, handle_daemon, d);
        }
        else {
            /* This should never happen : neither client nor daemon!? */
            log_failure (log_file, "Unknown connection");
        }

        continue;

close_socket:
        if (pcmd) {
            cmd_parse_free (pcmd);
            pcmd = NULL;
        }
        close (connected_sd);
    }

abort:
    if (slow_pool)
        pool_destroy (slow_pool);
    if (fast_pool)
        pool_destroy (fast_pool);
    if (clients_pool)
        pool_destroy (clients_pool);
    if (daemons_pool)
        pool_destroy (daemons_pool);
    conf_free (prefs);
    exit (EXIT_FAILURE);
}
Exemplo n.º 12
0
void daemonize (void) {
    int lock;
    char str[10];

    switch (fork ()) {
        case -1:
            exit (1);
        case 0:
            fclose (log_file);
            if (prefs)
                conf_free (prefs);
            exit (0);
            break;
        default:
            break;
    }

    setsid ();
    log_success (log_file, "setsid ok");

    close (STDIN_FILENO);
    close (STDOUT_FILENO);
    close (STDERR_FILENO);

    log_success (log_file, "Closed stdin, stdout, stderr.");

    umask (027);
    log_success (log_file, "Set file permissions to 750.");

    lock = open (prefs->lock_file, O_RDWR | O_CREAT, 0640);
    if (lock < 0) {
        log_failure (log_file,
                     "Failed to open lock file (%s).",
                     prefs->lock_file);
    }
    else
        log_success (log_file, "Opened lock file (%s).", prefs->lock_file);

    if (lockf (lock, F_TLOCK, 0) < 0) {
        log_failure (log_file, "Could not lock %s", prefs->lock_file);
    }

    sprintf (str, "%d\n", getpid ());
    write (lock, str, strlen (str));
    if (close (lock) < 0) {
        log_failure (log_file,
                     "Failed to close LOCK_FILE (%s).",
                    prefs->lock_file);
    }
    else
        log_success (log_file, "Created lock file (%s)", prefs->lock_file);

    sigset_t mask;
    sigemptyset (&mask);
    sigaddset (&mask, SIGCHLD);
    sigaddset (&mask, SIGTSTP);
    sigaddset (&mask, SIGTTOU);
    sigaddset (&mask, SIGTTIN);
    sigprocmask (SIG_BLOCK, &mask, NULL);

    struct sigaction    on_signal;
    sigemptyset (&on_signal.sa_mask);
    on_signal.sa_flags = 0;
    on_signal.sa_handler = signal_handler;
    sigaction (SIGHUP, &on_signal, NULL);
    on_signal.sa_handler = server_stop;
    sigaction (SIGINT, &on_signal, NULL);
    sigaction (SIGTERM, &on_signal, NULL);

    log_success (log_file, "Signals deferred or handled.");

}
Exemplo n.º 13
0
void logWindow::appendText(const char* str){
	//PERR("append Text");
	logText->append(str);
	emit log_success();
	
}