Chinaunix首页 | 论坛 | 博客
  • 博客访问: 598558
  • 博文数量: 770
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 4905
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-19 14:36
文章分类

全部博文(770)

文章存档

2011年(1)

2008年(769)

我的朋友

分类:

2008-09-19 14:44:04

static char file_lock[sizeof(ctl_addr.sun_path)] = /var/run/file.pid;
  static bool file_lock_created = FALSE;
  
  static int
  create_lock(void)
  {
   int fd = open(file_lock, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
   S_IRUSR | S_IRGRP | S_IROTH);
  
   if (fd < 0)
   {
   if (errno == EEXIST)
   {
   fprintf(stderr, "file: lock file \"%s\" already exists\n", file_lock);
   exit_file(10);
   }
   else
   {
   fprintf(stderr, "file: unable to create lock file \"%s\" (%d %s)\n"
   , file_lock, errno, strerror(errno));
   exit_file(1);
   }
   }
   file_lock_created = TRUE;
   return fd;
  }
  
  static bool
  fill_lock(int lockfd)
  {
   char buf[30]; /* holds "\n" */
   pid_t pid;
   int len;
  
   pid = getpid();
   len = snprintf(buf, sizeof(buf), "%u\n", (unsigned int) pid);
   bool ok = len > 0 && write(lockfd, buf, len) == len;
  
   close(lockfd);
   return ok;
  }
  
  static void
  delete_lock(void)
  {
   if (file_lock_created)
   {
   //delete_ctl_socket();
   unlink(file_lock); /* is noting failure useful? */
   }
  }

--------------------next---------------------

阅读(266) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~