Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. void _set_processing_status(const char* status)
  2.         {
  3.         if ( ! proc_status_file )
  4.                 return;
  5.  
  6.         // This function can be called from a signal context, so we have to
  7.         // make sure to only call reentrant functions and to restore errno
  8.         // afterwards.
  9.  
  10.         int old_errno = errno;
  11.  
  12.         int fd = open(proc_status_file, O_CREAT | O_WRONLY | O_TRUNC, 0700);
  13.  
  14.         if ( fd < 0 )
  15.                 {
  16.                 char buf[256];
  17.                 bro_strerror_r(errno, buf, sizeof(buf));
  18.  
  19.                 string open_err = "Failed to open process status file '";
  20.                         open_err += proc_status_file;
  21.                         open_err += "': ";
  22.                         open_err += buf;
  23.                         open_err += "\n";
  24.                 int fd_err_len = strlen(open_err.c_str());
  25.                 while ( fd_err_len )
  26.                         {
  27.                         int n = write(STDERR_FILENO, open_err.c_str(), fd_err_len);
  28.                         if ( n < 0 && errno != EINTR && errno != EAGAIN )
  29.                                 break;
  30.                         open_err += n;
  31.                         fd_err_len -= n;
  32.                         }
  33.  
  34.                 errno = old_errno;
  35.                 return;
  36.                 }
  37.  
  38.         int len = strlen(status);
  39.         while ( len )
  40.                 {
  41.                 int n = write(fd, status, len);
  42.  
  43.                 if ( n < 0 && errno != EINTR && errno != EAGAIN )
  44.                         // Ignore errors, as they're too difficult to
  45.                         // safely report here.
  46.                         break;
  47.  
  48.                 status += n;
  49.                 len -= n;
  50.                 }
  51.  
  52.         if ( close(fd) < 0 && errno != EINTR )
  53.                 {
  54.                 char buf[128];
  55.                 bro_strerror_r(errno, buf, sizeof(buf));
  56.                 string close_err = "close() error ";
  57.                         close_err += errno;
  58.                         close_err += ": ";
  59.                         close_err += buf;
  60.                         close_err += "\n";
  61.                 int fd_cls_err_len = strlen(close_err.c_str());
  62.                 while ( fd_cls_err_len )
  63.                         {
  64.                         int n = write(STDERR_FILENO, close_err.c_str(), fd_cls_err_len);
  65.                         if ( n < 0 && errno != EINTR && errno != EAGAIN )
  66.                                 break;
  67.                         close_err += n;
  68.                         fd_cls_err_len -= n;
  69.                         }
  70.                 abort();
  71.                 }
  72.  
  73.         errno = old_errno;
  74.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement