Skip to content

Commit

Permalink
pluginprocess: handle parentKey separately
Browse files Browse the repository at this point in the history
see #2142
  • Loading branch information
Markus Raab committed Jul 31, 2018
1 parent eb3f7e1 commit 5f21f15
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
48 changes: 33 additions & 15 deletions src/libs/pluginprocess/pluginprocess.c
Expand Up @@ -56,6 +56,25 @@ static char * intToStr (int i)
return str;
}

static void addParent (KeySet * ks, Key * key)
{
Key * nameKey = keyNew ("/pluginprocess/parent/name", KEY_VALUE, keyName (key), KEY_END);
ksAppendKey (ks, nameKey);

Key * parentKey = keyDup (key);
keySetName (parentKey, "/pluginprocess/parent");
ksAppendKey (ks, parentKey);
}

static Key * popParent (KeySet * ks)
{
Key * parentKey = ksLookupByName (ks, "/pluginprocess/parent", KDB_O_POP);
Key * parentNameKey = ksLookupByName (ks, "/pluginprocess/parent/name", KDB_O_POP);
keySetName (parentKey, keyString (parentNameKey));
keyDel (parentNameKey);
return parentKey;
}

/** Start the child process' command loop
*
* This will make the child process wait for plugin commands
Expand All @@ -81,8 +100,7 @@ void elektraPluginProcessStart (Plugin * handle, ElektraPluginProcess * pp)
ELEKTRA_LOG_DEBUG ("Child: We received a KeySet with %zd keys in it", ksGetSize (keySet));

Key * commandKey = ksLookupByName (keySet, "/pluginprocess/command", KDB_O_POP);
Key * originalNameKey = ksLookupByName (keySet, "/pluginprocess/original", KDB_O_POP);
Key * originalKey = ksLookupByName (keySet, keyString (originalNameKey), KDB_O_NONE);
Key * parentKey = popParent (keySet);
int result = ELEKTRA_PLUGIN_STATUS_ERROR;

char * endPtr;
Expand All @@ -98,20 +116,20 @@ void elektraPluginProcessStart (Plugin * handle, ElektraPluginProcess * pp)
{
case ELEKTRA_PLUGINPROCESS_OPEN:
counter++;
result = handle->kdbOpen (handle, originalKey);
result = handle->kdbOpen (handle, parentKey);
break;
case ELEKTRA_PLUGINPROCESS_CLOSE:
counter--;
result = handle->kdbClose (handle, originalKey);
result = handle->kdbClose (handle, parentKey);
break;
case ELEKTRA_PLUGINPROCESS_GET:
result = handle->kdbGet (handle, keySet, originalKey);
result = handle->kdbGet (handle, keySet, parentKey);
break;
case ELEKTRA_PLUGINPROCESS_SET:
result = handle->kdbSet (handle, keySet, originalKey);
result = handle->kdbSet (handle, keySet, parentKey);
break;
case ELEKTRA_PLUGINPROCESS_ERROR:
result = handle->kdbError (handle, keySet, originalKey);
result = handle->kdbError (handle, keySet, parentKey);
break;
default:
result = ELEKTRA_PLUGIN_STATUS_ERROR;
Expand All @@ -121,17 +139,18 @@ void elektraPluginProcessStart (Plugin * handle, ElektraPluginProcess * pp)
else
{
ELEKTRA_LOG_DEBUG ("Child: Unrecognized command %s", keyString (commandKey));
ELEKTRA_SET_ERRORF (191, originalKey, "Received invalid command code or no KeySet: %s", keyString (commandKey));
ELEKTRA_SET_ERRORF (191, parentKey, "Received invalid command code or no KeySet: %s", keyString (commandKey));
}
errno = prevErrno;
char * resultStr = intToStr (result);
addParent (keySet, parentKey);
ksAppendKey (keySet, keyNew ("/pluginprocess/result", KEY_VALUE, resultStr, KEY_END));
elektraFree (resultStr);

ELEKTRA_LOG_DEBUG ("Child: Writing the resulting KeySet back to the parent");
elektraInvoke2Args (pp->dump, "set", keySet, resultPipeKey);
keyDel (commandKey);
keyDel (originalNameKey);
keyDel (parentKey);
ELEKTRA_LOG ("Child: Command handled, startup counter is at %d", counter);
} while (counter);

Expand Down Expand Up @@ -182,8 +201,7 @@ int elektraPluginProcessSend (const ElektraPluginProcess * pp, pluginprocess_t c
char * commandStr = intToStr (command);
ksAppendKey (keySet, keyNew ("/pluginprocess/command", KEY_VALUE, commandStr, KEY_END));
elektraFree (commandStr);
Key * originalKey = keyNew ("/pluginprocess/original", KEY_VALUE, keyName (key), KEY_END);
ksAppendKey (keySet, originalKey);
addParent (keySet, key);

// Serialize, currently statically use dump as our default format, this already writes everything out to the pipe
Key * commandPipeKey = keyNew ("/pluginprocess/pipe/command", KEY_VALUE, pp->commandPipe, KEY_END);
Expand All @@ -195,7 +213,7 @@ int elektraPluginProcessSend (const ElektraPluginProcess * pp, pluginprocess_t c
ELEKTRA_LOG ("Parent: We received %zd keys in return", ksGetSize (keySet));

// Bring everything back in order by removing our process-related keys
Key * originalDeserializedKey = ksLookupByName (keySet, keyName (key), KDB_O_POP);
Key * parentDeserializedKey = popParent (keySet);
Key * resultKey = ksLookupByName (keySet, "/pluginprocess/result", KDB_O_POP);

// Parse the result value
Expand All @@ -205,20 +223,20 @@ int elektraPluginProcessSend (const ElektraPluginProcess * pp, pluginprocess_t c
long lresult = strtol (keyString (resultKey), &endPtr, 10);
if (*endPtr != '\0' || errno == ERANGE || lresult > INT_MAX || lresult < INT_MIN)
{
ELEKTRA_SET_ERRORF (191, originalKey, "Received invalid return code or no KeySet: %s", keyString (resultKey));
ELEKTRA_SET_ERRORF (191, key, "Received invalid return code or no KeySet: %s", keyString (resultKey));
errno = prevErrno;
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
errno = prevErrno;

// Copy everything back into the actual keysets
keyCopy (key, originalDeserializedKey);
keyCopy (key, parentDeserializedKey);
ksCopy (originalKeySet, keySet);

// Command finished, cleanup now
keyDel (commandPipeKey);
keyDel (resultPipeKey);
keyDel (originalDeserializedKey);
keyDel (parentDeserializedKey);
keyDel (resultKey);
ksDel (keySet);

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/python/testmod_python.c
Expand Up @@ -55,8 +55,8 @@ static void test_variable_passing (void)
Key * parentKey = keyNew ("user/from_c", KEY_END);
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
succeed_if (ksGetSize (ks) == 1, "keyset size is still 0");
succeed_if (ksGetSize (ks) == 1 && !strcmp (keyName (ksHead (ks)), "user/from_python"), "key in keyset has wrong name");
exit_if_fail (ksGetSize (ks) == 1, "keyset size is still 0");
succeed_if_same_string (keyName (ksHead (ks)), "user/from_python");

ksDel (ks);
keyDel (parentKey);
Expand Down

0 comments on commit 5f21f15

Please sign in to comment.