rpcss_main.c 8.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2001, Ove Kven, TransGaming Technologies Inc.
 * Copyright 2002 Greg Turner
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 * ---- rpcss_main.c:
 *   Initialize and start serving requests.  Bail if rpcss already is
 *   running.
 *
 * ---- RPCSS.EXE:
 *   
 *   Wine needs a server whose role is somewhat like that
 *   of rpcss.exe in windows.  This is not a clone of
 *   windows rpcss at all.  It has been given the same name, however,
 *   to provide for the possibility that at some point in the future, 
 *   it may become interface compatible with the "real" rpcss.exe on
 *   Windows.
 *
 * ---- KNOWN BUGS / TODO:
 *
 *   o Service hooks are unimplemented (if you bother to implement
 *     these, also implement net.exe, at least for "net start" and
 *     "net stop" (should be pretty easy I guess, assuming the rest
 *     of the services API infrastructure works.
 * 
 *   o Is supposed to use RPC, not random kludges, to map endpoints.
 *
 *   o Probably name services should be implemented here as well.
 *
 *   o Wine's named pipes (in general) may not interoperate with those of 
 *     Windows yet (?)
 *
46 47
 *   o There is a looming problem regarding listening on privileged
 *     ports.  We will need to be able to coexist with SAMBA, and be able
48 49 50 51 52 53 54 55 56 57 58 59
 *     to function without running winelib code as root.  This may
 *     take some doing, including significant reconceptualization of the
 *     role of rpcss.exe in wine.
 *
 *   o Who knows?  Whatever rpcss does, we ought to at
 *     least think about doing... but what /does/ it do?
 */

#include <stdio.h>
#include <limits.h>
#include <assert.h>

60 61
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include "rpcss.h"
#include "winnt.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(ole);

static HANDLE master_mutex;
static long max_lazy_timeout = RPCSS_DEFAULT_MAX_LAZY_TIMEOUT;

HANDLE RPCSS_GetMasterMutex(void)
{
  return master_mutex;
}

void RPCSS_SetMaxLazyTimeout(long mlt)
{
  /* FIXME: this max ensures that no caller will decrease our wait time,
     but could have other bad results.  fix: Store "next_max_lazy_timeout" 
Francois Gouget's avatar
Francois Gouget committed
81
     and install it as necessary next time we "do work"? */
82 83 84 85 86 87 88 89 90 91 92 93
  max_lazy_timeout = max(RPCSS_GetLazyTimeRemaining(), mlt);
}

long RPCSS_GetMaxLazyTimeout(void)
{
  return max_lazy_timeout;
}

/* when do we just give up and bail? (UTC) */
static SYSTEMTIME lazy_timeout_time;

#if defined(NONAMELESSSTRUCT)
94
# define FILETIME_TO_ULARGEINT(filetime, ularge) \
95 96
    ( ularge.u.LowPart = filetime.dwLowDateTime, \
      ularge.u.HighPart = filetime.dwHighDateTime )
97
# define ULARGEINT_TO_FILETIME(ularge, filetime) \
98 99
    ( filetime.dwLowDateTime = ularge.u.LowPart, \
      filetime.dwHighDateTime = ularge.u.HighPart )
100
#else
101
# define FILETIME_TO_ULARGEINT(filetime, ularge) \
102 103
    ( ularge.LowPart = filetime.dwLowDateTime, \
      ularge.HighPart = filetime.dwHighDateTime )
104
# define ULARGEINT_TO_FILETIME(ularge, filetime) \
105 106 107 108
    ( filetime.dwLowDateTime = ularge.LowPart, \
      filetime.dwHighDateTime = ularge.HighPart )
#endif /* NONAMELESSSTRUCT */

109
#define TEN_MIL ((ULONGLONG)10000000)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

/* returns time remaining in seconds */
long RPCSS_GetLazyTimeRemaining(void)
{
  SYSTEMTIME st_just_now;
  FILETIME ft_jn, ft_ltt;
  ULARGE_INTEGER ul_jn, ul_ltt;

  GetSystemTime(&st_just_now);
  SystemTimeToFileTime(&st_just_now, &ft_jn);
  FILETIME_TO_ULARGEINT(ft_jn, ul_jn);

  SystemTimeToFileTime(&lazy_timeout_time, &ft_ltt);
  FILETIME_TO_ULARGEINT(ft_ltt, ul_ltt);
  
  if (ul_jn.QuadPart > ul_ltt.QuadPart)
    return 0;
  else
    return (ul_ltt.QuadPart - ul_jn.QuadPart) / TEN_MIL;
}

void RPCSS_SetLazyTimeRemaining(long seconds)
{
  SYSTEMTIME st_just_now;
  FILETIME ft_jn, ft_ltt;
  ULARGE_INTEGER ul_jn, ul_ltt;

  WINE_TRACE("(seconds == %ld)\n", seconds);

  assert(seconds >= 0);     /* negatives are not allowed */
  
  GetSystemTime(&st_just_now);
  SystemTimeToFileTime(&st_just_now, &ft_jn);
  FILETIME_TO_ULARGEINT(ft_jn, ul_jn);

  /* we want to find the time ltt, s.t. ltt = just_now + seconds */
  ul_ltt.QuadPart = ul_jn.QuadPart + seconds * TEN_MIL;

  /* great. just remember it */
  ULARGEINT_TO_FILETIME(ul_ltt, ft_ltt);
  if (! FileTimeToSystemTime(&ft_ltt, &lazy_timeout_time))
    assert(FALSE);
}

#undef FILETIME_TO_ULARGEINT
#undef ULARGEINT_TO_FILETIME
#undef TEN_MIL

158
static BOOL RPCSS_work(void)
159 160 161 162
{
  return RPCSS_NPDoWork();
}

163
static BOOL RPCSS_Empty(void)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
{
  BOOL rslt = TRUE;

  rslt = RPCSS_EpmapEmpty();

  return rslt;
}

BOOL RPCSS_ReadyToDie(void)
{
  long ltr = RPCSS_GetLazyTimeRemaining();
  LONG stc = RPCSS_SrvThreadCount();
  BOOL empty = RPCSS_Empty();
  return ( empty && (ltr <= 0) && (stc == 0) );
}

180
static BOOL RPCSS_Initialize(void)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
{
  WINE_TRACE("\n");

  master_mutex = CreateMutexA( NULL, FALSE, RPCSS_MASTER_MUTEX_NAME);
  if (!master_mutex) {
    WINE_ERR("Failed to create master mutex\n");
    return FALSE;
  }

  if (!RPCSS_BecomePipeServer()) {
    WINE_WARN("Server already running: exiting.\n");

    CloseHandle(master_mutex);
    master_mutex = NULL;

    return FALSE;
  }

  return TRUE;
}

/* returns false if we discover at the last moment that we
   aren't ready to terminate */
204
static BOOL RPCSS_Shutdown(void)
205 206 207 208 209 210 211
{
  if (!RPCSS_UnBecomePipeServer())
    return FALSE;
   
  if (!CloseHandle(master_mutex))
    WINE_WARN("Failed to release master mutex\n");

212
  master_mutex = NULL;
213 214 215 216

  return TRUE;
}

217
static void RPCSS_MainLoop(void)
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
{
  BOOL did_something_new;

  WINE_TRACE("\n");

  for (;;) {
    did_something_new = FALSE;

    while ( (! did_something_new) && (! RPCSS_ReadyToDie()) )
      did_something_new = (RPCSS_work() || did_something_new);

    if ((! did_something_new) && RPCSS_ReadyToDie())
      break; /* that's it for us */

    if (did_something_new)
      RPCSS_SetLazyTimeRemaining(max_lazy_timeout);
  }
}

237
static BOOL RPCSS_ProcessArgs( int argc, char **argv )
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
{
  int i;
  char *c,*c1;

  for (i = 1; i < argc; i++) {
    c = argv[i];
    while (*c == ' ') c++;
    if ((*c != '-') && (*c != '/'))
      return FALSE;
    c++;
    switch (*(c++)) {
      case 't':
      case 'T':
        while (*c == ' ') c++;
	if (*c == '\0')  {
	  /* next arg */
	  if (++i >= argc)
	    return FALSE;
	  c = argv[i];
	  while (*c == ' ') c++;
	  if (c == '\0')
	    return FALSE;
	} else
	  return FALSE;
        max_lazy_timeout = strtol(c, &c1, 0);
        if (c == c1)
	  return FALSE;
	c = c1;
	if (max_lazy_timeout <= 0)
	  return FALSE;
	if (max_lazy_timeout == LONG_MAX)
	  return FALSE;
	WINE_TRACE("read timeout argument: %ld\n", max_lazy_timeout);
	break;
      default: 
        return FALSE;
	break;
    }
    while (*c == ' ') c++;
    if (*c != '\0') return FALSE;
  }

  return TRUE;
}

283
static void RPCSS_Usage(void)
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
{
  printf("\nWine RPCSS\n");
  printf("\nsyntax: rpcss [-t timeout]\n\n");
  printf("  -t: rpcss (or the running rpcss process) will\n");
  printf("      execute with at least the specified timeout.\n");
  printf("\n");
}

int main( int argc, char **argv )
{
  /* 
   * We are invoked as a standard executable; we act in a
   * "lazy" manner.  We open up our pipe, and hang around until we have
   * nothing left to do, and then silently terminate.  When we're needed
   * again, rpcrt4.dll.so will invoke us automatically.
   */
       
  if (!RPCSS_ProcessArgs(argc, argv)) {
    RPCSS_Usage();
    return 1;
  }
      
  /* we want to wait for something to happen, and then 
     timeout when no activity occurs. */
  RPCSS_SetLazyTimeRemaining(max_lazy_timeout);

  if (RPCSS_Initialize()) {
    do
      RPCSS_MainLoop();
    while (!RPCSS_Shutdown());
  }

  return 0;
}