Пример #1
0
int native_set_objectname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  SC_HANDLE service_handle = (SC_HANDLE) param;
  if (! service_handle) return -1;

  /*
    Logical syntax is: nssm set <service> ObjectName <username> <password>
    That means the username is actually passed in the additional parameter.
  */
  bool localsystem = false;
  TCHAR *username = NSSM_LOCALSYSTEM_ACCOUNT;
  TCHAR *password = 0;
  if (additional) {
    username = (TCHAR *) additional;
    if (value && value->string) password = value->string;
  }
  else if (value && value->string) username = value->string;

  const TCHAR *well_known = well_known_username(username);
  size_t passwordsize = 0;
  if (well_known) {
    if (str_equiv(well_known, NSSM_LOCALSYSTEM_ACCOUNT)) localsystem = true;
    username = (TCHAR *) well_known;
    password = _T("");
  }
  else if (! password) {
    /* We need a password if the account requires it. */
    print_message(stderr, NSSM_MESSAGE_MISSING_PASSWORD, name);
    return -1;
  }
  else passwordsize = _tcslen(password) * sizeof(TCHAR);

  /*
    ChangeServiceConfig() will fail to set the username if the service is set
    to interact with the desktop.
  */
  unsigned long type = SERVICE_NO_CHANGE;
  if (! localsystem) {
    QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
    if (! qsc) {
      if (passwordsize) SecureZeroMemory(password, passwordsize);
      return -1;
    }

    type = qsc->dwServiceType & ~SERVICE_INTERACTIVE_PROCESS;
    HeapFree(GetProcessHeap(), 0, qsc);
  }

  if (! well_known) {
    if (grant_logon_as_service(username)) {
      if (passwordsize) SecureZeroMemory(password, passwordsize);
      print_message(stderr, NSSM_MESSAGE_GRANT_LOGON_AS_SERVICE_FAILED, username);
      return -1;
    }
  }

  if (! ChangeServiceConfig(service_handle, type, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, username, password, 0)) {
    if (passwordsize) SecureZeroMemory(password, passwordsize);
    print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
    return -1;
  }

  if (passwordsize) SecureZeroMemory(password, passwordsize);

  if (localsystem) return 0;

  return 1;
}
Пример #2
0
inline void set_capture_direction(pcap_t *source,
                                  const capture_direction &dir) {
  if (pcap_setdirection(source, static_cast<pcap_direction_t>(dir)) != 0) {
    throw error{"pcap_setdirection error\n" + error_string(source)};
  }
}
Пример #3
0
/* Functions to manage native service settings. */
static int native_set_dependongroup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  SC_HANDLE service_handle = (SC_HANDLE) param;
  if (! service_handle) return -1;

  /*
    Get existing service dependencies because we must set both types together.
  */
  TCHAR *buffer;
  unsigned long buflen;
  if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, DEPENDENCY_SERVICES)) return -1;

  if (! value || ! value->string || ! value->string[0]) {
    if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, buffer, 0, 0, 0)) {
      print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
      if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
      return -1;
    }

    if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
    return 0;
  }

  unsigned long len = (unsigned long) _tcslen(value->string) + 1;
  TCHAR *unformatted = 0;
  unsigned long newlen;
  if (unformat_double_null(value->string, len, &unformatted, &newlen)) {
    if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
    return -1;
  }

  /* Prepend group identifier. */
  unsigned long missing = 0;
  TCHAR *canon = unformatted;
  size_t canonlen = 0;
  TCHAR *s;
  for (s = unformatted; *s; s++) {
    if (*s != SC_GROUP_IDENTIFIER) missing++;
    size_t len = _tcslen(s);
    canonlen += len + 1;
    s += len;
  }

  if (missing) {
    /* Missing identifiers plus double NULL terminator. */
    canonlen += missing + 1;
    newlen = (unsigned long) canonlen;

    canon = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, canonlen * sizeof(TCHAR));
    if (! canon) {
      print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("native_set_dependongroup"));
      if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted);
      if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
      return -1;
    }

    size_t i = 0;
    for (s = unformatted; *s; s++) {
      if (*s != SC_GROUP_IDENTIFIER) canon[i++] = SC_GROUP_IDENTIFIER;
      size_t len = _tcslen(s);
      memmove(canon + i, s, (len + 1) * sizeof(TCHAR));
      i += len + 1;
      s += len;
    }
  }

  TCHAR *dependencies;
  if (buflen > 2) {
    dependencies = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (newlen + buflen) * sizeof(TCHAR));
    if (! dependencies) {
      print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dependencies"), _T("native_set_dependongroup"));
      if (canon != unformatted) HeapFree(GetProcessHeap(), 0, canon);
      if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted);
      if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
      return -1;
    }

    memmove(dependencies, buffer, buflen * sizeof(TCHAR));
    memmove(dependencies + buflen - 1, canon, newlen * sizeof(TCHAR));
  }
  else dependencies = canon;

  int ret = 1;
  if (set_service_dependencies(service_name, service_handle, dependencies)) ret = -1;
  if (dependencies != unformatted) HeapFree(GetProcessHeap(), 0, dependencies);
  if (canon != unformatted) HeapFree(GetProcessHeap(), 0, canon);
  if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted);
  if (buffer) HeapFree(GetProcessHeap(), 0, buffer);

  return ret;
}
Пример #4
0
inline already_activated_error::already_activated_error(pcap_t *source)
    : error{"Capture source already activated\n" + error_string(source)} {}
