Beispiel #1
0
static void fill_job_info(launch_data_t data, const char *key,
		    LaunchJobStatus * info)
{
	switch (launch_data_get_type(data)) {
	case LAUNCH_DATA_STRING:
	    if (strcmp(key, LAUNCH_JOBKEY_LABEL) == 0) {
		info->m_label = std::string(launch_data_get_string(data));
	    } else if (strcmp(key, LAUNCH_JOBKEY_PROGRAM) == 0) {
		info->m_program = std::string(launch_data_get_string(data));
	    }

	    return;

	case LAUNCH_DATA_INTEGER:
	    if (strcmp(key, LAUNCH_JOBKEY_PID) == 0) {
		info->m_pid = (pid_t)launch_data_get_integer(data);
	    }

	    return;

	case LAUNCH_DATA_DICTIONARY:
	    launch_data_dict_iterate(data,
			(dict_iterate_callback)fill_job_info, info);
	    return;

	default:
	    return; /* do nothing */
	}
}
Beispiel #2
0
/*
 * Lookup the audit mach port in the launchd dictionary.
 */
static mach_port_t
lookup_machport(const char *label)
{
	launch_data_t msg, msd, ld, cdict, to;
	mach_port_t mp = MACH_PORT_NULL;

	msg = launch_data_new_string(LAUNCH_KEY_CHECKIN);

	cdict = launch_msg(msg);
	if (cdict == NULL) {
		auditd_log_err("launch_msg(\"" LAUNCH_KEY_CHECKIN
		    "\") IPC failure: %m");
                return (MACH_PORT_NULL);
        }

	if (launch_data_get_type(cdict) == LAUNCH_DATA_ERRNO) {
		errno = launch_data_get_errno(cdict);
		auditd_log_err("launch_data_get_type() can't get dict: %m");
		return (MACH_PORT_NULL);
	}

	to = launch_data_dict_lookup(cdict, LAUNCH_JOBKEY_TIMEOUT);
	if (to) {
		max_idletime = launch_data_get_integer(to);
		auditd_log_debug("launchd timeout set to %d", max_idletime);
	} else {
		auditd_log_debug("launchd timeout not set, setting to 60");
		max_idletime = 60;
	}

	msd = launch_data_dict_lookup(cdict, LAUNCH_JOBKEY_MACHSERVICES);
	if (msd == NULL) {
		auditd_log_err(
		    "launch_data_dict_lookup() can't get mach services");
		return (MACH_PORT_NULL);
	}

	ld = launch_data_dict_lookup(msd, label);
	if (ld == NULL) {
		auditd_log_err("launch_data_dict_lookup can't find %s", label);
		return (MACH_PORT_NULL);
	}

	mp = launch_data_get_machport(ld);

	return (mp);
}
Beispiel #3
0
static PyObject* 
launch2python(launch_data_t data)
{
	launch_data_type_t tp;

	tp = launch_data_get_type(data);
	switch (tp) {
	case LAUNCH_DATA_DICTIONARY:
		return launchdict2python(data);
	
	case LAUNCH_DATA_ARRAY:
		return launcharray2python(data);
	
	case LAUNCH_DATA_FD:
		return PyObject_CallFunction(file_descriptor_type, "i", 
				launch_data_get_fd(data));
		return NULL;

	case LAUNCH_DATA_INTEGER:
		return PyLong_FromLongLong(launch_data_get_integer(data));

	case LAUNCH_DATA_REAL:
		return PyFloat_FromDouble(launch_data_get_real(data));

	case LAUNCH_DATA_BOOL:
		return PyBool_FromLong(launch_data_get_bool(data));

	case LAUNCH_DATA_STRING:
		return PyString_FromString(launch_data_get_string(data));

	case LAUNCH_DATA_OPAQUE:
		return PyBuffer_FromMemory(
				launch_data_get_opaque(data),
				launch_data_get_opaque_size(data));

	case LAUNCH_DATA_ERRNO:
		return PyObject_CallFunction(errno_type, "i", 
				launch_data_get_errno(data));

	default:
		PyErr_Format(PyExc_ValueError,
			"Unhandled launch data type (tag: %d)", (int)tp);
		return NULL;
	}
}
Beispiel #4
0
bool launchd_checkin(unsigned * idle_timeout_secs)
{
	launch_data_t msg;
	launch_data_t resp;
	launch_data_t item;
	bool is_launchd = true;

	msg = launch_data_new_string(LAUNCH_KEY_CHECKIN);
	resp = launch_msg(msg);
	if (resp == NULL) {
		/* IPC to launchd failed. */
		LAUNCHD_MSG_ERR("checkin", LAUNCH_KEY_CHECKIN);
		launch_data_free(msg);
		return false;
	}

	if (launch_data_get_type(resp) == LAUNCH_DATA_ERRNO) {
		errno = launch_data_get_errno(resp);
		goto done;
	}

	/* At this point, we know we are running under launchd. */
	is_launchd = true;

	if ((item = launch_data_dict_lookup(resp, LAUNCH_JOBKEY_TIMEOUT))) {
		long long val = launch_data_get_integer(item);

		*idle_timeout_secs =
			(val < 0) ? 0 : ((val > UINT_MAX) ?  UINT_MAX : val);
	}

done:
	launch_data_free(msg);
	launch_data_free(resp);
	return is_launchd;
}
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) {
#ifdef DEBUG
    CFShow(local_error);
#endif //  DEBUG
    CFRelease(local_error);
  }
  return cf_type_ref;
}