示例#1
0
   void setIPAddress(UIPAddress& cAddr)
      {
      U_TRACE(0, "SocketAddress::setIPAddress(%p)", &cAddr)

      addr.psaGeneric.sa_family = cAddr.getAddressFamily();

#  ifdef ENABLE_IPV6
      if (addr.psaGeneric.sa_family == AF_INET6)
         U_MEMCPY(&(addr.psaIP6Addr.sin6_addr), cAddr.get_in_addr(), cAddr.getInAddrLength());
      else
#  endif
         U_MEMCPY(&(addr.psaIP4Addr.sin_addr),  cAddr.get_in_addr(), cAddr.getInAddrLength());
      }
示例#2
0
   /*
   struct addrinfo {
      int ai_flags;              // Input flags
      int ai_family;             // Protocol family for socket
      int ai_socktype;           // Socket type
      int ai_protocol;           // Protocol for socket
      socklen_t ai_addrlen;      // Length of socket address
      struct sockaddr* ai_addr;  // Socket address for socket
      char* ai_canonname;        // Canonical name for service location
      struct addrinfo* ai_next;  // Pointer to next in list
   };
   */
   void set(struct addrinfo* result)
      {
      U_TRACE(0, "SocketAddress::set(%p)", result)

      addr.psaGeneric.sa_family = result->ai_family;

#  ifdef ENABLE_IPV6
      if (addr.psaGeneric.sa_family == AF_INET6)
         U_MEMCPY(&(addr.psaIP6Addr.sin6_addr),
                         &((struct sockaddr_in6*)result->ai_addr)->sin6_addr, sizeof(in6_addr));
      else
#  endif
         U_MEMCPY(&(addr.psaIP4Addr.sin_addr),
                         &((struct sockaddr_in*)result->ai_addr)->sin_addr,   sizeof(in_addr));
      }
