extern List as_mysql_get_accts(mysql_conn_t *mysql_conn, uid_t uid, slurmdb_account_cond_t *acct_cond) { char *query = NULL; char *extra = NULL; char *tmp = NULL; List acct_list = NULL; ListIterator itr = NULL; char *object = NULL; int set = 0; int i=0, is_admin=1; MYSQL_RES *result = NULL; MYSQL_ROW row; uint16_t private_data = 0; slurmdb_user_rec_t user; /* if this changes you will need to edit the corresponding enum */ char *acct_req_inx[] = { "name", "description", "organization" }; enum { SLURMDB_REQ_NAME, SLURMDB_REQ_DESC, SLURMDB_REQ_ORG, SLURMDB_REQ_COUNT }; if (check_connection(mysql_conn) != SLURM_SUCCESS) return NULL; memset(&user, 0, sizeof(slurmdb_user_rec_t)); user.uid = uid; private_data = slurm_get_private_data(); if (private_data & PRIVATE_DATA_ACCOUNTS) { if (!(is_admin = is_user_min_admin_level( mysql_conn, uid, SLURMDB_ADMIN_OPERATOR))) { if (!is_user_any_coord(mysql_conn, &user)) { error("Only admins/coordinators " "can look at account usage"); errno = ESLURM_ACCESS_DENIED; return NULL; } } } if (!acct_cond) { xstrcat(extra, "where deleted=0"); goto empty; } if (acct_cond->with_deleted) xstrcat(extra, "where (deleted=0 || deleted=1)"); else xstrcat(extra, "where deleted=0"); if (acct_cond->assoc_cond && acct_cond->assoc_cond->acct_list && list_count(acct_cond->assoc_cond->acct_list)) { set = 0; xstrcat(extra, " && ("); itr = list_iterator_create(acct_cond->assoc_cond->acct_list); while ((object = list_next(itr))) { if (set) xstrcat(extra, " || "); xstrfmtcat(extra, "name='%s'", object); set = 1; } list_iterator_destroy(itr); xstrcat(extra, ")"); } if (acct_cond->description_list && list_count(acct_cond->description_list)) { set = 0; xstrcat(extra, " && ("); itr = list_iterator_create(acct_cond->description_list); while ((object = list_next(itr))) { if (set) xstrcat(extra, " || "); xstrfmtcat(extra, "description='%s'", object); set = 1; } list_iterator_destroy(itr); xstrcat(extra, ")"); } if (acct_cond->organization_list && list_count(acct_cond->organization_list)) { set = 0; xstrcat(extra, " && ("); itr = list_iterator_create(acct_cond->organization_list); while ((object = list_next(itr))) { if (set) xstrcat(extra, " || "); xstrfmtcat(extra, "organization='%s'", object); set = 1; } list_iterator_destroy(itr); xstrcat(extra, ")"); } empty: xfree(tmp); xstrfmtcat(tmp, "%s", acct_req_inx[i]); for(i=1; i<SLURMDB_REQ_COUNT; i++) { xstrfmtcat(tmp, ", %s", acct_req_inx[i]); } /* This is here to make sure we are looking at only this user * if this flag is set. We also include any accounts they may be * coordinator of. */ if (!is_admin && (private_data & PRIVATE_DATA_ACCOUNTS)) { slurmdb_coord_rec_t *coord = NULL; set = 0; itr = list_iterator_create(user.coord_accts); while ((coord = list_next(itr))) { if (set) { xstrfmtcat(extra, " || name='%s'", coord->name); } else { set = 1; xstrfmtcat(extra, " && (name='%s'", coord->name); } } list_iterator_destroy(itr); if (set) xstrcat(extra,")"); } query = xstrdup_printf("select %s from %s %s", tmp, acct_table, extra); xfree(tmp); xfree(extra); if (debug_flags & DEBUG_FLAG_DB_ASSOC) DB_DEBUG(mysql_conn->conn, "query\n%s", query); if (!(result = mysql_db_query_ret( mysql_conn, query, 0))) { xfree(query); return NULL; } xfree(query); acct_list = list_create(slurmdb_destroy_account_rec); if (acct_cond && acct_cond->assoc_cond && acct_cond->with_assocs) { /* We are going to be freeing the inners of this list in the acct->name so we don't free it here */ if (acct_cond->assoc_cond->acct_list) list_destroy(acct_cond->assoc_cond->acct_list); acct_cond->assoc_cond->acct_list = list_create(NULL); acct_cond->assoc_cond->with_deleted = acct_cond->with_deleted; } while ((row = mysql_fetch_row(result))) { slurmdb_account_rec_t *acct = xmalloc(sizeof(slurmdb_account_rec_t)); list_append(acct_list, acct); acct->name = xstrdup(row[SLURMDB_REQ_NAME]); acct->description = xstrdup(row[SLURMDB_REQ_DESC]); acct->organization = xstrdup(row[SLURMDB_REQ_ORG]); if (acct_cond && acct_cond->with_coords) { _get_account_coords(mysql_conn, acct); } if (acct_cond && acct_cond->with_assocs) { if (!acct_cond->assoc_cond) { acct_cond->assoc_cond = xmalloc( sizeof(slurmdb_association_cond_t)); } list_append(acct_cond->assoc_cond->acct_list, acct->name); } } mysql_free_result(result); if (acct_cond && acct_cond->with_assocs && acct_cond->assoc_cond && list_count(acct_cond->assoc_cond->acct_list)) { ListIterator assoc_itr = NULL; slurmdb_account_rec_t *acct = NULL; slurmdb_association_rec_t *assoc = NULL; List assoc_list = as_mysql_get_assocs( mysql_conn, uid, acct_cond->assoc_cond); if (!assoc_list) { error("no associations"); return acct_list; } itr = list_iterator_create(acct_list); assoc_itr = list_iterator_create(assoc_list); while ((acct = list_next(itr))) { while ((assoc = list_next(assoc_itr))) { if (strcmp(assoc->acct, acct->name)) continue; if (!acct->assoc_list) acct->assoc_list = list_create( slurmdb_destroy_association_rec); list_append(acct->assoc_list, assoc); list_remove(assoc_itr); } list_iterator_reset(assoc_itr); if (!acct->assoc_list) list_remove(itr); } list_iterator_destroy(itr); list_iterator_destroy(assoc_itr); list_destroy(assoc_list); } return acct_list; }
static List _get_other_user_names_to_mod(mysql_conn_t *mysql_conn, uint32_t uid, slurmdb_user_cond_t *user_cond) { List tmp_list = NULL; List ret_list = NULL; ListIterator itr = NULL; slurmdb_association_cond_t assoc_cond; slurmdb_wckey_cond_t wckey_cond; if (!user_cond->def_acct_list || !list_count(user_cond->def_acct_list)) goto no_assocs; /* We have to use a different association_cond here because other things could be set here we don't care about in the user's. (So to be safe just move over the info we care about) */ memset(&assoc_cond, 0, sizeof(slurmdb_association_cond_t)); assoc_cond.acct_list = user_cond->def_acct_list; if (user_cond->assoc_cond) { if (user_cond->assoc_cond->cluster_list) assoc_cond.cluster_list = user_cond->assoc_cond->cluster_list; if (user_cond->assoc_cond->user_list) assoc_cond.user_list = user_cond->assoc_cond->user_list; } assoc_cond.only_defs = 1; tmp_list = as_mysql_get_assocs(mysql_conn, uid, &assoc_cond); if (tmp_list) { slurmdb_association_rec_t *object = NULL; itr = list_iterator_create(tmp_list); while ((object = list_next(itr))) { if (!ret_list) ret_list = list_create(slurm_destroy_char); slurm_addto_char_list(ret_list, object->user); } list_iterator_destroy(itr); list_destroy(tmp_list); tmp_list = NULL; } no_assocs: if (!user_cond->def_wckey_list || !list_count(user_cond->def_wckey_list)) goto no_wckeys; memset(&wckey_cond, 0, sizeof(slurmdb_wckey_cond_t)); if (user_cond->assoc_cond) { if (user_cond->assoc_cond->cluster_list) wckey_cond.cluster_list = user_cond->assoc_cond->cluster_list; if (user_cond->assoc_cond->user_list) wckey_cond.user_list = user_cond->assoc_cond->user_list; } wckey_cond.name_list = user_cond->def_wckey_list; tmp_list = as_mysql_get_wckeys(mysql_conn, uid, &wckey_cond); if (tmp_list) { slurmdb_wckey_rec_t *object = NULL; itr = list_iterator_create(tmp_list); while ((object = list_next(itr))) { if (!ret_list) ret_list = list_create(slurm_destroy_char); slurm_addto_char_list(ret_list, object->user); } list_iterator_destroy(itr); list_destroy(tmp_list); tmp_list = NULL; } no_wckeys: return ret_list; }
extern List as_mysql_get_clusters(mysql_conn_t *mysql_conn, uid_t uid, slurmdb_cluster_cond_t *cluster_cond) { char *query = NULL; char *extra = NULL; char *tmp = NULL; List cluster_list = NULL; ListIterator itr = NULL; int i=0; MYSQL_RES *result = NULL; MYSQL_ROW row; slurmdb_assoc_cond_t assoc_cond; ListIterator assoc_itr = NULL; slurmdb_cluster_rec_t *cluster = NULL; slurmdb_assoc_rec_t *assoc = NULL; List assoc_list = NULL; /* if this changes you will need to edit the corresponding enum */ char *cluster_req_inx[] = { "name", "classification", "control_host", "control_port", "rpc_version", "dimensions", "flags", "plugin_id_select" }; enum { CLUSTER_REQ_NAME, CLUSTER_REQ_CLASS, CLUSTER_REQ_CH, CLUSTER_REQ_CP, CLUSTER_REQ_VERSION, CLUSTER_REQ_DIMS, CLUSTER_REQ_FLAGS, CLUSTER_REQ_PI_SELECT, CLUSTER_REQ_COUNT }; if (check_connection(mysql_conn) != SLURM_SUCCESS) return NULL; if (!cluster_cond) { xstrcat(extra, " where deleted=0"); goto empty; } _setup_cluster_cond_limits(cluster_cond, &extra); empty: xfree(tmp); i=0; xstrfmtcat(tmp, "%s", cluster_req_inx[i]); for(i=1; i<CLUSTER_REQ_COUNT; i++) { xstrfmtcat(tmp, ", %s", cluster_req_inx[i]); } query = xstrdup_printf("select %s from %s%s", tmp, cluster_table, extra); xfree(tmp); xfree(extra); if (debug_flags & DEBUG_FLAG_DB_ASSOC) DB_DEBUG(mysql_conn->conn, "query\n%s", query); if (!(result = mysql_db_query_ret( mysql_conn, query, 0))) { xfree(query); return NULL; } xfree(query); cluster_list = list_create(slurmdb_destroy_cluster_rec); memset(&assoc_cond, 0, sizeof(slurmdb_assoc_cond_t)); if (cluster_cond) { /* I don't think we want the with_usage flag here. * We do need the with_deleted though. */ //assoc_cond.with_usage = cluster_cond->with_usage; assoc_cond.with_deleted = cluster_cond->with_deleted; } assoc_cond.cluster_list = list_create(NULL); while ((row = mysql_fetch_row(result))) { MYSQL_RES *result2 = NULL; MYSQL_ROW row2; cluster = xmalloc(sizeof(slurmdb_cluster_rec_t)); list_append(cluster_list, cluster); cluster->name = xstrdup(row[CLUSTER_REQ_NAME]); list_append(assoc_cond.cluster_list, cluster->name); cluster->classification = slurm_atoul(row[CLUSTER_REQ_CLASS]); cluster->control_host = xstrdup(row[CLUSTER_REQ_CH]); cluster->control_port = slurm_atoul(row[CLUSTER_REQ_CP]); cluster->rpc_version = slurm_atoul(row[CLUSTER_REQ_VERSION]); cluster->dimensions = slurm_atoul(row[CLUSTER_REQ_DIMS]); cluster->flags = slurm_atoul(row[CLUSTER_REQ_FLAGS]); cluster->plugin_id_select = slurm_atoul(row[CLUSTER_REQ_PI_SELECT]); query = xstrdup_printf( "select tres, cluster_nodes from " "\"%s_%s\" where time_end=0 and node_name='' limit 1", cluster->name, event_table); if (debug_flags & DEBUG_FLAG_DB_TRES) DB_DEBUG(mysql_conn->conn, "query\n%s", query); if (!(result2 = mysql_db_query_ret(mysql_conn, query, 0))) { xfree(query); continue; } xfree(query); if ((row2 = mysql_fetch_row(result2))) { cluster->tres_str = xstrdup(row2[0]); if (row2[1] && row2[1][0]) cluster->nodes = xstrdup(row2[1]); } mysql_free_result(result2); /* get the usage if requested */ if (cluster_cond && cluster_cond->with_usage) { as_mysql_get_usage( mysql_conn, uid, cluster, DBD_GET_CLUSTER_USAGE, cluster_cond->usage_start, cluster_cond->usage_end); } } mysql_free_result(result); if (!list_count(assoc_cond.cluster_list)) { FREE_NULL_LIST(assoc_cond.cluster_list); return cluster_list; } assoc_cond.acct_list = list_create(NULL); list_append(assoc_cond.acct_list, "root"); assoc_cond.user_list = list_create(NULL); list_append(assoc_cond.user_list, ""); assoc_list = as_mysql_get_assocs(mysql_conn, uid, &assoc_cond); FREE_NULL_LIST(assoc_cond.cluster_list); FREE_NULL_LIST(assoc_cond.acct_list); FREE_NULL_LIST(assoc_cond.user_list); if (!assoc_list) return cluster_list; itr = list_iterator_create(cluster_list); assoc_itr = list_iterator_create(assoc_list); while ((cluster = list_next(itr))) { while ((assoc = list_next(assoc_itr))) { if (strcmp(assoc->cluster, cluster->name)) continue; if (cluster->root_assoc) { debug("This cluster %s already has " "an association.", cluster->name); continue; } cluster->root_assoc = assoc; list_remove(assoc_itr); } list_iterator_reset(assoc_itr); } list_iterator_destroy(itr); list_iterator_destroy(assoc_itr); if (list_count(assoc_list)) error("I have %d left over associations", list_count(assoc_list)); FREE_NULL_LIST(assoc_list); return cluster_list; }
extern List as_mysql_get_users(mysql_conn_t *mysql_conn, uid_t uid, slurmdb_user_cond_t *user_cond) { char *query = NULL; char *extra = NULL; char *tmp = NULL; List user_list = NULL; ListIterator itr = NULL; char *object = NULL; int set = 0; int i=0, is_admin=1; MYSQL_RES *result = NULL; MYSQL_ROW row; uint16_t private_data = 0; slurmdb_user_rec_t user; /* if this changes you will need to edit the corresponding enum */ char *user_req_inx[] = { "name", "admin_level" }; enum { USER_REQ_NAME, USER_REQ_AL, USER_REQ_COUNT }; if (check_connection(mysql_conn) != SLURM_SUCCESS) return NULL; memset(&user, 0, sizeof(slurmdb_user_rec_t)); user.uid = uid; private_data = slurm_get_private_data(); if (private_data & PRIVATE_DATA_USERS) { if (!(is_admin = is_user_min_admin_level( mysql_conn, uid, SLURMDB_ADMIN_OPERATOR))) { assoc_mgr_fill_in_user( mysql_conn, &user, 1, NULL); } if (!is_admin && !user.name) { debug("User %u has no assocations, and is not admin, " "so not returning any users.", user.uid); return NULL; } } if (!user_cond) { xstrcat(extra, "where deleted=0"); set = 1; goto empty; } if (user_cond->with_deleted) xstrcat(extra, "where (deleted=0 || deleted=1)"); else xstrcat(extra, "where deleted=0"); user_list = _get_other_user_names_to_mod(mysql_conn, uid, user_cond); if (user_list) { if (!user_cond->assoc_cond) user_cond->assoc_cond = xmalloc(sizeof(slurmdb_association_rec_t)); if (!user_cond->assoc_cond->user_list) user_cond->assoc_cond->user_list = user_list; else { list_transfer(user_cond->assoc_cond->user_list, user_list); list_destroy(user_list); } user_list = NULL; } else if ((user_cond->def_acct_list && list_count(user_cond->def_acct_list)) || (user_cond->def_wckey_list && list_count(user_cond->def_wckey_list))) return NULL; if (user_cond->assoc_cond && user_cond->assoc_cond->user_list && list_count(user_cond->assoc_cond->user_list)) { set = 0; xstrcat(extra, " && ("); itr = list_iterator_create(user_cond->assoc_cond->user_list); while ((object = list_next(itr))) { if (set) xstrcat(extra, " || "); xstrfmtcat(extra, "name='%s'", object); set = 1; } list_iterator_destroy(itr); xstrcat(extra, ")"); } if (user_cond->admin_level != SLURMDB_ADMIN_NOTSET) { xstrfmtcat(extra, " && admin_level=%u", user_cond->admin_level); set = 1; } empty: /* This is here to make sure we are looking at only this user * if this flag is set. */ if (!is_admin && (private_data & PRIVATE_DATA_USERS)) { xstrfmtcat(extra, " && name='%s'", user.name); } xfree(tmp); xstrfmtcat(tmp, "%s", user_req_inx[0]); for(i=1; i<USER_REQ_COUNT; i++) { xstrfmtcat(tmp, ", %s", user_req_inx[i]); } query = xstrdup_printf("select %s from %s %s", tmp, user_table, extra); xfree(tmp); xfree(extra); debug3("%d(%s:%d) query\n%s", mysql_conn->conn, THIS_FILE, __LINE__, query); if (!(result = mysql_db_query_ret( mysql_conn, query, 0))) { xfree(query); return NULL; } xfree(query); user_list = list_create(slurmdb_destroy_user_rec); while ((row = mysql_fetch_row(result))) { slurmdb_user_rec_t *user = xmalloc(sizeof(slurmdb_user_rec_t)); list_append(user_list, user); user->name = xstrdup(row[USER_REQ_NAME]); user->admin_level = slurm_atoul(row[USER_REQ_AL]); if (user_cond && user_cond->with_coords) _get_user_coords(mysql_conn, user); } mysql_free_result(result); if (user_cond && (user_cond->with_assocs || (user_cond->assoc_cond && user_cond->assoc_cond->only_defs))) { ListIterator assoc_itr = NULL; slurmdb_user_rec_t *user = NULL; slurmdb_association_rec_t *assoc = NULL; List assoc_list = NULL; /* Make sure we don't get any non-user associations * this is done by at least having a user_list * defined */ if (!user_cond->assoc_cond) user_cond->assoc_cond = xmalloc(sizeof(slurmdb_association_cond_t)); if (!user_cond->assoc_cond->user_list) user_cond->assoc_cond->user_list = list_create(NULL); user_cond->assoc_cond->with_deleted = user_cond->with_deleted; assoc_list = as_mysql_get_assocs( mysql_conn, uid, user_cond->assoc_cond); if (!assoc_list) { error("no associations"); goto get_wckeys; } itr = list_iterator_create(user_list); assoc_itr = list_iterator_create(assoc_list); while ((user = list_next(itr))) { while ((assoc = list_next(assoc_itr))) { if (strcmp(assoc->user, user->name)) continue; /* Set up the default. This is needed * for older versions primarily that * don't have the notion of default * account per cluster. */ if (!user->default_acct && (assoc->is_def == 1)) user->default_acct = xstrdup(assoc->acct); if (!user_cond->with_assocs) { /* We just got the default so no reason to hang around if we aren't getting the associations. */ if (user->default_acct) break; else continue; } if (!user->assoc_list) user->assoc_list = list_create( slurmdb_destroy_association_rec); list_append(user->assoc_list, assoc); list_remove(assoc_itr); } list_iterator_reset(assoc_itr); } list_iterator_destroy(itr); list_iterator_destroy(assoc_itr); list_destroy(assoc_list); } get_wckeys: if (user_cond && (user_cond->with_wckeys || (user_cond->assoc_cond && user_cond->assoc_cond->only_defs))) { ListIterator wckey_itr = NULL; slurmdb_user_rec_t *user = NULL; slurmdb_wckey_rec_t *wckey = NULL; List wckey_list = NULL; slurmdb_wckey_cond_t wckey_cond; memset(&wckey_cond, 0, sizeof(slurmdb_wckey_cond_t)); if (user_cond->assoc_cond) { wckey_cond.user_list = user_cond->assoc_cond->user_list; wckey_cond.cluster_list = user_cond->assoc_cond->cluster_list; wckey_cond.only_defs = user_cond->assoc_cond->only_defs; } wckey_list = as_mysql_get_wckeys(mysql_conn, uid, &wckey_cond); if (!wckey_list) return user_list; itr = list_iterator_create(user_list); wckey_itr = list_iterator_create(wckey_list); while ((user = list_next(itr))) { while ((wckey = list_next(wckey_itr))) { if (strcmp(wckey->user, user->name)) continue; /* Set up the default. This is needed * for older versions primarily that * don't have the notion of default * wckey per cluster. */ if (!user->default_wckey && (wckey->is_def == 1)) user->default_wckey = xstrdup(wckey->name); /* We just got the default so no reason to hang around if we aren't getting the wckeys. */ if (!user_cond->with_wckeys) { /* We just got the default so no reason to hang around if we aren't getting the wckeys. */ if (user->default_wckey) break; else continue; } if (!user->wckey_list) user->wckey_list = list_create( slurmdb_destroy_wckey_rec); list_append(user->wckey_list, wckey); list_remove(wckey_itr); } list_iterator_reset(wckey_itr); /* If a user doesn't have a default wckey (they might not of had track_wckeys on), set it now. */ if (!user->default_wckey) user->default_wckey = xstrdup(""); } list_iterator_destroy(itr); list_iterator_destroy(wckey_itr); list_destroy(wckey_list); } return user_list; }