2 * Copyright (C) 1995-1997, 1999 Jeffrey A. Uphoff
3 * Modified by Olaf Kirch, 1996.
4 * Modified by H.J. Lu, 1998.
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...
28 extern short int restart;
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));
33 if ((size = read (fd, &MY_STATE, sizeof MY_STATE)) == -1)
34 die ("read (%s): %s", SM_STAT_PATH, strerror (errno));
36 if (size != 0 && size != sizeof MY_STATE) {
37 log (L_ERROR, "Error in status file format...correcting.");
40 die ("close (%s): %s", SM_STAT_PATH, strerror (errno));
42 if ((fd = creat (SM_STAT_PATH, S_IRUSR | S_IWUSR)) == -1)
43 die ("creat (%s): %s", SM_STAT_PATH, strerror (errno));
45 log (L_DEBUG, "New state: %u", (++MY_STATE % 2) ? MY_STATE : ++MY_STATE);
47 if (lseek (fd, 0, SEEK_SET) == -1)
48 die ("lseek (%s): %s", SM_STAT_PATH, strerror (errno));
50 if (write (fd, &MY_STATE, sizeof MY_STATE) != sizeof MY_STATE)
51 die ("write (%s): %s", SM_STAT_PATH, strerror (errno));
54 log (L_ERROR, "fsync (%s): %s", SM_STAT_PATH, strerror (errno));
57 log (L_ERROR, "close (%s): %s", SM_STAT_PATH, strerror (errno));
59 if (MY_NAME == NULL) {
60 char fullhost[SM_MAXSTRLEN + 1];
61 struct hostent *hostinfo;
63 if (gethostname (fullhost, SM_MAXSTRLEN) == -1)
64 die ("gethostname: %s", strerror (errno));
66 if ((hostinfo = gethostbyname (fullhost)) == NULL)
67 log (L_ERROR, "gethostbyname error for %s", fullhost);
69 strncpy (fullhost, hostinfo->h_name, sizeof (fullhost) - 1);
70 fullhost[sizeof (fullhost) - 1] = '\0';
73 MY_NAME = xstrdup (fullhost);
79 * Fairly traditional use of two directories for this.
90 if (stat (SM_DIR, &st) == -1 && errno != ENOENT)
91 die ("stat (%s): %s", SM_DIR, strerror (errno));
93 if (!S_ISDIR (st.st_mode))
94 if (mkdir (SM_DIR, S_IRWXU) == -1)
95 die ("mkdir (%s): %s", SM_DIR, strerror (errno));
97 memset (&st, 0, sizeof st);
99 if (stat (SM_BAK_DIR, &st) == -1 && errno != ENOENT)
100 die ("stat (%s): %s", SM_BAK_DIR, strerror (errno));
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));
106 if (!(nld = opendir (SM_DIR)))
107 die ("opendir (%s): %s", SM_DIR, strerror (errno));
110 len2=strlen(SM_BAK_DIR);
111 while ((de = readdir (nld))) {
112 if (de->d_name[0] == '.')
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));
124 if (closedir (nld) == -1)
125 log (L_ERROR, "closedir (%s): %s", SM_DIR, strerror (errno));