コード例 #1
0
int procauth_init() {
    /* assume random number generator already seeded */
    list_init(&proclist);
    list_attributes_copy(&proclist, procpid_meter, 1);

    return 0;
}
コード例 #2
0
ファイル: ddata.c プロジェクト: Ape/DCPUToolchain
list_t* dbgfmt_create_list()
{
    list_t* result = malloc(sizeof(list_t));
    list_init(result);
    list_attributes_copy(result, &dbgsym_meter, 1);
    list_attributes_comparator(result, &dbgsym_comparator);
    return result;
}
コード例 #3
0
ファイル: ldconv.c プロジェクト: Beliaar/DCPUToolchain
list_t* list_create()
{
	list_t* list = malloc(sizeof(list_t));
	list_init(list);
	list_attributes_copy(list, &lconv_entry_meter, 1);
	list_attributes_comparator(list, &lconv_entry_comparator);
	list_attributes_seeker(list, &lconv_entry_seeker);
	return list;
}
コード例 #4
0
ファイル: ppimpl.c プロジェクト: patflick/DCPUToolchain
///
/// Performs preprocessing.
///
void ppimpl(freed_bstring filename, int line, freed_bstring lang, has_t has_input, pop_t input, push_t output)
{
    state_t state;
    list_init(&state.cached_input);
    list_init(&state.cached_output);
    list_init(&state.handlers);
    list_init(&state.scopes);
    list_attributes_comparator(&state.cached_input, list_comparator_char_t);
    list_attributes_comparator(&state.cached_output, list_comparator_char_t);
    list_attributes_copy(&state.cached_input, list_meter_char_t, false);
    list_attributes_copy(&state.cached_output, list_meter_char_t, false);
    list_attributes_hash_computer(&state.cached_input, list_hashcomputer_char_t);
    list_attributes_hash_computer(&state.cached_output, list_hashcomputer_char_t);
    state.has_input = has_input;
    state.input = input;
    state.output = output;
    state.current_line = line;
    state.current_filename = bstrcpy(filename.ref);
    state.default_filename = bfromcstr("<unknown>");
    state.in_single_string = false;
    state.in_double_string = false;
    if (biseqcstrcaseless(lang.ref, "asm"))
    {
        ppimpl_asm_line_register(&state);
        ppimpl_asm_expr_register(&state);
        ppimpl_asm_define_register(&state);
        ppimpl_asm_include_register(&state);
        ppimpl_asm_lua_register(&state);
        ppimpl_asm_init(&state);
    }
    else if (biseqcstrcaseless(lang.ref, "c"))
    {
        ppimpl_c_line_register(&state);
        ppimpl_c_expr_register(&state);
        ppimpl_c_define_register(&state);
        ppimpl_c_include_register(&state);
        ppimpl_c_init(&state);
    }
    ppimpl_process(&state);
    bautodestroy(lang);
    bautodestroy(filename);
}
コード例 #5
0
ファイル: ldconv.c プロジェクト: Beliaar/DCPUToolchain
list_t* list_clone(list_t* original)
{
	list_t* list = malloc(sizeof(list_t));
	list_init(list);
	list_attributes_copy(list, &lconv_entry_meter, 1);
	list_attributes_comparator(list, &lconv_entry_comparator);
	list_attributes_seeker(list, &lconv_entry_seeker);
	list_iterator_start(original);
	while (list_iterator_hasnext(original))
		list_append(list, list_iterator_next(original));
	list_iterator_stop(original);
	return list;
}
コード例 #6
0
ファイル: eventhandler.c プロジェクト: kenieevan/pcsc-lite
LONG EHInitializeEventStructures(void)
{
	(void)list_init(&ClientsWaitingForEvent);

	/* request to store copies, and provide the metric function */
    (void)list_attributes_copy(&ClientsWaitingForEvent, list_meter_int32_t, 1);

	/* setting the comparator, so the list can sort, find the min, max etc */
    (void)list_attributes_comparator(&ClientsWaitingForEvent, list_comparator_int32_t);

	(void)pthread_mutex_init(&ClientsWaitingForEvent_lock, NULL);

	return SCARD_S_SUCCESS;
}
コード例 #7
0
ファイル: dbgaux.c プロジェクト: Wallbraker/DCPUToolchain
void ddbg_create_vm()
{
    breakpoints = breakpoint_list_create();
    list_init(&backtrace);
    list_attributes_copy(&backtrace, list_meter_uint16_t, 1);
    ignore_next_breakpoint = false;
    vm = vm_create();
    vm_hook_register(vm, &ddbg_precycle_hook, HOOK_ON_PRE_CYCLE, NULL);
    vm_hook_register(vm, &ddbg_postcycle_hook, HOOK_ON_POST_CYCLE, NULL);
    vm_hook_register(vm, &ddbg_write_hook, HOOK_ON_WRITE, NULL);
    vm_hook_register(vm, &ddbg_break_hook, HOOK_ON_BREAK, NULL);
    vm_hook_register(vm, &ddbg_interrupt_hook, HOOK_ON_INTERRUPT, NULL);

    vm_hook_register(vm, &ddbg_hardware_change_hook, HOOK_ON_HARDWARE_CHANGE, NULL);
    vm_hook_register(vm, &ddbg_send_vm_state, HOOK_ON_60HZ, NULL);
    printd(LEVEL_DEFAULT, "Created VM.\n");
}
コード例 #8
0
ファイル: hosts.c プロジェクト: brownsys/pane-sshguard
int fw_init() {
    char buf[HOSTS_MAXCMDLEN];
    FILE *tmp, *deny;

    /* set the filename of the temporary configuration file */
    if (snprintf(tempflname, MAX_TEMPFILE_NAMELEN, "%s-sshguard.%u", HOSTSFILE_PATH, getpid()) >= MAX_TEMPFILE_NAMELEN) {
        sshguard_log(LOG_ERR, "'tempflname' buffer too small to hold '%s-sshguard.%u!'", HOSTSFILE_PATH, getpid());
        return FWALL_ERR;
    }

    hosts_clearsshguardblocks();

    /* place sshguard block delimiters (header/footer) into HOSTSFILE_PATH */
    deny = fopen(HOSTSFILE_PATH, "r+");
    if (deny == NULL) {
        sshguard_log(LOG_ERR, "Could not initialize " HOSTSFILE_PATH " for use by sshguard: %s", strerror(errno));
        return FWALL_ERR;
    }

    tmp = make_temporary_conffile();
    if (tmp == NULL) {
        sshguard_log(LOG_ERR, "Could not create temporary file %s!", tempflname);
        fclose(deny);
        return FWALL_ERR;
    }
    fprintf(tmp, "%s%s", HOSTS_SSHGUARD_PREFIX, HOSTS_SSHGUARD_SUFFIX);

    /* copy the original content of HOSTSFILE_PATH into tmp */
    while (fgets(buf, HOSTS_MAXCMDLEN, deny) != NULL) {
        fprintf(tmp, "%s", buf);
    }
    
    fclose(tmp);
    fclose(deny);

    /* install temporary conf file into main file */
    if (install_temporary_conffile() != FWALL_OK)
        return FWALL_ERR;

    list_init(&hosts_blockedaddrs);
    list_attributes_copy(&hosts_blockedaddrs, addr_service_meter, 1);
    list_attributes_comparator(&hosts_blockedaddrs, addr_service_comparator);

    return FWALL_OK;
}
コード例 #9
0
ファイル: ext.c プロジェクト: dogbert2/simclist
int main() {
    list_t l;
    int i;

    srandom(time(NULL));

    list_init(&l);
    list_attributes_copy(&l, szelem, 1);

    for (i = 0; i < NELS; i++) {
        list_append(&l, &i);
    }

    for (i = 0; i < 1000; i++) {
        list_get_at(&l, random()%NELS);
    }

    return 0;
}
コード例 #10
0
ファイル: ldconv.c プロジェクト: Beliaar/DCPUToolchain
list_t* list_convert(struct lprov_entry* first)
{
	struct lconv_entry* entry;
	list_t* list = malloc(sizeof(list_t));
	list_init(list);
	list_attributes_copy(list, &lconv_entry_meter, 1);
	list_attributes_comparator(list, &lconv_entry_comparator);
	list_attributes_seeker(list, &lconv_entry_seeker);
	while (first != NULL)
	{
		entry = malloc(sizeof(struct lconv_entry));
		entry->address = first->address;
		entry->bin = NULL;
		entry->label = bfromcstr(first->label);
		list_append(list, entry);
		free(entry);
		first = first->next;
	}
	return list;
}
コード例 #11
0
int logsuck_init() {
    list_init(& sources_list);
    list_attributes_copy(& sources_list, list_meter_sourceentry, 1);

#if defined(HAVE_KQUEUE)
    /* will need file descriptor seeker to look up source items from fds */
    list_attributes_seeker(& sources_list, list_seeker_filedescriptor);
#endif

#if defined(HAVE_KQUEUE)
    /* initialize kqueue */
    if ((kq = kqueue()) == -1) {
        sshguard_log(LOG_CRIT, "Unable to create kqueue! %s.", strerror(errno));
        return -1;
    }
    /* re-test sources every this interval */
    kev_timeout.tv_sec = 1;
    kev_timeout.tv_nsec = 500 * 1000 * 1000;
#endif

    return 0;
}
コード例 #12
0
list_t *blacklist_load(const char *filename) {
    char blacklist_line[BL_MAXBUF];
    unsigned int linecnt;

    assert(blacklist_file == NULL && blacklist == NULL);
    blacklist_file = fopen(filename, "a+");
    if (blacklist_file == NULL) {
        return NULL;
    }

    blacklist = (list_t *)malloc(sizeof(list_t));
    list_init(blacklist);
    list_attributes_copy(blacklist, attacker_el_meter, 1);
    rewind(blacklist_file);

    /* loading content of the file in the blacklist */
    for (linecnt = 1; fgets(blacklist_line, BL_MAXBUF, blacklist_file) != NULL; ++linecnt) {
        attacker_t newattacker;

        /* discard empty lines and lines starting with a white-space or # */
        if (isspace(blacklist_line[0]) || blacklist_line[0] == '#') {
            while (blacklist_line[strlen(blacklist_line)-1] != '\n') {
                /* consume until end of line */
                if (fgets(blacklist_line, BL_MAXBUF, blacklist_file) == NULL) return blacklist;
            }
            continue;
        }

        long long blacklist_time;
        int service_no;
        if (sscanf(blacklist_line, "%lld|%d|%d|%" stringify(ADDRLEN) "s",
                    &blacklist_time, &service_no,
                    &newattacker.attack.address.kind,
                    newattacker.attack.address.value) != 4) {
            sshguard_log(LOG_NOTICE,
                    "blacklist: ignoring malformed line %d", linecnt);
            continue;
        }
        newattacker.whenlast = (time_t)blacklist_time;
        newattacker.attack.service = (enum service)service_no;

        if (newattacker.attack.address.kind != ADDRKIND_IPv4 &&
                newattacker.attack.address.kind != ADDRKIND_IPv6) {
            /* unknown address type */
            sshguard_log(LOG_NOTICE,
                    "blacklist: unknown address type on line %d", linecnt);
            continue;
        }

        /* initialization of other default information */
        newattacker.attack.dangerousness = 1;
        newattacker.whenfirst = 0;
        newattacker.pardontime = 0;
        newattacker.numhits = 1;
        newattacker.cumulated_danger = 1;

        /* add new element to the blacklist */
        list_append(blacklist, & newattacker);
    }

    atexit(blacklist_close);
    return blacklist;
}
コード例 #13
0
ファイル: sshguard_procauth.c プロジェクト: spthaolt/sshguard
void procauth_init() {
    list_init(&proclist);
    list_attributes_copy(&proclist, procpid_meter, 1);
}
コード例 #14
0
ファイル: tier1_testb.c プロジェクト: dogbert2/simclist
/* implementation of Test B */
void testb_check(size_t const n) {
  list_t lists[NUM_OF_LISTS];
  int *added_value;
  int values[NUM_OF_LISTS][n];
  int ret, i, j;
  int value;

  // --------------
  // INITIALIZATION
  // of the lists and check for correctness
  // --------------
  for(i=0; i<NUM_OF_LISTS; ++i) {
    ret = list_init(&lists[i]);
    assert(ret == 0 && "list initialization fails");
  }

  // for achieving the insertion of new elements creating new
  // memory areas, instead of calling malloc(), we set the
  // list_attribute_copy and check the correctness.
  for(i=0; i<NUM_OF_LISTS; ++i) {
    ret = list_attributes_copy(&lists[i], int_size, 1);
    assert(ret == 0 && "setting attribute copy fails");
  }

  // ---------
  // INSERTION
  // both successful and unsuccessful
  // ---------
  for(i=0; i<NUM_OF_LISTS; ++i) {
    // successful insertion in empty list
    randomi(&value);
    ret = list_insert_at(&lists[i], &value, 0);
    assert(ret > 0 && "element insertion fails");
    assert(list_size(&lists[i]) == 1 && "list size is wrong");
    // checking the inserted value
    added_value = (int *)list_get_at(&lists[i], 0);
    assert(added_value != NULL && "retrieving element fails");
    assert(value == *added_value && "retrieved value has wrong value");
    // successful deletion 
    ret = list_delete_at(&lists[i], 0);
    assert(ret == 0 && "delete element fails");
    assert(list_size(&lists[i]) == 0 && "list size is wrong");
  }

  // Failure of insertion on multiple elements.
  // The insertion is done at indexes that are not
  // reachable for the list because it's empty
  for(i=0; i<NUM_OF_LISTS; ++i) {
    randomi(&value);
    for(j=1; j<4; ++j) {
      ret = list_insert_at(&lists[i], &value, j);
      assert(ret < 0 && "element insertion failure fails");
      assert(list_size(&lists[i]) == 0 && "list size is wrong");

      ret = list_delete_at(&lists[i], j);
      assert(ret < 0 && "element deletion failure fails");
      assert(list_size(&lists[i]) == 0 && "list size is wrong");
    }
  }

  // ---------
  // PREAPPEND
  // ---------

  // append the second half of the lists
  for(i=0; i<NUM_OF_LISTS; ++i) {
    for(j=0; j<(n/2); ++j) {
      randomi(&value);
      ret = list_prepend(&lists[i], &value);
      assert(ret == 1 && "element prepend fails");
      // checking also the value appended
      added_value = (int *)list_get_at(&lists[i], 0);
      assert(added_value != NULL && "retrieving element fails");
      assert(value == *added_value && "retrieved value has wrong value");
      // store the max for each list
      values[i][(n/2-1)-j] = value;
    }

    assert(list_size(&lists[i]) == n/2 && "list size is wrong");
  }

  // ------
  // APPEND
  // ------

  // append the first half of the lists
  for(i=0; i<NUM_OF_LISTS; ++i) {
    for(j=(n/2); j<n; ++j) {
      randomi(&value);
      ret = list_append(&lists[i], &value);
      assert(ret == 1 && "element append fails");
      // checking also the value appended
      added_value = (int *)list_get_at(&lists[i], j);
      assert(added_value != NULL && "retrieving element fails");
      assert(value == *added_value && "retrieved value has wrong value");
      // store the value in the matrix
      values[i][j] = value;
    }

    assert(list_size(&lists[i]) == n && "list size is wrong");
  }

  // check the values inserted in the lists
  for(i=0; i<NUM_OF_LISTS; ++i) {
    for(j=0; j<n; ++j) {
      assert(values[i][j] == *(int *)list_get_at(&lists[i], j)
             && "retrieved value has wrong value");
    }
  }

  // -----
  // CLEAR
  // -----

  // check the correctness of the clear function execution
  // and check also the length of the cleared list
  for(i=0; i<NUM_OF_LISTS; ++i) {
      unsigned int isize;
    isize = list_size(&lists[i]);

    ret = list_clear(&lists[i]);
    assert(ret == (int)isize && "clearing list fails");

    ret = list_size(&lists[i]);
    assert(ret == 0 && "list size is wrong");
  }

  // -------
  // DESTROY
  // -------

  // destroy both lists
  for(i=0; i<NUM_OF_LISTS; ++i)
    list_destroy(&lists[i]);
}
コード例 #15
0
ファイル: main.c プロジェクト: macaddino/Networks-Projects
int main(int argc, char *argv[])
{
  time_t current_time;
  char * createdDate;
  int opt;
  char *port = "6667", *passwd = NULL;


  while ((opt = getopt(argc, argv, "p:o:h")) != -1)
    switch (opt)
    {
      case 'p':
        port = strdup(optarg);
        break;
      case 'o':
        passwd = strdup(optarg);
        break;
      default:
        printf("ERROR: Unknown option -%c\n", opt);
        exit(-1);
    }

  if (!passwd)
  {
    fprintf(stderr, "ERROR: You must specify an operator password\n");
    exit(-1);
  }
  num_pthreads = 0;
  int serverSocket;
  int clientSocket;
  pthread_t worker_thread;
  struct sockaddr_in serverAddr, clientAddr;
  int yes = 1; 
  socklen_t sinSize = sizeof(struct sockaddr_in);

  memset(&serverAddr, 0, sizeof(serverAddr));
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(atoi(port));
  serverAddr.sin_addr.s_addr = INADDR_ANY;

  // store server data
  serverInfo * servData;
  servData = (serverInfo *) malloc(sizeof(serverInfo));
  memset(servData, 0, sizeof(servData));
  struct hostent *heServ;
  char hostname[1024];
  hostname[1023] = '\0';
  gethostname(hostname, 1023);
  heServ = gethostbyname(hostname);
  memcpy(servData->passwd, passwd, strlen(passwd));
  memcpy(servData->serverHost, heServ->h_name, strlen(heServ->h_name));
  char serverVersion[] = "version2";
  memcpy(servData->serverVersion, serverVersion, strlen(serverVersion));
  current_time = time(NULL);
  createdDate = ctime(&current_time);
  memcpy(servData->createdDate, createdDate, strlen(createdDate));
  char userModes[] = "ao";
  memcpy(servData->userModes, userModes, strlen(userModes));
  char chanModes[] = "mtov";
  memcpy(servData->chanModes, chanModes, strlen(chanModes));

  serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
  bind(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
  listen(serverSocket, 5);

  // initialize global list of users
  list_t * userList = (list_t *) malloc(sizeof(list_t));
  list_init(userList);
  list_attributes_copy(userList, user_info_size, 1);
  list_attributes_comparator(userList, nick_comparator);
  list_attributes_seeker(userList, (element_seeker) seeker);

  // initialize global list of channels
  list_t * chanList = (list_t *) malloc(sizeof(list_t));
  list_init(chanList);
  list_attributes_copy(chanList, chan_info_size, 1);
  list_attributes_comparator(chanList, chan_comparator);
  list_attributes_seeker(chanList, (element_seeker) chan_seeker);

  // initialize list of users not in a channel
/*  channelData * newChannel;
  newChannel = (channelData *) malloc(sizeof(channelData));
  memcpy(newChannel->name, "*\0", strlen("*\0"));
  pthread_mutex_init(&newChannel->chanUserLock, NULL);
  // remember to delete mutex upon channel deletion
  newChannel->userList = (list_t *) malloc(sizeof(list_t));
  list_init(newChannel->userList);
  list_attributes_copy(newChannel->userList, user_info_size, 1);
  list_attributes_comparator(newChannel->userList, nick_comparator);
  list_attributes_seeker(newChannel->userList, (element_seeker) seeker);
  list_append(chanList, newChannel);
  list_sort(chanList, 1);
*/
  struct workerArgs *wa;

  pthread_mutex_init(&lock, NULL);
  pthread_mutex_init(&chanLock, NULL);

  while(1)
  {
    clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddr, &sinSize);
    setsockopt(clientSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
    // retrieve client hostname
    struct hostent *he;
    struct in_addr ipv4addr;
    char *IP = inet_ntoa(clientAddr.sin_addr);
    inet_pton(AF_INET, IP, &ipv4addr);
    he = gethostbyaddr(&ipv4addr, sizeof(ipv4addr), AF_INET);
    char *clientHost = he->h_name;
    // pass on arguments to worker thread
    wa = malloc(sizeof(struct workerArgs));
    wa->socket = clientSocket;
    wa->clientHost = clientHost;
    wa->userList = userList;
    wa->chanList = chanList;
    wa->servData = servData;
    if(pthread_create(&worker_thread, NULL, run_client, wa) != 0)
    {
      perror("Could not create a worker thread");
      free(wa);
      close(clientSocket);
      pthread_exit(NULL);
    }
  }
  close(serverSocket);
  pthread_mutex_destroy(&lock);
  pthread_mutex_destroy(&chanLock);
  return 0;
}
コード例 #16
0
ファイル: dbglua.c プロジェクト: jathd/DCPUToolchain
void dbg_lua_init()
{
    DIR* dir;
    struct dirent* entry;
    struct lua_debugst* dsm;
    bstring name = NULL;
    bstring modpath = NULL;
    bstring ext = bfromcstr(".lua");

    // Initialize lists.
    list_init(&modules);
    list_attributes_copy(&modules, &lua_debugst_meter, 1);

    // Get the module path.
    modpath = osutil_getmodulepath();
    if (modpath == NULL)
    {
        bdestroy(ext);
        return;
    }

    // Attempt to open the module directory.
    dir = opendir(modpath->data);
    if (dir == NULL)
    {
        // The directory does not exist, so we don't load
        // any custom debugger modules.
        modules_online = false;
        bdestroy(modpath);
        bdestroy(ext);
        return;
    }

    // Load each file from the hw directory.
    while ((entry = readdir(dir)) != NULL)
    {
        name = bfromcstr(&entry->d_name[0]);

        // Check to see whether it is a lua file.
        if (binstr(name, blength(name) - 4, ext) == BSTR_ERR)
        {
            // Not a Lua file, skip over and
            // then continue.
            bdestroy(name);
            continue;
        }

        // Check to see if it is a normal file.
#if defined(DT_REG)
        if (entry->d_type != DT_REG)
#elif defined(DT_DIR)
        if (entry->d_type == DT_DIR)
#elif defined(DT_UNKNOWN)
        if (entry->d_type == DT_UNKNOWN)
#else
#error Build system must support DT_REG, DT_DIR or DT_UNKNOWN in dirent.h.
#endif
        {
            // Not a file, skip over and then
            // continue.
            bdestroy(name);
            continue;
        }

        // Attempt to load the Lua file.
        printd(LEVEL_VERBOSE, "loading custom debugger module from: %s\n", name->data);
        dsm = dbg_lua_load(name);
        if (dsm != NULL)
            list_append(&modules, dsm);

        // Free data.
        bdestroy(name);
    }

    // Free resources.
    closedir(dir);
}
コード例 #17
0
ファイル: main.c プロジェクト: patflick/DCPUToolchain
bool do_search(CURL* curl, bstring name, bool all)
{
    DIR* dir;
    bool printed;
    CURLcode res;
    FILE* fp;
    list_t installed;
    struct bStream* stream;
    long httpcode = 0;
    bstring buffer, fname, sstr;
    bstring ext = bfromcstr(".lua");
    bstring url = bfromcstr("http://dms.dcputoolcha.in/modules/search?q=");
    bstring modpath = osutil_getmodulepath();
    struct dirent* entry;
    list_init(&installed);
    list_attributes_copy(&installed, list_meter_string, 1);
    list_attributes_comparator(&installed, list_comparator_string);

    // Attempt to open the modules directory.
    dir = opendir(modpath->data);
    if (dir == NULL)
    {
        printd(LEVEL_ERROR, "unable to query local repository.\n");
        return 1;
    }

    // Append the temporary search file name.
    bcatcstr(modpath, "/.search");
    bconcat(url, name);

    // Open the file and do the cURL transfer.
    printd(LEVEL_DEFAULT, "querying module repository...\n");
    fp = fopen(modpath->data, "wb");
    curl_easy_setopt(curl, CURLOPT_URL, url->data);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    res = curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode);
    if (res != 0 || httpcode != 200)
    {
        bdestroy(url);
        bdestroy(name);
        bdestroy(modpath);
        printd(LEVEL_ERROR, "curl failed with error code %i, HTTP error code %i.\n", res, httpcode);
        return 1;
    }
    fclose(fp);

    // Print the local results.
    if (all)
        printd(LEVEL_DEFAULT, "all modules:\n");
    else
        printd(LEVEL_DEFAULT, "search results for %s:\n", name->data);
    while ((entry = readdir(dir)) != NULL)
    {
        fname = bfromcstr(&entry->d_name[0]);
        if (binstr(fname, blength(fname) - 4, ext) == BSTR_ERR)
        {
            bdestroy(fname);
            continue;
        }
        if (binstr(fname, 0, name) == BSTR_ERR)
        {
            bdestroy(fname);
            continue;
        }
        if (entry->d_type != DT_REG)
        {
            bdestroy(fname);
            continue;
        }
        sstr = bmidstr(fname, 0, blength(fname) - 4);
        printd(LEVEL_DEFAULT, "   %s (installed)\n", sstr->data);
        list_append(&installed, sstr->data);
        bdestroy(sstr);
        bdestroy(fname);
    }

    // Print the online results.
    fp = fopen(modpath->data, "r");
    stream = bsopen(&read_data, fp);
    buffer = bfromcstr("");
    printed = false;
    while (bsreadln(buffer, stream, '\n') != BSTR_ERR)
    {
        btrimws(buffer);
        sstr = bmidstr(buffer, 0, blength(buffer) - 4);
        if (!list_contains(&installed, sstr->data))
            printd(LEVEL_DEFAULT, "  %s\n", sstr->data);
        printed = true;
        bdestroy(sstr);
    }
    if (!printed)
        printd(LEVEL_DEFAULT, "   <no online results>\n");
    bsclose(stream);
    fclose(fp);

    // Clean up.
    curl_easy_cleanup(curl);
    return 0;
}
コード例 #18
0
ファイル: main.c プロジェクト: patflick/DCPUToolchain
bool do_install_all(CURL* curl)
{
    // define used variables
    DIR* dir;
    FILE* fp;
    CURLcode res;
    bool printed;
    bool install_status;
    bool something_errored;
    bool if_something_was_installed;
    list_t installed;
    long httpcode = 0;
    struct dirent* entry;
    struct bStream* stream;
    bstring buffer, fname, sstr;
    bstring modpath = osutil_getmodulepath();
    bstring ext = bfromcstr(".lua");
    bstring url = bfromcstr("http://dms.dcputoolcha.in/modules/list");
    list_init(&installed);
    list_attributes_copy(&installed, list_meter_string, 1);
    list_attributes_comparator(&installed, list_comparator_string);

    // Attempt to open the modules directory.
    dir = opendir(modpath->data);
    if (dir == NULL)
    {
        printd(LEVEL_ERROR, "unable to query local repository.\n");
        return 1;
    }

    // add the filename we wish to query to the modules folder path
    bcatcstr(modpath, "/.all_avail");

    // Open the file and do the cURL transfer.
    printd(LEVEL_DEFAULT, "loading a list of all the modules...\n");
    fp = fopen(modpath->data, "wb");
    curl_easy_setopt(curl, CURLOPT_URL, url->data);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    res = curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode);
    if (res != 0 || httpcode != 200)
    {
        bdestroy(url);
        bdestroy(modpath);
        printd(LEVEL_ERROR, "curl failed with error code %i, HTTP error code %i.\n", res, httpcode);
        return 1;
    }
    fclose(fp);


    // create a list of already installed modules
    while ((entry = readdir(dir)) != NULL)
    {
        fname = bfromcstr(&entry->d_name[0]);
        if (binstr(fname, blength(fname) - 4, ext) == BSTR_ERR)
        {
            bdestroy(fname);
            continue;
        }
        if (entry->d_type != DT_REG)
        {
            bdestroy(fname);
            continue;
        }
        sstr = bmidstr(fname, 0, blength(fname) - 4);
        list_append(&installed, sstr->data);
        bdestroy(sstr);
        bdestroy(fname);
    }

    printd(LEVEL_DEFAULT, "\n");

    // Print the names of the modules, and install them through the do_install function
    fp = fopen(modpath->data, "r");
    stream = bsopen(&read_data, fp);
    buffer = bfromcstr("");
    printed = false;
    if_something_was_installed = false;
    something_errored = false;
    while (bsreadln(buffer, stream, '\n') != BSTR_ERR)
    {
        btrimws(buffer);
        sstr = bmidstr(buffer, 0, blength(buffer) - 4);

        // if the module is not already installed
        if (!list_contains(&installed, sstr->data))
        {
            install_status = do_install(curl, bfromcstr(sstr->data));
            if_something_was_installed = true;
            // check whether the installation was successful
            if (install_status != 0)
            {
                printd(LEVEL_DEFAULT, "  %s failed to install.\n", sstr->data);
                something_errored = true;
            }
            printd(LEVEL_DEFAULT, "\n");
        }

        printed = true;
        bdestroy(sstr);
    }
    if (!printed)
        printd(LEVEL_DEFAULT, "  <no modules available>\n");
    if (something_errored)
        printd(LEVEL_DEFAULT, "errors occured\n");
    if (!if_something_was_installed)
        printd(LEVEL_DEFAULT, "no changes were made\n");
    bsclose(stream);
    fclose(fp);

    // Clean up.
    curl_easy_cleanup(curl);

    return 0;
}
コード例 #19
0
ファイル: winscard_svc.c プロジェクト: ZuyingWo/PCSC
/**
 * @brief Creates threads to handle messages received from Clients.
 *
 * @param[in] pdwClientID Connection ID used to reference the Client.
 *
 * @return Error code.
 * @retval SCARD_S_SUCCESS Success.
 * @retval SCARD_F_INTERNAL_ERROR Exceded the maximum number of simultaneous Application Contexts.
 * @retval SCARD_E_NO_MEMORY Error creating the Context Thread.
 */
