Example #1
0
static bool enough_memory_for_hibernation(void) {
        _cleanup_free_ char *active = NULL;
        unsigned long long act = 0;
        size_t size = 0, used = 0;
        int r;

        r = hibernation_partition_size(&size, &used);
        if (r < 0)
                return false;

        r = get_status_field("/proc/meminfo", "\nActive(anon):", &active);
        if (r < 0) {
                log_error_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
                return false;
        }

        r = safe_atollu(active, &act);
        if (r < 0) {
                log_error_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m",
                                active);
                return false;
        }

        r = act <= (size - used) * HIBERNATION_SWAP_THRESHOLD;
        log_debug("Hibernation is %spossible, Active(anon)=%llu kB, size=%zu kB, used=%zu kB, threshold=%.2g%%",
                  r ? "" : "im", act, size, used, 100*HIBERNATION_SWAP_THRESHOLD);

        return r;
}
Example #2
0
/* Get the process status char of the given process (e.g. 'R' for Running, 'T' for stopped, 'Z' for zombie, etc). On
 * failure return '?'. */
char procfs_pid_state(pid_t pid, pid_t tid) {
    char * val = get_status_field(pid, tid, "State");
    
    if (val) {
        return val[0];
        free(val);
    } else
        return '?';
}
Example #3
0
/* Get the value of the given field from a /proc/.../status file and convert it to an unsigned int.
 * Return success flag.*/
