Logo Search packages:      
Sourcecode: pcsc-lite version File versions  Download package

LONG SCardEstablishContext ( DWORD  dwScope,
LPCVOID  pvReserved1,
LPCVOID  pvReserved2,
LPSCARDCONTEXT  phContext 
)

Creates an Application Context to the PC/SC Resource Manager.

This must be the first WinSCard function called in a PC/SC application. Each thread of an application shall use its own SCARDCONTEXT.

Parameters:
[in]dwScopeScope of the establishment. This can either be a local or remote connection.

[in]pvReserved1Reserved for future use.
[in]pvReserved2Reserved for future use.
[out]phContextReturned Application Context.
Returns:
Connection status.
Return values:
SCARD_S_SUCCESSSuccessful (SCARD_S_SUCCESS)
SCARD_E_INVALID_PARAMETERphContext is null (SCARD_E_INVALID_PARAMETER)
SCARD_E_INVALID_VALUEInvalid scope type passed (SCARD_E_INVALID_VALUE )
SCARD_E_NO_MEMORYThere is no free slot to store hContext (SCARD_E_NO_MEMORY)
SCARD_E_NO_SERVICEThe server is not runing (SCARD_E_NO_SERVICE)
SCARD_F_COMM_ERRORAn internal communications error has been detected (SCARD_F_COMM_ERROR)
SCARD_F_INTERNAL_ERRORAn internal consistency check failed (SCARD_F_INTERNAL_ERROR)
 SCARDCONTEXT hContext;
 LONG rv;
 ...
 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);

Definition at line 443 of file winscard_clnt.c.

References PCSCLITE_SVC_IDENTITY, SCARD_E_INVALID_HANDLE, SCARD_E_INVALID_VALUE, SCARD_E_NO_SERVICE, SCARD_F_INTERNAL_ERROR, SCARD_S_SUCCESS, SCARD_SCOPE_GLOBAL, SCARD_SCOPE_SYSTEM, SCARD_SCOPE_TERMINAL, SCARD_SCOPE_USER, SCardCheckDaemonAvailability(), SCardEstablishContextTH(), SCardLockThread(), SCardUnlockThread(), and SYS_USleep().

{
      LONG rv;
      int daemon_launched = FALSE;
      int retries = 0;

      API_TRACE_IN("%ld, %p, %p", dwScope, pvReserved1, pvReserved2)
      PROFILE_START

again:
      /* Check if the server is running */
      rv = SCardCheckDaemonAvailability();
      if (SCARD_E_INVALID_HANDLE == rv)
            /* we reconnected to a daemon or we got called from a forked child */
            rv = SCardCheckDaemonAvailability();

      if (SCARD_E_NO_SERVICE == rv)
      {
launch:
            if (daemon_launched)
            {
                  retries++;
                  if (retries < 50) /* 50 x 100ms = 5 seconds */
                  {
                        /* give some more time to the server to start */
                        SYS_USleep(100*1000);   /* 100 ms */
                        goto again;
                  }

                  /* the server failed to start (in time) */
                  goto end;
            }
            else
            {
                  int pid;

                  pid = fork();

                  if (pid < 0)
                  {
                        Log2(PCSC_LOG_CRITICAL, "fork failed: %s", strerror(errno));
                        rv = SCARD_F_INTERNAL_ERROR;
                        goto end;
                  }

                  if (0 == pid)
                  {
                        int i, max;
                        char *param = getenv("PCSCLITE_PCSCD_ARGS");

                        /* close all file handles except stdin, stdout and
                         * stderr so that pcscd does not confiscate ressources
                         * allocated by the application */
                        max = sysconf(_SC_OPEN_MAX);
                        if (-1 == max)
                              max = 1024;
                        for (i=3; i<max; i++)
                              (void)close(i);

                        /* son process */
                        execl(PCSCD_BINARY, "pcscd", "--auto-exit", param,
                              (char *)NULL);
                        Log2(PCSC_LOG_CRITICAL, "exec " PCSCD_BINARY " failed: %s",
                              strerror(errno));
                        exit(1);
                  }

                  /* father process */
                  daemon_launched = TRUE;

                  if (waitpid(pid, NULL, 0) < 0)
                        Log2(PCSC_LOG_CRITICAL, "waitpid failed: %s", strerror(errno));

                  goto again;
            }
      }

      if (rv != SCARD_S_SUCCESS)
            goto end;

      (void)SCardLockThread();
      rv = SCardEstablishContextTH(dwScope, pvReserved1,
            pvReserved2, phContext);
      (void)SCardUnlockThread();

      /* SCardEstablishContextTH may fail if the previous pcscd crashed
       * without cleaning /var/run/pcscd/pcscd.comm */
      if (SCARD_E_NO_SERVICE == rv)
      {
            retries++;
            if (retries <= 1)
                  goto launch;
      }

end:
      PROFILE_END(rv)
      API_TRACE_OUT("%ld", *phContext)

      return rv;
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index