Exemple #1
0
tcmRet oalMsg_init(tcmEntityId eid, void **msgHandle)
{
   tcmMsgHandle *handle;
   const tcmEntityInfo *eInfo;
   struct sockaddr_un serverAddr;
   int rc;

   if( (eInfo = tcmEid_getEntityInfo(eid)) == NULL)
   {
      return TCMRET_INVALID_PARAMETER;
   }

   if( ( handle = malloc(sizeof(tcmMsgHandle))) == NULL )
   {
      printf( "handle = malloc(sizeof(tcmMsgHandle)))\n" );
      return TCMRET_INTERNAL_ERROR;
   }

   handle->eid = eid;

   //Socket Resource Creation
   handle->commFd = socket(AF_LOCAL, SOCK_STREAM, 0);
   if (handle->commFd < 0)
   {
      printf( "handle->commFd < 0\n" );
      free(handle);
      return TCMRET_INTERNAL_ERROR;
   }

   /*
    * Set close-on-exec, even though all apps should close their
    * fd's before fork and exec.
    */
   if ((rc = fcntl(handle->commFd, F_SETFD, FD_CLOEXEC)) != 0)
   {
      printf( "rc = fcntl(handle->commFd, F_SETFD, FD_CLOEXEC))\n" );
      close(handle->commFd);
      free(handle);
      return TCMRET_INTERNAL_ERROR;
   }

   /*
    * Connect to .
    * sched app  .
    */
   memset(&serverAddr, 0, sizeof(serverAddr));
   serverAddr.sun_family = AF_LOCAL;
   strncpy(serverAddr.sun_path, SCHED_MESSAGE_ADDR, sizeof(serverAddr.sun_path));

   rc = connect(handle->commFd, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
   if (rc != 0)
   {
      printf( "rc != 0\n" );
      close(handle->commFd);
      free(handle);
      return TCMRET_INTERNAL_ERROR;
   }

   /* send a launched message to sched */
   {
      tcmRet ret;
      tcmMsgHeader launchMsg = EMPTY_MSG_HEADER;

      launchMsg.type = TCM_MSG_APP_LAUNCHED;
      launchMsg.src = (eInfo->flags & APPCFG_MULTIPLE_INSTANCES ) ? MAKE_SPECIFIC_EID(getpid(), eid):eid;
      launchMsg.dst = ID_SCHED;
      launchMsg.flags_event = 1;

      if ((ret = oalMsg_send(handle->commFd, &launchMsg)) != TCMRET_SUCCESS)
      {
         printf( "(ret = oalMsg_send(handle->commFd, &launchMsg)) != TCMRET_SUCCESS\n" );
         close(handle->commFd);
         free(handle);
         return TCMRET_INTERNAL_ERROR;
      }
   }

   /* successful, set handle pointer */
   *msgHandle = (void *) handle;

   return TCMRET_SUCCESS;
}
CmsRet oalMsg_init(CmsEntityId eid, void **msgHandle)
{
   CmsMsgHandle *handle;
   const CmsEntityInfo *eInfo;
   struct sockaddr_un serverAddr;
   SINT32 rc;

   if ((eInfo = cmsEid_getEntityInfo(eid)) == NULL)
   {
      cmsLog_error("Invalid eid %d", eid);
      return CMSRET_INVALID_ARGUMENTS;
   }
   
   if ((handle = (CmsMsgHandle *) cmsMem_alloc(sizeof(CmsMsgHandle), ALLOC_ZEROIZE)) == NULL)
   {
      cmsLog_error("could not allocate storage for msg handle");
      return CMSRET_RESOURCE_EXCEEDED;
   }

   /* store caller's eid */
   handle->eid = eid;

#ifdef DESKTOP_LINUX
   /*
    * Applications may be run without smd on desktop linux, so if we
    * don't see a socket for smd, don't bother connecting to it.
    */
   {
      struct stat statbuf;

      if ((rc = stat(SMD_MESSAGE_ADDR, &statbuf)) < 0)
      {
         handle->commFd = CMS_INVALID_FD;
         handle->standalone = TRUE;
         *msgHandle = (void *) handle;
         cmsLog_notice("no smd server socket detected, running in standalone mode.");
         return CMSRET_SUCCESS;
      }
   }
#endif /* DESKTOP_LINUX */


      /*
       * Create a unix domain socket.
       */
      handle->commFd = socket(AF_LOCAL, SOCK_STREAM, 0);
      if (handle->commFd < 0)
      {
         cmsLog_error("Could not create socket");
         cmsMem_free(handle);
         return CMSRET_INTERNAL_ERROR;
      }


      /*
       * Set close-on-exec, even though all apps should close their
       * fd's before fork and exec.
       */
      if ((rc = fcntl(handle->commFd, F_SETFD, FD_CLOEXEC)) != 0)
      {
         cmsLog_error("set close-on-exec failed, rc=%d errno=%d", rc, errno);
         close(handle->commFd);
         cmsMem_free(handle);
         return CMSRET_INTERNAL_ERROR;
      }


      /*
       * Connect to smd.
       */
      memset(&serverAddr, 0, sizeof(serverAddr));
      serverAddr.sun_family = AF_LOCAL;
      strncpy(serverAddr.sun_path, SMD_MESSAGE_ADDR, sizeof(serverAddr.sun_path));

      rc = connect(handle->commFd, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
      if (rc != 0)
      {
         cmsLog_error("connect to %s failed, rc=%d errno=%d", SMD_MESSAGE_ADDR, rc, errno);
         close(handle->commFd);
         cmsMem_free(handle);
         return CMSRET_INTERNAL_ERROR;
      }
      else
      {
         cmsLog_debug("commFd=%d connected to smd", handle->commFd);
      }

      /* send a launched message to smd */
      {
         CmsRet ret;
         CmsMsgHeader launchMsg = EMPTY_MSG_HEADER;

         launchMsg.type = CMS_MSG_APP_LAUNCHED;
         launchMsg.src = (eInfo->flags & EIF_MULTIPLE_INSTANCES) ? MAKE_SPECIFIC_EID(getpid(), eid) : eid;
         launchMsg.dst = EID_SMD;
         launchMsg.flags_event = 1;

         if ((ret = oalMsg_send(handle->commFd, &launchMsg)) != CMSRET_SUCCESS)
         {
            close(handle->commFd);
            cmsMem_free(handle);
            return CMSRET_INTERNAL_ERROR;
         }
         else
         {
            cmsLog_debug("sent LAUNCHED message to smd");
         }
      }

   /* successful, set handle pointer */
   *msgHandle = (void *) handle;

   return CMSRET_SUCCESS;
}