static bool get_status_field_uint(pid_t pid, pid_t tid, const char * field, unsigned int * result) {
    char * str;
    long int val;
    int success;
    
    if (!(str = get_status_field(pid, tid, field))) {
        *result = 0;
        return 0;
    }
    
    val = strtol(str, NULL, 0);
    
    success = (val >= 0) && ((unsigned long) val <= UINT_MAX);
    
    if (success)
        *result = (unsigned int) val;
    else {
        error("Value of '%s' of thread %d:%d is out of range.", field, pid, tid);
        *result = 0;
    }
    
    return success;
}
Example #4
0
apps_info_t *get_apps_list(char *argv) {
    apps_info_t *apps_info_list = NULL, **follow_apps_info_list = NULL, *follow_apps_info;
    char *apps_info;
    char *pkg_head, *pkg_tail;
    char info_name[128];
    FILE *fp;
    unsigned long file_size = 0;
    int pid;
    char *cmd[] = {"app_update.sh", NULL};
    char line[128], buf[4096];
    char *tmp_apps_name;
    int got_apps;
    char *STATUS;

    if(!argv || strcmp(argv, APP_OWNER_OTHERS)) {
        // Get the newest version of the installed packages,
        // and information of the non-installed packages from APPS_LIST_ASUS.
        if((fp = fopen(APPS_LIST_ASUS, "r")) == NULL)
            return apps_info_list;

        fseek(fp, 0, SEEK_END);
        file_size = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        if(file_size <= 0) {
            _dprintf("httpd: get the Zero size of the ASUS APP list.\n");
            fclose(fp);
            _eval(cmd, NULL, 0, &pid);
            return apps_info_list;
        }

        memset(line, 0, sizeof(line));
        while(fgets(line, 128, fp) != NULL) {
            if((tmp_apps_name = get_status_field(line, FIELD_PACKAGE)) == NULL)
                continue;

            memset(buf, 0, sizeof(buf));
            pkg_tail = pkg_head = buf;
            do {
                sprintf(pkg_tail, "%s", line);
                pkg_tail += strlen(line);

                memset(line, 0, sizeof(line));
            } while(fgets(line, 128, fp) != NULL && strlen(line) > 1);

            follow_apps_info = apps_info_list;
            got_apps = 0;
            while(follow_apps_info != NULL) {
                if(!strcmp(follow_apps_info->name, tmp_apps_name)) {
                    got_apps = 1;
                    break;
                }

                follow_apps_info = follow_apps_info->next;
            }
            free(tmp_apps_name);

            // Installed package.
            if(got_apps) {
                follow_apps_info->new_version = get_status_field(pkg_head, FIELD_VERSION);
                follow_apps_info->new_optional_utility = get_status_field(pkg_head, FIELD_OPTIONALUTILITY);
                follow_apps_info->new_file_name = get_status_field(pkg_head, FIELD_FILENAME);
                if(follow_apps_info->from_owner != NULL)
                    free(follow_apps_info->from_owner);
                follow_apps_info->from_owner = alloc_string(APP_OWNER_ASUS);
            }
            // Non-installed package.
            else {
                follow_apps_info_list = &apps_info_list;
                while(*follow_apps_info_list != NULL)
                    follow_apps_info_list = &((*follow_apps_info_list)->next);

                *follow_apps_info_list = initial_apps_data();

                (*follow_apps_info_list)->name = get_status_field(pkg_head, FIELD_PACKAGE);
                (*follow_apps_info_list)->new_version = get_status_field(pkg_head, FIELD_VERSION);
                (*follow_apps_info_list)->installed = alloc_string(FIELD_NO);
                (*follow_apps_info_list)->enabled = alloc_string(FIELD_NO);
                (*follow_apps_info_list)->source = get_status_field(pkg_head, FIELD_SOURCE);
                (*follow_apps_info_list)->url = get_status_field(pkg_head, FIELD_URL);
                (*follow_apps_info_list)->description = get_status_field(pkg_head, FIELD_DESCRIPTION);
                (*follow_apps_info_list)->depends = get_status_field(pkg_head, FIELD_DEPENDS);
                (*follow_apps_info_list)->new_optional_utility = get_status_field(pkg_head, FIELD_OPTIONALUTILITY);
                (*follow_apps_info_list)->help_path = get_status_field(pkg_head, FIELD_HELPPATH);
                (*follow_apps_info_list)->new_file_name = get_status_field(pkg_head, FIELD_FILENAME);
                (*follow_apps_info_list)->from_owner = alloc_string(APP_OWNER_ASUS);
            }

            memset(line, 0, sizeof(line));
        }
        fclose(fp);
    }

    if(!argv || strcmp(argv, APP_OWNER_ASUS)) {
        // Get the newest version of the installed packages,
        // and information of the non-installed packages from APPS_LIST_OLEG.
        if((fp = fopen(APPS_LIST_OLEG, "r")) == NULL)
            return apps_info_list;

        fseek(fp, 0, SEEK_END);
        file_size = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        if(file_size <= 0) {
            _dprintf("httpd: get the Zero size of the third-party APP list.\n");
            fclose(fp);
            _eval(cmd, NULL, 0, &pid);
            return apps_info_list;
        }

        memset(line, 0, sizeof(line));
        while(fgets(line, 128, fp) != NULL) {
            if((tmp_apps_name = get_status_field(line, FIELD_PACKAGE)) == NULL)
                continue;

            memset(buf, 0, sizeof(buf));
            pkg_tail = pkg_head = buf;
            do {
                sprintf(pkg_tail, "%s", line);
                pkg_tail += strlen(line);

                memset(line, 0, sizeof(line));
            } while(fgets(line, 128, fp) != NULL && strlen(line) > 1);

            follow_apps_info = apps_info_list;
            got_apps = 0;
            while(follow_apps_info != NULL) {
                if(!strcmp(follow_apps_info->name, tmp_apps_name)) {
                    got_apps = 1;
                    break;
                }

                follow_apps_info = follow_apps_info->next;
            }
            free(tmp_apps_name);

            // Installed package.
            if(got_apps) {
                follow_apps_info->new_version = get_status_field(pkg_head, FIELD_VERSION);
                follow_apps_info->new_optional_utility = get_status_field(pkg_head, FIELD_OPTIONALUTILITY);
                if(follow_apps_info->from_owner != NULL)
                    free(follow_apps_info->from_owner);
                follow_apps_info->from_owner = alloc_string(APP_OWNER_OLEG);
            }
            // Non-installed package.
            else {
                follow_apps_info_list = &apps_info_list;
                while(*follow_apps_info_list != NULL)
                    follow_apps_info_list = &((*follow_apps_info_list)->next);

                *follow_apps_info_list = initial_apps_data();

                (*follow_apps_info_list)->name = get_status_field(pkg_head, FIELD_PACKAGE);
                (*follow_apps_info_list)->new_version = get_status_field(pkg_head, FIELD_VERSION);
                (*follow_apps_info_list)->installed = alloc_string(FIELD_NO);
                (*follow_apps_info_list)->enabled = alloc_string(FIELD_NO);
                (*follow_apps_info_list)->source = get_status_field(pkg_head, FIELD_SOURCE);
                (*follow_apps_info_list)->url = get_status_field(pkg_head, FIELD_URL);
                (*follow_apps_info_list)->description = get_status_field(pkg_head, FIELD_DESCRIPTION);
                (*follow_apps_info_list)->depends = get_status_field(pkg_head, FIELD_DEPENDS);
                (*follow_apps_info_list)->new_optional_utility = get_status_field(pkg_head, FIELD_OPTIONALUTILITY);
                (*follow_apps_info_list)->help_path = get_status_field(pkg_head, FIELD_HELPPATH);
                (*follow_apps_info_list)->new_file_name = get_status_field(pkg_head, FIELD_FILENAME);
                (*follow_apps_info_list)->from_owner = alloc_string(APP_OWNER_OLEG);
            }

            memset(line, 0, sizeof(line));
        }
        fclose(fp);
    }

    // Get the name and version of the installed packages from APPS_STATUS.
    if((fp = fopen(APPS_STATUS, "r")) == NULL)
        return apps_info_list;

    memset(line, 0, sizeof(line));
    while(fgets(line, 128, fp) != NULL) {
        if((tmp_apps_name = get_status_field(line, FIELD_PACKAGE)) == NULL)
            continue;

        memset(buf, 0, sizeof(buf));
        pkg_tail = pkg_head = buf;
        do {
            sprintf(pkg_tail, "%s", line);
            pkg_tail += strlen(line);

            memset(line, 0, sizeof(line));
        } while(fgets(line, 128, fp) != NULL && strlen(line) > 1);

        if((STATUS = get_status_field(pkg_head, FIELD_STATUS)) == NULL)
            continue;
        if(strstr(STATUS, "not-installed")) {
            free(STATUS);
            continue;
        }
        free(STATUS);

        follow_apps_info = apps_info_list;
        got_apps = 0;
        while(follow_apps_info != NULL) {
            if(!strcmp(follow_apps_info->name, tmp_apps_name)) {
                got_apps = 1;
                break;
            }

            follow_apps_info = follow_apps_info->next;
        }
        free(tmp_apps_name);

        // Installed package.
        if(got_apps) {
            follow_apps_info->version = get_status_field(pkg_head, FIELD_VERSION);
            if(follow_apps_info->installed != NULL)
                free(follow_apps_info->installed);
            follow_apps_info->installed = alloc_string(FIELD_YES);
        }
        // Non-installed package.
        else if(!argv || !strcmp(argv, APP_OWNER_ALL)) {
            follow_apps_info_list = &apps_info_list;
            while(*follow_apps_info_list != NULL)
                follow_apps_info_list = &((*follow_apps_info_list)->next);

            *follow_apps_info_list = initial_apps_data();

            (*follow_apps_info_list)->name = get_status_field(pkg_head, FIELD_PACKAGE);
            (*follow_apps_info_list)->version = get_status_field(pkg_head, FIELD_VERSION);
            (*follow_apps_info_list)->installed = alloc_string(FIELD_YES);
            (*follow_apps_info_list)->from_owner = alloc_string(APP_OWNER_OTHERS);
        }

        memset(line, 0, sizeof(line));
    }
    fclose(fp);

    follow_apps_info = apps_info_list;
    while(follow_apps_info != NULL) {
        // Get the source, description and depends of the installed packages from the control in APPS_INFO.
        memset(info_name, 0, sizeof(info_name));
        sprintf(info_name, "%s/%s.control", APPS_INFO, follow_apps_info->name);
        apps_info = read_whole_file(info_name);
        if(apps_info != NULL) {
            if((tmp_apps_name = get_status_field(apps_info, FIELD_ENABLED)) != NULL) {
                if(follow_apps_info->enabled != NULL)
                    free(follow_apps_info->enabled);
                follow_apps_info->enabled = get_status_field(apps_info, FIELD_ENABLED);
                free(tmp_apps_name);
            }
            follow_apps_info->optional_utility = get_status_field(apps_info, FIELD_OPTIONALUTILITY);

            free(apps_info);
        }

        follow_apps_info = follow_apps_info->next;
    }

    return apps_info_list;
}