forked from rpodgorny/xotd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
leave_pid.c
117 lines (96 loc) · 2.36 KB
/
leave_pid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define _GNU_SOURCE
#include "leave_pid.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
static const char *name_pidfile = "/etc/pad_svr/xotd.pid";
extern char *Myname;
void
leave_pid(){
clear_pid();
int pidfd;
if ((pidfd = open(name_pidfile, O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
Printf(0,"Cant create pid file %s\n",name_pidfile);
Perror("");
exit(1);
}
char *buf;
asprintf(&buf,"%i",getpid());
//printf("i have pid %s\n",buf);
if (write(pidfd,buf,strlen(buf)) < 0) {
Perror("Cant write pid to pid file.");
exit(1);
}
free(buf);
if (close(pidfd) != 0) {
Perror("Cant close pid file.");
exit(1);
}
}
void
clear_pid(){
if (unlink(name_pidfile) != 0 && errno != ENOENT) {
Printf(0,"Cant unlink pid file %s\n",name_pidfile);
Perror("\t");
}
}
//exit kod 0 - process do not running
int
exist_pid(){
int pidfd;
if ((pidfd = open(name_pidfile,O_RDONLY)) < 0) {
//pid file not exist
return(0);
}
char buf[30];
int c;
if ( (c = read(pidfd,buf,sizeof(buf))) < 0) {
Perror("Cant read pid file.");
exit(1);
}
*(buf + c) = 0;
//printf("Read prev pid %s\n",buf);
c = atoi(buf);
if (close(pidfd) != 0) {
Perror("Cant close pid file.");
exit(1);
}
char *procnm;
asprintf(&procnm,"/proc/%s/status",buf);
//printf("file status :%s\n",procnm);
FILE *stafile;
if ((stafile = fopen(procnm,"r")) == NULL) {
//printf("cant open %s\n",procnm);
//process with pid not exist
return(0);
}
free(procnm);
if (fgets(buf,sizeof(buf),stafile) == NULL) {
//empty file
return(0);
}
fclose(stafile);
//printf ("readed from pid file %s, c: %i\n",buf,c);
if (strstr(buf,Myname) == NULL) {
//pid = other process
//printf("working other process\n");
return(0);
}
return(c);
}