 {
    *error = local_error;
  } else if (local_error) {
    CFShow(local_error);
    CFRelease(local_error);
  }
  return result;
}

CFTypeRef GTMCFTypeCreateFromLaunchData(launch_data_t ldata,
                                        bool convert_non_standard_objects,
                                        CFErrorRef *error) {
  CFTypeRef cf_type_ref = NULL;
  CFErrorRef local_error = NULL;
  if (ldata == NULL) {
    local_error = GTMCFLaunchCreateUnlocalizedError(EINVAL,
                                                    CFSTR("NULL ldata"));
    goto exit;
  }

  launch_data_type_t ldata_type = launch_data_get_type(ldata);
  switch (ldata_type) {
    case LAUNCH_DATA_STRING:
      cf_type_ref
        = CFStringCreateWithCString(kCFAllocatorDefault,
                                    launch_data_get_string(ldata),
                                    kCFStringEncodingUTF8);
      break;

    case LAUNCH_DATA_INTEGER: {
      long long value = launch_data_get_integer(ldata);
      cf_type_ref = CFNumberCreate(kCFAllocatorDefault,
                                   kCFNumberLongLongType,
                                   &value);
      break;
    }

    case LAUNCH_DATA_REAL: {
      double value = launch_data_get_real(ldata);
      cf_type_ref = CFNumberCreate(kCFAllocatorDefault,
                                   kCFNumberDoubleType,
                                   &value);
      break;
    }

    case LAUNCH_DATA_BOOL: {
      bool value = launch_data_get_bool(ldata);
      cf_type_ref = value? kCFBooleanTrue : kCFBooleanFalse;
      CFRetain(cf_type_ref);
      break;
    }

    case LAUNCH_DATA_OPAQUE: {
      size_t size = launch_data_get_opaque_size(ldata);
      void *data = launch_data_get_opaque(ldata);
      cf_type_ref = CFDataCreate(kCFAllocatorDefault, data, size);
      break;
    }

    case LAUNCH_DATA_ARRAY: {
      size_t count = launch_data_array_get_count(ldata);
      cf_type_ref = CFArrayCreateMutable(kCFAllocatorDefault,
                                         count,
                                         &kCFTypeArrayCallBacks);
      if (cf_type_ref) {
        for (size_t i = 0;!local_error && i < count; i++) {
          launch_data_t l_sub_data = launch_data_array_get_index(ldata, i);
          CFTypeRef cf_sub_type
            = GTMCFTypeCreateFromLaunchData(l_sub_data,
                                            convert_non_standard_objects,
                                            &local_error);
          if (cf_sub_type) {
            CFArrayAppendValue((CFMutableArrayRef)cf_type_ref, cf_sub_type);
            CFRelease(cf_sub_type);
          }
        }
      }
      break;
    }

    case LAUNCH_DATA_DICTIONARY:
      cf_type_ref = CFDictionaryCreateMutable(kCFAllocatorDefault,
                                              0,
                                              &kCFTypeDictionaryKeyCallBacks,
                                              &kCFTypeDictionaryValueCallBacks);
      if (cf_type_ref) {
        GTMLToCFDictContext context = {
          (CFMutableDictionaryRef)cf_type_ref,
          convert_non_standard_objects,
          &local_error
        };
        launch_data_dict_iterate(ldata,
                                 GTMConvertLaunchDataDictEntryToCFDictEntry,
                                 &context);
      }
      break;

    case LAUNCH_DATA_FD:
      if (convert_non_standard_objects) {
        int file_descriptor = launch_data_get_fd(ldata);
        cf_type_ref = CFNumberCreate(kCFAllocatorDefault,
                                     kCFNumberIntType,
                                     &file_descriptor);
      }
      break;

    case LAUNCH_DATA_MACHPORT:
      if (convert_non_standard_objects) {
        mach_port_t port = launch_data_get_machport(ldata);
        cf_type_ref = CFNumberCreate(kCFAllocatorDefault,
                                     kCFNumberIntType,
                                     &port);
      }
      break;

    default:
      local_error =
        GTMCFLaunchCreateUnlocalizedError(EINVAL,
                                          CFSTR("Unknown launchd type %d"),
                                          ldata_type);
      break;
  }
exit:
  if (error) {
    *error = local_error;
  } else if (local_error) {
    CFShow(local_error);
    CFRelease(local_error);
  }
  return cf_type_ref;
}

Boolean GTMSMJobSubmit(CFDictionaryRef cf_job, CFErrorRef *error) {
  CFErrorRef local_error = NULL;
  launch_data_t launch_job = GTMLaunchDataCreateFromCFType(cf_job,
                                                           &local_error);
  if (!local_error) {
    launch_data_t jobs = launch_data_alloc(LAUNCH_DATA_ARRAY);
    launch_data_array_set_index(jobs, launch_job, 0);
    launch_data_t msg = launch_data_alloc(LAUNCH_DATA_DICTIONARY);
    launch_data_dict_insert(msg, jobs, LAUNCH_KEY_SUBMITJOB);
    launch_data_t resp = launch_msg(msg);
    if (resp) {
      launch_data_type_t resp_type = launch_data_get_type(resp);
      switch (resp_type) {
        case LAUNCH_DATA_ARRAY:
          for (size_t i = 0; i < launch_data_array_get_count(jobs); i++) {
            launch_data_t job_response = launch_data_array_get_index(resp, i);
            launch_data_t job = launch_data_array_get_index(jobs, i);
            launch_data_t job_label
              = launch_data_dict_lookup(job, LAUNCH_JOBKEY_LABEL);
            const char *job_string
              = job_label? launch_data_get_string(job_label) : "Unlabeled job";
            if (LAUNCH_DATA_ERRNO == launch_data_get_type(job_response)) {
              int job_err = launch_data_get_errno(job_response);
              if (job_err!= 0) {
                // We only keep the last error
                if (local_error) {
                  CFRelease(local_error);
                  local_error = NULL;
                }
                switch (job_err) {
                  case EEXIST:
                    local_error
                      = GTMCFLaunchCreateUnlocalizedError(job_err,
                                                          CFSTR("%s already loaded"),
                                                          job_string);
                    break;
                  case ESRCH:
                    local_error
                      = GTMCFLaunchCreateUnlocalizedError(job_err,
                                                          CFSTR("%s not loaded"),
                                                          job_string);
                    break;
                  default:
                    local_error
                      = GTMCFLaunchCreateUnlocalizedError(job_err,
                                                          CFSTR("%s failed to load"),
                                                          job_string);
                    break;
                }
              }
            }
          }
          break;

        case LAUNCH_DATA_ERRNO: {
          int e = launch_data_get_errno(resp);
          if (e) {
            local_error = GTMCFLaunchCreateUnlocalizedError(e, CFSTR(""));
          }
          break;
        }

        default:
          local_error
            = GTMCFLaunchCreateUnlocalizedError(EINVAL,
                                                CFSTR("unknown response from launchd %d"),
                                                resp_type);
          break;
      }
      launch_data_free(resp);
      launch_data_free(msg);
    } else {
      local_error = GTMCFLa