Пример #5
0
inline void set_filter(pcap_t *source, bpf_program *filter_program) {
  if (pcap_setfilter(source, filter_program) == -1) {
    throw error{"Couldn't set filter\n" + error_string(source)};
  }
}
Пример #6
0
std::string error_checker::error_string()
{
    return (error_string(_error));
}
Пример #7
0
inline error::error(pcap_t *source) : error{error_string(source)} {}
Пример #8
0
void report_error(cl_int err){
  std::cout << error_string(err) << std::endl;
}
Пример #9
0
void report_error_pf(const char* pf, cl_int err){
  std::cout << pf << " : " << error_string(err) << std::endl;
}
Пример #10
0
void print_error(int e) {
  char* p;

  p = error_string(e);
  fprintf(stderr,"%s\n",p);
}
Пример #11
0
/* Start the service */
int start_service() {
  stopping = false;

  if (process_handle) return 0;

  /* Allocate a STARTUPINFO structure for a new process */
  STARTUPINFO si;
  ZeroMemory(&si, sizeof(si));
  si.cb = sizeof(si);

  /* Allocate a PROCESSINFO structure for the process */
  PROCESS_INFORMATION pi;
  ZeroMemory(&pi, sizeof(pi));

  /* Get startup parameters */
  char *env = 0;
  int ret = get_parameters(service_name, exe, sizeof(exe), flags, sizeof(flags), dir, sizeof(dir), &env, &throttle_delay);
  if (ret) {
    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_PARAMETERS_FAILED, service_name, 0);
    return stop_service(2, true, true);
  }

  /* Launch executable with arguments */
  char cmd[CMD_LENGTH];
  if (_snprintf(cmd, sizeof(cmd), "\"%s\" %s", exe, flags) < 0) {
    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "command line", "start_service", 0);
    return stop_service(2, true, true);
  }

  throttle_restart();

  if (! CreateProcess(0, cmd, 0, 0, false, 0, env, dir, &si, &pi)) {
    unsigned long error = GetLastError();
    if (error == ERROR_INVALID_PARAMETER && env) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED_INVALID_ENVIRONMENT, service_name, exe, NSSM_REG_ENV, 0);
    else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service_name, exe, error_string(error), 0);
    return stop_service(3, true, true);
  }
  process_handle = pi.hProcess;
  pid = pi.dwProcessId;

  if (get_process_creation_time(process_handle, &creation_time)) ZeroMemory(&creation_time, sizeof(creation_time));

  /* Signal successful start */
  service_status.dwCurrentState = SERVICE_RUNNING;
  SetServiceStatus(service_handle, &service_status);

  /* Wait for a clean startup. */
  if (WaitForSingleObject(process_handle, throttle_delay) == WAIT_TIMEOUT) throttle = 0;

  return 0;
}
Пример #12
0
int monitor_service() {
  /* Set service status to started */
  int ret = start_service();
  if (ret) {
    char code[16];
    _snprintf(code, sizeof(code), "%d", ret);
    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_START_SERVICE_FAILED, exe, service_name, ret, 0);
    return ret;
  }
  log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_STARTED_SERVICE, exe, flags, service_name, dir, 0);

  /* Monitor service service */
  if (! RegisterWaitForSingleObject(&wait_handle, process_handle, end_service, (void *) pid, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION)) {
    log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_REGISTERWAITFORSINGLEOBJECT_FAILED, service_name, exe, error_string(GetLastError()), 0);
  }

  return 0;
}
Пример #13
0
/* Make sure service recovery actions are taken where necessary */
void set_service_recovery(SC_HANDLE service, char *service_name) {
  SC_HANDLE services = 0;

  if (! service) {
    services = open_service_manager();
    if (! services) return;

    service = OpenService(services, service_name, SC_MANAGER_ALL_ACCESS);
    if (! service) return;
  }

  SERVICE_FAILURE_ACTIONS_FLAG flag;
  ZeroMemory(&flag, sizeof(flag));
  flag.fFailureActionsOnNonCrashFailures = true;

  /* This functionality was added in Vista so the call may fail */
  if (! ChangeServiceConfig2(service, SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, &flag)) {
    unsigned long error = GetLastError();
    /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */
    if (error != ERROR_INVALID_LEVEL) {
      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CHANGESERVICECONFIG2_FAILED, service_name, error_string(error), 0);
    }
  }

  if (services) {
    CloseServiceHandle(service);
    CloseServiceHandle(services);
  }
}
Пример #14
0
bool ChannelImpl::GetNextFrameFromBroker(amqp_frame_t& frame, boost::chrono::microseconds timeout)
{
    int socketno = amqp_get_sockfd(m_connection);

start:
    // Possibly set a timeout on receiving
    if (timeout != boost::chrono::microseconds::max() && !amqp_frames_enqueued(m_connection) && !amqp_data_in_buffer(m_connection))
    {
        struct timeval tv_timeout;
        memset(&tv_timeout, 0, sizeof(tv_timeout));

        // boost::chrono::seconds.count() returns boost::int_atleast64_t,
        // long can be 32 or 64 bit depending on the platform/arch
        // unless the timeout is something absurd cast to long will be ok, but
        // lets guard against the case where someone does something silly
        assert(boost::chrono::duration_cast<boost::chrono::seconds>(timeout).count() <
               static_cast<boost::chrono::seconds::rep>(std::numeric_limits<long>::max()));

        tv_timeout.tv_sec = static_cast<long>(boost::chrono::duration_cast<boost::chrono::seconds>(timeout).count());
        tv_timeout.tv_usec = static_cast<long>((timeout - boost::chrono::seconds(tv_timeout.tv_sec)).count());

        fd_set fds;
        FD_ZERO(&fds);
        FD_SET(static_cast<unsigned int>(socketno), &fds);

        int select_return = select(socketno + 1, &fds, NULL, &fds, &tv_timeout);

        if (select_return == 0) // If it times out, return
        {
            return false;
        }
        else if (select_return == -1)
        {
            // If its an interupted system call just try again
            if (errno == EINTR)
            {
                goto start;
            }
            else
            {
                std::string error_string("error calling select on socket: ");
#ifdef HAVE_STRERROR_S
                const int BUFFER_LENGTH = 256;
                char error_string_buffer[BUFFER_LENGTH] = {0};
                strerror_s(error_string_buffer, errno);
                error_string += error_string_buffer;
#elif defined(HAVE_STRERROR_R)
                const int BUFFER_LENGTH = 256;
                char error_string_buffer[BUFFER_LENGTH] = {0};
                strerror_r(errno, error_string_buffer, BUFFER_LENGTH);
                error_string += error_string_buffer;
#else
                error_string += strerror(errno);
#endif
                throw std::runtime_error(error_string.c_str());
            }
        }
    }

    CheckForError(amqp_simple_wait_frame(m_connection, &frame));
    return true;
}