]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/state.c
Haircut.
[nfs-utils.git] / utils / statd / state.c
1 /*
2  * Copyright (C) 1995-1997, 1999 Jeffrey A. Uphoff
3  * Modified by Olaf Kirch, 1996.
4  * Modified by H.J. Lu, 1998.
5  *
6  * NSM for Linux.
7  */
8
9 #include "config.h"
10 #include <dirent.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <netdb.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <sys/stat.h>
17 #include "statd.h"
18
19
20 /* 
21  * Most NSM's keep the status number in an ASCII file.  I'm keeping it
22  * as an int (4-byte binary) for now...
23  */
24 void
25 change_state (void)
26 {
27   int fd, size;
28   extern short int restart;
29   
30   if ((fd = open (SM_STAT_PATH, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) == -1)
31     die ("open (%s): %s", SM_STAT_PATH, strerror (errno));
32
33   if ((size = read (fd, &MY_STATE, sizeof MY_STATE)) == -1)
34     die ("read (%s): %s", SM_STAT_PATH, strerror (errno));
35
36   if (size != 0 && size != sizeof MY_STATE) {
37     log (L_ERROR, "Error in status file format...correcting.");
38
39     if (close (fd) == -1)
40       die ("close (%s): %s", SM_STAT_PATH, strerror (errno));
41
42     if ((fd = creat (SM_STAT_PATH, S_IRUSR | S_IWUSR)) == -1)
43       die ("creat (%s): %s", SM_STAT_PATH, strerror (errno));
44   }
45   log (L_DEBUG, "New state: %u", (++MY_STATE % 2) ? MY_STATE : ++MY_STATE);
46
47   if (lseek (fd, 0, SEEK_SET) == -1)
48     die ("lseek (%s): %s", SM_STAT_PATH, strerror (errno));
49
50   if (write (fd, &MY_STATE, sizeof MY_STATE) != sizeof MY_STATE)
51     die ("write (%s): %s", SM_STAT_PATH, strerror (errno));
52
53   if (fsync (fd) == -1)
54     log (L_ERROR, "fsync (%s): %s", SM_STAT_PATH, strerror (errno));
55
56   if (close (fd) == -1)
57     log (L_ERROR, "close (%s): %s", SM_STAT_PATH, strerror (errno));
58
59   if (MY_NAME == NULL) {
60     char fullhost[SM_MAXSTRLEN + 1];
61     struct hostent *hostinfo;
62
63     if (gethostname (fullhost, SM_MAXSTRLEN) == -1)
64       die ("gethostname: %s", strerror (errno));
65
66     if ((hostinfo = gethostbyname (fullhost)) == NULL)
67       log (L_ERROR, "gethostbyname error for %s", fullhost);
68     else {
69       strncpy (fullhost, hostinfo->h_name, sizeof (fullhost) - 1);
70       fullhost[sizeof (fullhost) - 1] = '\0';
71     }
72
73     MY_NAME = xstrdup (fullhost);
74   }
75 }
76
77
78 /* 
79  * Fairly traditional use of two directories for this.
80  */
81 void 
82 shuffle_dirs (void)
83 {
84   DIR *nld;
85   struct dirent *de;
86   struct stat st;
87   char *src, *dst;
88   int len1, len2, len;
89   
90   if (stat (SM_DIR, &st) == -1 && errno != ENOENT)
91     die ("stat (%s): %s", SM_DIR, strerror (errno));
92
93   if (!S_ISDIR (st.st_mode))
94     if (mkdir (SM_DIR, S_IRWXU) == -1)
95       die ("mkdir (%s): %s", SM_DIR, strerror (errno));
96
97   memset (&st, 0, sizeof st);
98
99   if (stat (SM_BAK_DIR, &st) == -1 && errno != ENOENT)
100     die ("stat (%s): %s", SM_BAK_DIR, strerror (errno));
101
102   if (!S_ISDIR (st.st_mode))
103     if (mkdir (SM_BAK_DIR, S_IRWXU) == -1)
104       die ("mkdir (%s): %s", SM_BAK_DIR, strerror (errno));
105
106   if (!(nld = opendir (SM_DIR)))
107     die ("opendir (%s): %s", SM_DIR, strerror (errno));
108
109   len1=strlen(SM_DIR);
110   len2=strlen(SM_BAK_DIR);
111   while ((de = readdir (nld))) {
112     if (de->d_name[0] == '.')
113       continue;
114     len=strlen(de->d_name);
115     src=xmalloc(len1+len+2);
116     dst=xmalloc(len2+len+2);
117     sprintf (src, "%s/%s", SM_DIR, de->d_name);
118     sprintf (dst, "%s/%s", SM_BAK_DIR, de->d_name);
119     if (rename (src, dst) == -1)
120       die ("rename (%s to %s): %s", SM_DIR, SM_BAK_DIR, strerror (errno));
121     free(src);
122     free(dst);
123   }
124   if (closedir (nld) == -1)
125     log (L_ERROR, "closedir (%s): %s", SM_DIR, strerror (errno));
126 }