MCPcopy Create free account
hub / github.com/F-Stack/f-stack / ng_add_hook

Function ng_add_hook

lib/ff_ng_base.c:1069–1121  ·  view source on GitHub ↗

* Add an unconnected hook to a node. Only used internally. * Assumes node is locked. (XXX not yet true ) */

Source from the content-addressed store, hash-verified

1067 * Assumes node is locked. (XXX not yet true )
1068 */
1069static int
1070ng_add_hook(node_p node, const char *name, hook_p *hookp)
1071{
1072 hook_p hook;
1073 int error = 0;
1074
1075 /* Check that the given name is good */
1076 if (name == NULL) {
1077 TRAP_ERROR();
1078 return (EINVAL);
1079 }
1080 if (ng_findhook(node, name) != NULL) {
1081 TRAP_ERROR();
1082 return (EEXIST);
1083 }
1084
1085 /* Allocate the hook and link it up */
1086 NG_ALLOC_HOOK(hook);
1087 if (hook == NULL) {
1088 TRAP_ERROR();
1089 return (ENOMEM);
1090 }
1091 hook->hk_refs = 1; /* add a reference for us to return */
1092 hook->hk_flags = HK_INVALID;
1093 hook->hk_peer = &ng_deadhook; /* start off this way */
1094 hook->hk_node = node;
1095 NG_NODE_REF(node); /* each hook counts as a reference */
1096
1097 /* Set hook name */
1098 strlcpy(NG_HOOK_NAME(hook), name, NG_HOOKSIZ);
1099
1100 /*
1101 * Check if the node type code has something to say about it
1102 * If it fails, the unref of the hook will also unref the node.
1103 */
1104 if (node->nd_type->newhook != NULL) {
1105 if ((error = (*node->nd_type->newhook)(node, hook, name))) {
1106 NG_HOOK_UNREF(hook); /* this frees the hook */
1107 return (error);
1108 }
1109 }
1110 /*
1111 * The 'type' agrees so far, so go ahead and link it in.
1112 * We'll ask again later when we actually connect the hooks.
1113 */
1114 LIST_INSERT_HEAD(&node->nd_hooks, hook, hk_hooks);
1115 node->nd_numhooks++;
1116 NG_HOOK_REF(hook); /* one for the node */
1117
1118 if (hookp)
1119 *hookp = hook;
1120 return (0);
1121}
1122
1123/*
1124 * Find a hook

Callers 2

ng_con_nodesFunction · 0.70
ng_mkpeerFunction · 0.70

Calls 2

ng_findhookFunction · 0.70
strlcpyFunction · 0.50

Tested by

no test coverage detected