Browse code

cfg framework: cfg_group_inst_exists() added

The function checkes whether or not a group instance exists.

Miklos Tirpak authored on 04/01/2011 15:21:04
Showing 2 changed files
... ...
@@ -1648,6 +1648,56 @@ error:
1648 1648
 	return -1;
1649 1649
 }
1650 1650
 
1651
+/* Check the existance of a group instance.
1652
+ * return value:
1653
+ *	1: exists
1654
+ *	0: does not exist
1655
+ */
1656
+int cfg_group_inst_exists(cfg_ctx_t *ctx, str *group_name, unsigned int group_id)
1657
+{
1658
+	cfg_group_t	*group;
1659
+	cfg_add_var_t	*add_var;
1660
+	int	found;
1661
+
1662
+	/* verify the context even if we do not need it now
1663
+	to make sure that a cfg driver has called the function
1664
+	(very very weak security) */
1665
+	if (!ctx) {
1666
+		LOG(L_ERR, "ERROR: cfg_group_inst_exists(): context is undefined\n");
1667
+		return 0;
1668
+	}
1669
+
1670
+	if (!(group = cfg_lookup_group(group_name->s, group_name->len))) {
1671
+		LOG(L_ERR, "ERROR: cfg_group_inst_exists(): group not found\n");
1672
+		return 0;
1673
+	}
1674
+
1675
+	if (!cfg_shmized) {
1676
+		/* group instances are stored in the additional variable list
1677
+		 * before forking */
1678
+		found = 0;
1679
+		for (	add_var = group->add_var;
1680
+			add_var;
1681
+			add_var = add_var->next
1682
+		)
1683
+			if (add_var->group_id == group_id) {
1684
+				found = 1;
1685
+				break;
1686
+			}
1687
+
1688
+	} else {
1689
+		/* make sure that nobody else replaces the global config meantime */
1690
+		CFG_WRITER_LOCK();
1691
+		found = (cfg_find_group(CFG_GROUP_META(*cfg_global, group),
1692
+								group->size,
1693
+								group_id)
1694
+				!= NULL);
1695
+		CFG_WRITER_UNLOCK();
1696
+	}
1697
+
1698
+	return found;
1699
+}
1700
+
1651 1701
 /* Apply the changes to a group instance as long as the additional variable
1652 1702
  * belongs to the specified group_id. *add_var_p is moved to the next additional
1653 1703
  * variable, and all the consumed variables are freed.
... ...
@@ -198,6 +198,13 @@ int cfg_add_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int group_id);
198 198
 /* Delete an instance of a group */
199 199
 int cfg_del_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int group_id);
200 200
 
201
+/* Check the existance of a group instance.
202
+ * return value:
203
+ *	1: exists
204
+ *	0: does not exist
205
+ */
206
+int cfg_group_inst_exists(cfg_ctx_t *ctx, str *group_name, unsigned int group_id);
207
+
201 208
 /* Apply the changes to a group instance as long as the additional variable
202 209
  * belongs to the specified group_id. *add_var_p is moved to the next additional
203 210
  * variable, and all the consumed variables are freed.