Skip to main content
Version: Dev (main)

Co-Process Onboarding

Source: qar-streaming-c/examples/co_process_onboarding.c. Which mode to use: Choosing an Onboarding Mode. API details: Onboarding and Sessions.

Co-Process Onboarding

What You Will Learn​

  • Join as a local app with an app tag (QarOnboardLocalAppTagExt)
  • Request an invite for a companion process once per session (QarRequestInviteInit) and reuse its bytes for every start of the companion
  • Hand the invite over the companion's stdin, never on its command line
  • Join from the invite with a runtime that keeps its identity in memory only (QarRuntimeEphemeralStorageExt), in one onboarding call bounded by a cancel token

When to Use It​

Your app runs on the QAROS PC and starts a helper in another process, for example a companion written in C#. The helper is another executable, so it cannot share your app's session (Several API Instances in One Process) and must not ask the user for an approval of its own or store an identity: it joins under your app's approval with the invite your app hands it. Windows and Linux, like local enrollment.

Build and Run​

 cmake --build --preset x64-windows-debug --target co_process_onboarding
 ./build/x64-windows/Debug/co_process_onboarding.exe

The example joins, requests one invite, then starts itself twice with --co-process as the companion and writes the same invite to both. Each start prints its peer id: both are the same peer.

Step 1: The App Requests the Invite, Once​

The app first joins as a local app (Local App Onboarding), then asks for an invite named by the companion's tag and serializes it:

  /* Once per session: the invite works for every start of the companion
  * while this session lives. */
  request.app_tag = COMPANION_TAG; /* required: names the companion */
  QarOnboardingInvite* invite = NULL;
  QarResult requested =
  qar_session_request_onboarding_invite(session, &request, NULL, NULL, NULL, &invite);
  {
  /* Final: this session did not join as a local app of this PC. */
  fprintf(stderr, "Only a local app may request an invite for its companion.\n");
  }
  log_result("qar_session_request_onboarding_invite", requested);
 
  /* Serialize into a buffer of its own so it can be cleared when the session
  * ends. The result is one line of text; the companion reads it up to the
  * newline. */
  size_t size = 0;
  char* line = NULL;
  if(qar_result_is_success(requested))
  {
  if(qar_result_is_error(sized))
  {
  log_result("qar_onboarding_invite_serialized_size", sized);
  }
  else if(size == 0 || size >= MAX_INVITE_LINE - 1)
  {
  fprintf(stderr, "The invite has an unexpected size (%zu bytes).\n", size);
  }
  else
  {
  line = (char*)calloc(1, size + 1);
  }
  }
  size_t written = 0;
  QarResult serialized = qar_result_success();
  if(line != NULL)
  {
  serialized = qar_onboarding_invite_serialize(invite, (uint8_t*)line, size, &written);
  log_result("qar_onboarding_invite_serialize", serialized); /* never the bytes */
  }
  qar_onboarding_invite_handle_destroy(invite); /* the bytes are what is kept; NULL is ignored */

What is checked before the invite is made:

  • The requester holds a certificate the hub signed for a local app on its own PC, or is a process the QAROS launcher started. Any other session is refused with QAR_STATUS_ONBOARDING_INVITE_REFUSED (final; nothing was made).
  • The tag follows the app tag rules; a missing, empty or non-ASCII tag, or a QarRequestInviteInit built from an older header, fails with QAR_STATUS_ARGUMENT_NOT_SUPPORTED ("app tag required") before anything is asked.

What the invite is:

  • It names your app on this PC, not the hub: a named pipe on Windows, a Unix socket under /run/user/<uid>/qaros on Linux. It reaches only this user on this PC.
  • It works for every start of the companion while your app's session lives, and has no expiry of its own. Request it once per session and reuse the bytes; there is no timer to watch and nothing to refresh.
  • Each onboarding attempt of the companion asks your app for a fresh one-time hub invite, so your app stays in the session while companions start.
  • It is bound to the peer id the hub remembers for your app's approval and the companion's tag: the companion joins as the same peer on every start, while no other live process holds that id (then a one-off id).
  • The companion's certificate renews only while your app is still in the session.

The invite is a secret. Hand it over a private channel (stdin, a pipe, shared memory), never on a command line, in an environment variable, a file or a log, and clear your copy when your session ends.

