Exemple #1
0
int main(int argc, char **argv)
{
  int passwd = 0, nprocs, nuids;
  struct process *plist;
  uid_t *approved;

  if (argc == 2 && strcmp(argv[1], "-passwd") == 0)
    passwd = 1;
  else if (argc != 1 && (argc != 2 || strcmp(argv[1], "-loggedin") != 0))
    {
      fprintf(stderr, "Usage: %s [-passwd]\n", argv[0]);
      return 1;
    }

  /* Fetch the process list and list of approved uids.  Also cleans up
   * sessions if -passwd not specified. */
  plist = get_processes(&nprocs);
  if (passwd)
    approved = get_passwd_uids(&nuids);
  else
      approved = get_logged_in_uids(&nuids, plist, nprocs);

  /* Sort the approved uid list for fast search. */
  qsort(approved, nuids, sizeof(uid_t), uidcomp);

  kill_processes(plist, nprocs, approved, nuids);
  return 0;
}
Exemple #2
0
int main( int argc, char *argv[] )
{
    extern HANDLE CDECL __wine_make_process_system(void);
    static const WCHAR RunW[] = {'R','u','n',0};
    static const WCHAR RunOnceW[] = {'R','u','n','O','n','c','e',0};
    static const WCHAR RunServicesW[] = {'R','u','n','S','e','r','v','i','c','e','s',0};
    static const WCHAR RunServicesOnceW[] = {'R','u','n','S','e','r','v','i','c','e','s','O','n','c','e',0};
    static const WCHAR wineboot_eventW[] = {'_','_','w','i','n','e','b','o','o','t','_','e','v','e','n','t',0};

    /* First, set the current directory to SystemRoot */
    int optc;
    int end_session = 0, force = 0, init = 0, kill = 0, restart = 0, shutdown = 0, update = 0;
    HANDLE event;
    SECURITY_ATTRIBUTES sa;
    BOOL is_wow64;

    GetWindowsDirectoryW( windowsdir, MAX_PATH );
    if( !SetCurrentDirectoryW( windowsdir ) )
        WINE_ERR("Cannot set the dir to %s (%d)\n", wine_dbgstr_w(windowsdir), GetLastError() );

    if (IsWow64Process( GetCurrentProcess(), &is_wow64 ) && is_wow64)
    {
        STARTUPINFOW si;
        PROCESS_INFORMATION pi;
        WCHAR filename[MAX_PATH];
        void *redir;
        DWORD exit_code;

        memset( &si, 0, sizeof(si) );
        si.cb = sizeof(si);
        GetModuleFileNameW( 0, filename, MAX_PATH );

        Wow64DisableWow64FsRedirection( &redir );
        if (CreateProcessW( filename, GetCommandLineW(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
        {
            WINE_TRACE( "restarting %s\n", wine_dbgstr_w(filename) );
            WaitForSingleObject( pi.hProcess, INFINITE );
            GetExitCodeProcess( pi.hProcess, &exit_code );
            ExitProcess( exit_code );
        }
        else WINE_ERR( "failed to restart 64-bit %s, err %d\n", wine_dbgstr_w(filename), GetLastError() );
        Wow64RevertWow64FsRedirection( redir );
    }

    while ((optc = getopt_long(argc, argv, short_options, long_options, NULL )) != -1)
    {
        switch(optc)
        {
        case 'e': end_session = 1; break;
        case 'f': force = 1; break;
        case 'i': init = 1; break;
        case 'k': kill = 1; break;
        case 'r': restart = 1; break;
        case 's': shutdown = 1; break;
        case 'u': update = 1; break;
        case 'h': usage(); return 0;
        case '?': usage(); return 1;
        }
    }

    if (end_session)
    {
        if (kill)
        {
            if (!shutdown_all_desktops( force )) return 1;
        }
        else if (!shutdown_close_windows( force )) return 1;
    }

    if (kill) kill_processes( shutdown );

    if (shutdown) return 0;

    sa.nLength = sizeof(sa);
    sa.lpSecurityDescriptor = NULL;
    sa.bInheritHandle = TRUE;  /* so that services.exe inherits it */
    event = CreateEventW( &sa, TRUE, FALSE, wineboot_eventW );

    ResetEvent( event );  /* in case this is a restart */

    create_hardware_registry_keys();
    create_dynamic_registry_keys();
    create_environment_registry_keys();
    wininit();
    pendingRename();

    ProcessWindowsFileProtection();
    ProcessRunKeys( HKEY_LOCAL_MACHINE, RunServicesOnceW, TRUE, FALSE );

    if (init || (kill && !restart))
    {
        ProcessRunKeys( HKEY_LOCAL_MACHINE, RunServicesW, FALSE, FALSE );
        start_services_process();
    }
    if (init || update) update_wineprefix( update );

    create_volatile_environment_registry_key();

    ProcessRunKeys( HKEY_LOCAL_MACHINE, RunOnceW, TRUE, TRUE );

    if (!init && !restart)
    {
        ProcessRunKeys( HKEY_LOCAL_MACHINE, RunW, FALSE, FALSE );
        ProcessRunKeys( HKEY_CURRENT_USER, RunW, FALSE, FALSE );
        ProcessStartupItems();
    }

    WINE_TRACE("Operation done\n");

    SetEvent( event );
    return 0;
}
Exemple #3
0
int main(int argc, char **argv) {
  int rc;
  pid_t pid;
  int i;

  options_t *opts = get_options(argc, argv);

  char *fullname = NULL;
  asprintf(&fullname, "%s:", opts->name ? opts->name : argv[0]);
  argv[0] = fullname;

  if(opts->detached) {
    pid = fork();
    if(pid == 0) {
      rc = setsid();
      if(rc == -1) {
        perror("setsid");
        return EXIT_FAILURE;
      }
    } else if(pid == -1) {
      perror("fork");
      return EXIT_FAILURE;
    } else {
      return EXIT_SUCCESS;
    }
  }

  if(opts->group) {
    struct group  *grp = getgrnam(opts->group);
    if(grp == NULL) {
      fprintf(stderr, "getgrnam(%s): Couldn't find group\n", opts->group);
      return EXIT_FAILURE;
    }

    rc = setgid(grp->gr_gid);
    if(rc != 0) {
      fprintf(stderr, "setgid(%d): Couldn't set the group ID\n", grp->gr_gid);
      return EXIT_FAILURE;
    }

    rc = initgroups(opts->group, grp->gr_gid);
    if(rc == -1) {
      fprintf(stderr, "initgroups(%s, %d): You must run the program as root\n",
              opts->group, grp->gr_gid);
      return EXIT_FAILURE;
    }
  }

  if(opts->user) {
    struct passwd *pwd = getpwnam(opts->user);
    if(pwd == NULL) {
      fprintf(stderr, "getpwnam(%s): Couldn't find user\n", opts->user);
      return EXIT_FAILURE;
    }

    rc = setuid(pwd->pw_uid);
    if(rc != 0) {
      fprintf(stderr, "setuid(%d): Couldn't set the user ID\n", pwd->pw_uid);
      return EXIT_FAILURE;
    }
  }

  char **versions;
  numver = get_versions(opts->dir, &versions);

  pids = (pid_t*)calloc(numver, sizeof(pid_t));

  for(i = 0; i < numver; i++) {
    pid = fork();

    switch(pid) {
    case -1:
      perror("fork");
      kill_processes(SIGTERM);
      return EXIT_FAILURE;
    case 0:
      rc = start_process(opts, versions[i]);
      if(rc == -1) {
        /* This is in a child process that might have a "smaller" version of the
         * list of PIDs. Some processes might then not be killed here.
         * This is probably good enough corresponding the scope of this program.
         */
        kill_processes(SIGTERM);
        return EXIT_FAILURE;
      }
    default:
      pids[i] = pid;
    }
  }

  init_sighandler();

  asprintf(&argv[1], "%i/%i remaining processes", numver, numver);
  argv[2] = NULL;

  int status;
  while((pid = wait(&status)) != -1 || errno == EINTR) {
    if(WIFEXITED(status) || WIFSIGNALED(status)) {
      int nrem = 0;
      for(i = 0; i < numver; i++) {
        if(pids[i] == pid) { pids[i] = 0; }
        if(pids[i] != 0)   { nrem++; }
      }
      asprintf(&argv[1], "%i/%i remaining processes", nrem, numver);
      if(nrem == 0) {
        break;
      }
    }
  }

  free(fullname);
  free_options(opts);
  return EXIT_SUCCESS;
}