LONG CreateContextThread(uint32_t *pdwClientID)
{
	int rv;
	int lrv;
	int listSize;
	SCONTEXT * newContext = NULL;
	LONG retval = SCARD_E_NO_MEMORY;

	(void)pthread_mutex_lock(&contextsList_lock);

	listSize = list_size(&contextsList);
	if (listSize >= contextMaxThreadCounter)
	{
		Log2(PCSC_LOG_CRITICAL, "Too many context running: %d", listSize);
		goto out;
	}

	/* Create the context for this thread. */
	newContext = malloc(sizeof(*newContext));
	if (NULL == newContext)
	{
		Log1(PCSC_LOG_CRITICAL, "Could not allocate new context");
		goto out;
	}
	memset(newContext, 0, sizeof(*newContext));

	newContext->dwClientID = *pdwClientID;

	/* Initialise the list of card contexts */
	lrv = list_init(&newContext->cardsList);
	if (lrv < 0)
	{
		Log2(PCSC_LOG_CRITICAL, "list_init failed with return value: %d", lrv);
		goto out;
	}

	/* request to store copies, and provide the metric function */
	list_attributes_copy(&newContext->cardsList, list_meter_int32_t, 1);

	/* Adding a comparator
	 * The stored type is SCARDHANDLE (long) but has only 32 bits
	 * usefull even on a 64-bit CPU since the API between pcscd and
	 * libpcscliter uses "int32_t hCard;"
	 */
	lrv = list_attributes_comparator(&newContext->cardsList,
		list_comparator_int32_t);
	if (lrv != 0)
	{
		Log2(PCSC_LOG_CRITICAL,
			"list_attributes_comparator failed with return value: %d", lrv);
		list_destroy(&newContext->cardsList);
		goto out;
	}

	(void)pthread_mutex_init(&newContext->cardsList_lock, NULL);

	lrv = list_append(&contextsList, newContext);
	if (lrv < 0)
	{
		Log2(PCSC_LOG_CRITICAL, "list_append failed with return value: %d",
			lrv);
		list_destroy(&newContext->cardsList);
		goto out;
	}

	rv = ThreadCreate(&newContext->pthThread, THREAD_ATTR_DETACHED,
		(PCSCLITE_THREAD_FUNCTION( )) ContextThread, (LPVOID) newContext);
	if (rv)
	{
		int lrv2;

		Log2(PCSC_LOG_CRITICAL, "ThreadCreate failed: %s", strerror(rv));
		lrv2 = list_delete(&contextsList, newContext);
		if (lrv2 < 0)
			Log2(PCSC_LOG_CRITICAL, "list_delete failed with error %d", lrv2);
		list_destroy(&newContext->cardsList);
		goto out;
	}

	/* disable any suicide alarm */
	if (AutoExit)
		alarm(0);

	retval = SCARD_S_SUCCESS;

out:
	(void)pthread_mutex_unlock(&contextsList_lock);

	if (retval != SCARD_S_SUCCESS)
	{
		if (newContext)
			free(newContext);
		(void)close(*pdwClientID);
	}

	return retval;
}