示例#3
0
bool UDirWalk::setDirectory(const UString& dir, const char* _filter, uint32_t _filter_len)
{
   U_TRACE(0, "UDirWalk::setDirectory(%.*S,%.*S,%u)", U_STRING_TO_TRACE(dir), _filter_len, _filter, _filter_len)

   pthis->pathlen = dir.size();

   const char* pdir = u_getPathRelativ(dir.data(), &(pthis->pathlen));

   U_INTERNAL_ASSERT_MAJOR(pthis->pathlen, 0)

   U_MEMCPY(pthis->pathname, pdir, pthis->pathlen);

   pthis->pathname[pthis->pathlen] = '\0';

   if (UFile::access(pthis->pathname) == false)
      {
      pthis->pathlen = 0;

      U_RETURN(false);
      }

   setFilter(_filter, _filter_len);

   U_RETURN(true);
}
示例#4
0
ULog::ULog(const UString& path, uint32_t _size, const char* dir_log_gz) : UFile(path)
{
   U_TRACE_REGISTER_OBJECT(0, ULog, "%.*S,%u,%S", U_STRING_TO_TRACE(path), _size, dir_log_gz)

   log_gzip_sz                = 0;
   U_Log_start_stop_msg(this) = false;

   if (UFile::getPath().equal(U_CONSTANT_TO_PARAM("syslog")))
      {
      lock               = 0;
      ptr_log_data       = 0;
#  ifdef USE_LIBZ
      buf_path_compress  = 0;
#  endif
      U_Log_syslog(this) = true;

#  ifndef __MINGW32__
      U_SYSCALL_VOID(openlog, "%S,%d,%d", u_progname, LOG_PID, LOG_LOCAL0);
#  endif

      return;
      }

   if (UFile::creat(O_RDWR | O_APPEND, 0664) == false)
      {
      U_ERROR("cannot creat log file %.*S", U_FILE_TO_TRACE(*this));

      return;
      }

   ptr_log_data = U_MALLOC_TYPE(log_data);

   ptr_log_data->file_ptr = 0;

   if (_size)
      {
      uint32_t file_size = UFile::size();

      bool bsize = (file_size != _size);

      U_INTERNAL_DUMP("bsize = %b", bsize)

      if ((bsize && UFile::ftruncate(_size) == false) ||
          UFile::memmap(PROT_READ | PROT_WRITE) == false)
         {
         U_ERROR("cannot init log file %.*S", U_FILE_TO_TRACE(*this));

         return;
         }

      if (bsize) ptr_log_data->file_ptr = file_size; // append mode
      else
         {
         // NB: we can have a previous crash without resizing the file or we are an other process (apache like log)...

         char* ptr = (char*) u_find(UFile::map, file_size, U_CONSTANT_TO_PARAM(U_MARK_END));

         U_INTERNAL_DUMP("ptr = %p", ptr)

         if (ptr)
            {
            ptr_log_data->file_ptr = ptr - UFile::map;

            // NB: we can be an other process that manage this file (apache like log)...

            (void) memcpy(ptr, U_CONSTANT_TO_PARAM(U_MARK_END));

            UFile::msync(ptr + U_CONSTANT_SIZE(U_MARK_END), UFile::map, MS_SYNC);
            }

         U_INTERNAL_ASSERT_MINOR(ptr_log_data->file_ptr, UFile::st_size)
         }

      log_file_sz = UFile::st_size;
      }

   U_INTERNAL_DUMP("ptr_log_data->file_ptr = %u UFile::st_size = %u log_gzip_sz = %u", ptr_log_data->file_ptr, UFile::st_size, log_gzip_sz)

   U_INTERNAL_ASSERT(ptr_log_data->file_ptr <= UFile::st_size)

   lock                    = U_NEW(ULock);
   U_Log_syslog(this)      = false;
   ptr_log_data->gzip_len  = 0;
   ptr_log_data->file_page = ptr_log_data->file_ptr;

#ifdef USE_LIBZ
   char suffix[32];
   uint32_t len_suffix = u__snprintf(suffix, sizeof(suffix), ".%4D.gz");

   buf_path_compress = U_NEW(UString(MAX_FILENAME_LEN));

   char* ptr = buf_path_compress->data();

   if (dir_log_gz == 0)
      {
      (void) UFile::setPathFromFile(*this, ptr, suffix, len_suffix);

      buf_path_compress->size_adjust();

      index_path_compress = (buf_path_compress->size() - len_suffix + 1);
      }
   else
      {
      UString name = UFile::getName();
      uint32_t len = u__strlen(dir_log_gz, __PRETTY_FUNCTION__), sz = name.size();

      U_MEMCPY(ptr, dir_log_gz, len);

       ptr  += len;
      *ptr++ = '/';

      buf_path_compress->size_adjust(len + 1 + sz + len_suffix);

      U_MEMCPY(ptr, name.data(), sz);
                ptr += sz;
      U_MEMCPY(ptr, suffix, len_suffix);

      index_path_compress = buf_path_compress->distance(ptr) + 1;

      buf_path_compress->UString::setNullTerminated();
      }

   U_INTERNAL_DUMP("buf_path_compress(%u) = %.*S index_path_compress = %u",
                    buf_path_compress->size(), U_STRING_TO_TRACE(*buf_path_compress), index_path_compress)
#endif
}
示例#5
0
ULog::ULog(const UString& path, uint32_t _size, const char* dir_log_gz) : UFile(path, 0)
{
   U_TRACE_REGISTER_OBJECT(0, ULog, "%V,%u,%S", path.rep, _size, dir_log_gz)

   lock         = 0;
   ptr_log_data = 0;
   log_file_sz  =
   log_gzip_sz  = 0;

   U_Log_start_stop_msg(this) = false;

#ifdef USE_LIBZ
     buf_path_compress = 0;
   index_path_compress = 0;
#endif

   if (UFile::getPath().equal(U_CONSTANT_TO_PARAM("syslog")))
      {
      U_Log_syslog(this) = true;

#  ifndef _MSWINDOWS_
      openlog(u_progname, LOG_PID, LOG_LOCAL0);
#  endif

      return;
      }

   if (UFile::creat(O_RDWR | O_APPEND, 0664) == false)
      {
#  ifndef U_COVERITY_FALSE_POSITIVE
      U_ERROR("Cannot creat log file %.*S", U_FILE_TO_TRACE(*this));
#  endif

      return;
      }

   /**
    * typedef struct log_data {
    *  uint32_t file_ptr;
    *  uint32_t file_page;
    *  uint32_t gzip_len;
    *  sem_t lock_shared;
    *  char spinlock_shared[1];
    *  // --------------> maybe unnamed array of char for gzip compression...
    * } log_data;
    */

   ptr_log_data = U_MALLOC_TYPE(log_data);

   ptr_log_data->file_ptr = 0;

   if (_size)
      {
      uint32_t file_size = UFile::size();

      bool bsize = (file_size != _size);

      if ((bsize && UFile::ftruncate(_size) == false) ||
          UFile::memmap(PROT_READ | PROT_WRITE) == false)
         {
         U_ERROR("Cannot init log file %.*S", U_FILE_TO_TRACE(*this));

         return;
         }

      if (bsize) ptr_log_data->file_ptr = file_size; // append mode
      else
         {
         // NB: we can have a previous crash without resizing the file or we are an other process (apache like log)...

         char* ptr = (char*) u_find(UFile::map, file_size, U_CONSTANT_TO_PARAM(U_MARK_END));

         if (ptr)
            {
            ptr_log_data->file_ptr = ptr - UFile::map;

            // NB: we can be an other process that manage this file (apache like log)...

            u_put_unalignedp64(ptr,    U_MULTICHAR_CONSTANT64('\n','\n','\n','\n','\n','\n','\n','\n'));
            u_put_unalignedp64(ptr+8,  U_MULTICHAR_CONSTANT64('\n','\n','\n','\n','\n','\n','\n','\n'));
            u_put_unalignedp64(ptr+16, U_MULTICHAR_CONSTANT64('\n','\n','\n','\n','\n','\n','\n','\n'));

            UFile::msync(ptr + U_CONSTANT_SIZE(U_MARK_END), UFile::map, MS_SYNC);
            }

         U_INTERNAL_ASSERT_MINOR(ptr_log_data->file_ptr, UFile::st_size)
         }

      log_file_sz = UFile::st_size;
      }

   U_INTERNAL_ASSERT(ptr_log_data->file_ptr <= UFile::st_size)

   U_NEW(ULock, lock, ULock);

   U_Log_syslog(this)      = false;
   ptr_log_data->gzip_len  = 0;
   ptr_log_data->file_page = ptr_log_data->file_ptr;

#ifdef USE_LIBZ
   char suffix[32];
   uint32_t len_suffix = u__snprintf(suffix, sizeof(suffix), U_CONSTANT_TO_PARAM(".%4D.gz"));

   U_NEW(UString, buf_path_compress, UString(MAX_FILENAME_LEN));

   char* ptr = buf_path_compress->data();

   if (dir_log_gz == 0)
      {
#  ifndef U_COVERITY_FALSE_POSITIVE // Uninitialized pointer read (UNINIT)
      (void) UFile::setPathFromFile(*this, ptr, suffix, len_suffix);
#  endif

      buf_path_compress->size_adjust();

      index_path_compress = (buf_path_compress->size() - len_suffix + 1);
      }
   else
      {
      UString name = UFile::getName();
      uint32_t len = u__strlen(dir_log_gz, __PRETTY_FUNCTION__), sz = name.size();

      U_MEMCPY(ptr, dir_log_gz, len);

       ptr  += len;
      *ptr++ = '/';

      buf_path_compress->size_adjust(len + 1 + sz + len_suffix);

      U_MEMCPY(ptr, name.data(), sz);
               ptr +=            sz;
      U_MEMCPY(ptr, suffix, len_suffix);

      index_path_compress = buf_path_compress->distance(ptr) + 1;
      }
#endif
}