From: Jarkko Sakkinen Date: Thu, 18 Nov 2010 03:06:32 +0000 (-0800) Subject: Removed flags parameter from smack_write_rules_to_file() and X-Git-Tag: accepted/trunk/20130107.204027~195 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cdf71cc7da2abfdf84ed6f0b9668c9b71b492a7e;p=platform%2Fupstream%2Fsmack.git Removed flags parameter from smack_write_rules_to_file() and separated new function smack_write_rules_to_kernel(). --- diff --git a/src/smack.h b/src/smack.h index f931459..9e324a8 100644 --- a/src/smack.h +++ b/src/smack.h @@ -43,11 +43,6 @@ typedef struct smack_rules *smack_rules_t; typedef struct smack_users *smack_users_t; /*! - * Flags for rules IO. - */ -#define SMACK_RULES_KERNEL 1 - -/*! * Flags for extended attributes. */ #define SMACK_XATTR_SYMLINK 1 @@ -93,8 +88,17 @@ extern int smack_read_rules_from_file(smack_rules_t handle, * @param flags write flags * @return 0 on success */ -extern int smack_write_rules_to_file(smack_rules_t handle, const char *path, - int flags); +extern int smack_write_rules_to_file(smack_rules_t handle, const char *path); + +/*! + * Write rules to SmackFS rules file. + * + * @param handle handle to a rules + * @param path path to the rules file + * @param flags write flags + * @return 0 on success + */ +extern int smack_write_rules_to_kernel(smack_rules_t handle, const char *path); /*! * Add new rule to a rule set. Updates existing rule if there is already rule diff --git a/src/smack_rules.c b/src/smack_rules.c index efeb298..473b11c 100644 --- a/src/smack_rules.c +++ b/src/smack_rules.c @@ -58,7 +58,8 @@ static int update_rule(struct smack_subject **subjects, unsigned ac); static void destroy_rules(struct smack_subject **subjects); inline unsigned str_to_ac(const char *str); -inline void ac_to_str(unsigned ac, char *str, int flags); +inline void ac_to_config_str(unsigned ac, char *str); +inline void ac_to_kernel_str(unsigned ac, char *str); smack_rules_t smack_create_rules(void) { @@ -121,8 +122,37 @@ int smack_read_rules_from_file(smack_rules_t handle, const char *path, return ret; } -int smack_write_rules_to_file(smack_rules_t handle, const char *path, - int flags) +int smack_write_rules_to_file(smack_rules_t handle, const char *path) +{ + struct smack_subject *s, *stmp; + struct smack_object *o, *otmp; + FILE *file; + char str[SMACK_ACC_LEN + 1]; + int err = 0; + + file = fopen(path, "w+"); + if (!file) + return -1; + + HASH_ITER(hh, handle->subjects, s, stmp) { + HASH_ITER(hh, s->objects, o, otmp) { + ac_to_config_str(o->ac, str); + + err = fprintf(file, "%s %s %s\n", + s->subject, o->object, str); + + if (err < 0) { + fclose(file); + return errno; + } + } + } + + fclose(file); + return 0; +} + +int smack_write_rules_to_kernel(smack_rules_t handle, const char *path) { struct smack_subject *s, *stmp; struct smack_object *o, *otmp; @@ -136,14 +166,10 @@ int smack_write_rules_to_file(smack_rules_t handle, const char *path, HASH_ITER(hh, handle->subjects, s, stmp) { HASH_ITER(hh, s->objects, o, otmp) { - ac_to_str(o->ac, str, flags); + ac_to_kernel_str(o->ac, str); - if ((flags & SMACK_RULES_KERNEL) != 0) - err = fprintf(file, "%-23s %-23s %4s\n", - s->subject, o->object, str); - else - err = fprintf(file, "%s %s %s\n", - s->subject, o->object, str); + err = fprintf(file, "%-23s %-23s %4s\n", + s->subject, o->object, str); if (err < 0) { fclose(file); @@ -154,6 +180,7 @@ int smack_write_rules_to_file(smack_rules_t handle, const char *path, fclose(file); return 0; + } int smack_add_rule(smack_rules_t handle, const char *subject, @@ -313,26 +340,27 @@ inline unsigned str_to_ac(const char *str) return access; } -inline void ac_to_str(unsigned access, char *str, int flags) +inline void ac_to_config_str(unsigned access, char *str) { int i; - if ((flags & SMACK_RULES_KERNEL) != 0) { - str[0] = ((access & SMACK_ACC_R) != 0) ? 'r' : '-'; - str[1] = ((access & SMACK_ACC_W) != 0) ? 'w' : '-'; - str[2] = ((access & SMACK_ACC_X) != 0) ? 'x' : '-'; - str[3] = ((access & SMACK_ACC_A) != 0) ? 'a' : '-'; - str[4] = '\0'; - } else { - i = 0; - if ((access & SMACK_ACC_R) != 0) - str[i++] = 'r'; - if ((access & SMACK_ACC_W) != 0) - str[i++] = 'w'; - if ((access & SMACK_ACC_X) != 0) - str[i++] = 'x'; - if ((access & SMACK_ACC_A) != 0) - str[i++] = 'a'; - str[i] = '\0'; - } + i = 0; + if ((access & SMACK_ACC_R) != 0) + str[i++] = 'r'; + if ((access & SMACK_ACC_W) != 0) + str[i++] = 'w'; + if ((access & SMACK_ACC_X) != 0) + str[i++] = 'x'; + if ((access & SMACK_ACC_A) != 0) + str[i++] = 'a'; + str[i] = '\0'; +} + +inline void ac_to_kernel_str(unsigned access, char *str) +{ + str[0] = ((access & SMACK_ACC_R) != 0) ? 'r' : '-'; + str[1] = ((access & SMACK_ACC_W) != 0) ? 'w' : '-'; + str[2] = ((access & SMACK_ACC_X) != 0) ? 'x' : '-'; + str[3] = ((access & SMACK_ACC_A) != 0) ? 'a' : '-'; + str[4] = '\0'; } diff --git a/tests/check_rules.c b/tests/check_rules.c index 5781d91..494ad7f 100644 --- a/tests/check_rules.c +++ b/tests/check_rules.c @@ -36,7 +36,7 @@ START_TEST(test_add_new_rule) fail_unless(rc == 0, "Failed to read ruleset"); rc = smack_add_rule(rules, "Orange", "Apple", "ra"); fail_unless(rc == 0, "Failed to add rule"); - rc = smack_write_rules_to_file(rules, "add_new_rule-result.txt", SMACK_RULES_KERNEL); + rc = smack_write_rules_to_kernel(rules, "add_new_rule-result.txt"); fail_unless(rc == 0, "Failed to write ruleset"); rc = files_equal("add_new_rule-result.txt", "data/add_new_rule-excepted.txt"); fail_unless(rc == 1, "Unexcepted result"); @@ -53,7 +53,7 @@ START_TEST(test_modify_existing_rule) fail_unless(rc == 0, "Failed to read ruleset"); rc = smack_add_rule(rules, "Foo", "Bar", "wx"); fail_unless(rc == 0, "Failed to add rule"); - rc = smack_write_rules_to_file(rules, "modify_existing_rule-result.txt", SMACK_RULES_KERNEL); + rc = smack_write_rules_to_kernel(rules, "modify_existing_rule-result.txt"); fail_unless(rc == 0, "Failed to write ruleset"); rc = files_equal("modify_existing_rule-result.txt", "data/modify_existing_rule-excepted.txt"); fail_unless(rc == 1, "Unexcepted result"); @@ -68,7 +68,7 @@ START_TEST(test_rw_rules_config) fail_unless(rules != NULL, "Ruleset creation failed"); rc = smack_read_rules_from_file(rules, "data/write_rules_config-excepted.txt", NULL); fail_unless(rc == 0, "Failed to read ruleset"); - rc = smack_write_rules_to_file(rules, "write_rules_config-result.txt", 0); + rc = smack_write_rules_to_file(rules, "write_rules_config-result.txt"); fail_unless(rc == 0, "Failed to write ruleset"); rc = files_equal("write_rules_config-result.txt", "data/write_rules_config-excepted.txt"); fail_unless(rc == 1, "Unexcepted result"); @@ -83,7 +83,7 @@ START_TEST(test_rw_rules_kernel) fail_unless(rules != NULL, "Ruleset creation failed"); rc = smack_read_rules_from_file(rules, "data/write_rules_config-excepted.txt", NULL); fail_unless(rc == 0, "Failed to read ruleset"); - rc = smack_write_rules_to_file(rules, "write_rules_kernel-result.txt", SMACK_RULES_KERNEL); + rc = smack_write_rules_to_kernel(rules, "write_rules_kernel-result.txt"); fail_unless(rc == 0, "Failed to write ruleset"); rc = files_equal("write_rules_kernel-result.txt", "data/write_rules_kernel-excepted.txt"); fail_unless(rc == 1, "Unexcepted result"); @@ -100,7 +100,7 @@ START_TEST(test_remove_rule) fail_unless(rc == 0, "Failed to read ruleset"); rc = smack_remove_rule(rules, "Orange", "Apple"); fail_unless(rc == 0, "Failed to remove rule"); - rc = smack_write_rules_to_file(rules, "remove_rule-result.txt", SMACK_RULES_KERNEL); + rc = smack_write_rules_to_kernel(rules, "remove_rule-result.txt"); fail_unless(rc == 0, "Failed to write ruleset"); rc = files_equal("remove_rule-result.txt", "data/remove_rule-excepted.txt"); fail_unless(rc == 1, "Unexcepted result"); @@ -116,7 +116,7 @@ START_TEST(test_remove_rules_by_subject) rc = smack_read_rules_from_file(rules, "data/remove_rules_by_subject-in.txt", NULL); fail_unless(rc == 0, "Failed to read ruleset"); smack_remove_rules_by_subject(rules, "Foo"); - rc = smack_write_rules_to_file(rules, "remove_rules_by_subject-result.txt", SMACK_RULES_KERNEL); + rc = smack_write_rules_to_kernel(rules, "remove_rules_by_subject-result.txt"); fail_unless(rc == 0, "Failed to write ruleset"); rc = files_equal("remove_rules_by_subject-result.txt", "data/remove_rules_by_subject-excepted.txt"); fail_unless(rc == 1, "Unexcepted result"); @@ -132,7 +132,7 @@ START_TEST(test_remove_rules_by_object) rc = smack_read_rules_from_file(rules, "data/remove_rules_by_object-in.txt", NULL); fail_unless(rc == 0, "Failed to read ruleset"); smack_remove_rules_by_object(rules, "Apple"); - rc = smack_write_rules_to_file(rules, "remove_rules_by_object-result.txt", SMACK_RULES_KERNEL); + rc = smack_write_rules_to_kernel(rules, "remove_rules_by_object-result.txt"); fail_unless(rc == 0, "Failed to write ruleset"); rc = files_equal("remove_rules_by_object-result.txt", "data/remove_rules_by_object-excepted.txt"); fail_unless(rc == 1, "Unexcepted result");