Step 2: Hand the Same Bytes to Every Start​

  if(line != NULL && qar_result_is_success(serialized))
  {
  /* The same bytes for every start while this session lives. The invite
  * is a secret: hand it over a private channel such as the companion's
  * stdin, never on its command line, in its environment, a file or a
  * log. */
  exit_code = 0;
  for(int start = 1; start <= COMPANION_STARTS; ++start)
  {
  printf("Starting the companion (%d of %d) with the same invite\n", start, COMPANION_STARTS);
  if(run_companion(program, line, written) != 0)
  {
  exit_code = 1;
  }
  }
  }
  if(line != NULL)
  {
  memset(line, 0, size + 1); /* clear the secret before freeing it */
  free(line);
  }

Step 3: The Companion Joins From the Invite​

  runtime_init.header.next = &ephemeral.header; /* nothing written to disk */
  QarRuntime* runtime = NULL;
  QarResult created = qar_runtime_create(&runtime_init, &runtime);
  log_result("qar_runtime_create (companion)", created);
  if(qar_result_is_error(created) || runtime == NULL)
  {
  return 1;
  }
 
  int exit_code = 1;
  char* line = (char*)calloc(1, MAX_INVITE_LINE);
  size_t length = 0;
  QarOnboardingInvite* invite = NULL;
  const bool got_line = line != NULL && read_invite_line(line, MAX_INVITE_LINE, &length);
  if(got_line)
  {
  parsed = qar_onboarding_invite_deserialize((const uint8_t*)line, length, &invite);
  log_result("qar_onboarding_invite_deserialize", parsed);
  }
  else
  {
  fprintf(stderr, "No invite on stdin.\n");
  }
  /* The call retries by itself while the app or the hub is away (250 ms,
  * doubling up to 5 s): the token bounds that wait. */
  QarCancelToken* cancel = NULL;
  QarResult token_created = qar_result_success();
  if(got_line && qar_result_is_success(parsed))
  {
  log_result("qar_cancel_token_create_with_timeout", token_created);
  }
  if(got_line && qar_result_is_success(parsed) && qar_result_is_success(token_created))
  {
  init.presentation.display_name = "Co-Process Onboarding Example (companion)";
  invite_mode.invite = invite; /* borrowed */
  init.header.next = &invite_mode.header;
 
  QarOnboardingId unused = qar_onboarding_id_default(); /* zero: in memory */
  QarSession* session = NULL;
  /* One call; the companion does not retry it. */
  QarResult joined =
  qar_runtime_onboard(runtime, &init, NULL, NULL, cancel, &unused, &session);
  log_result("qar_runtime_onboard (companion)", joined);
  {
  fprintf(
  stderr,
  "Neither the app nor the hub answered within %u s.\n",
  );
  }
  if(qar_result_is_success(joined) && session != NULL)
  {
  exit_code = example_print_own_peer_id("Companion", session) ? 0 : 1;
  }
  }
  if(cancel != NULL)
  {
  }
  if(line != NULL)
  {
  memset(line, 0, MAX_INVITE_LINE);
  free(line);
  }
  • QarRuntimeEphemeralStorageExt keeps the identity in memory: nothing is written under the storage folder and the returned onboarding id is zero. A runtime older than the extension skips it and stores the identity on disk: check qar_runtime_info before relying on it.
  • The companion makes one qar_runtime_onboard call and does not retry it. While your app or the hub is away, the call tries again by itself, after 250 ms, doubling up to 5 s. The cancel token bounds that wait; when it runs out the call fails with QAR_STATUS_TIMEOUT.
  • The call fails at once only for what no retry changes: a forged invite, a denied approval, or your app's session gone. Treat these as final.

What Is Stored, and for How Long​

  • The app: nothing on disk. It keeps the invite bytes in memory for its session and clears them when it leaves.
  • The companion: nothing on disk. Its memory holds key and certificate (local profile under your app's approval), 30 minutes, renewed only while your app is in the session.
  • The hub: the remembered peer id for (your approval, companion tag), as for local app tags until unused for 30 days.
  • When your app leaves, the companion drops out at the latest when its certificate ends. Keep your app in the session while the companion runs; this example waits for each companion before it goes on.
  • Revoking your app's approval revokes both and forgets their peer ids.
  • Give the companion a tag of its own, not one your app's instances use.

Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.9.8.