2 * Copyright (C) 1995-1997, 1999 Jeffrey A. Uphoff
3 * Modified by Olaf Kirch, 1996.
4 * Modified by H.J. Lu, 1998.
24 * Most NSM's keep the status number in an ASCII file. I'm keeping it
25 * as an int (4-byte binary) for now...
31 extern short int restart;
33 if ((fd = open (SM_STAT_PATH, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) == -1)
34 die ("open (%s): %s", SM_STAT_PATH, strerror (errno));
36 if ((size = read (fd, &MY_STATE, sizeof MY_STATE)) == -1)
37 die ("read (%s): %s", SM_STAT_PATH, strerror (errno));
39 if (size != 0 && size != sizeof MY_STATE) {
40 note (N_ERROR, "Error in status file format...correcting.");
43 die ("close (%s): %s", SM_STAT_PATH, strerror (errno));
45 if ((fd = creat (SM_STAT_PATH, S_IRUSR | S_IWUSR)) == -1)
46 die ("creat (%s): %s", SM_STAT_PATH, strerror (errno));
48 note (N_DEBUG, "New state: %u", (++MY_STATE % 2) ? MY_STATE : ++MY_STATE);
50 if (lseek (fd, 0, SEEK_SET) == -1)
51 die ("lseek (%s): %s", SM_STAT_PATH, strerror (errno));
53 if (write (fd, &MY_STATE, sizeof MY_STATE) != sizeof MY_STATE)
54 die ("write (%s): %s", SM_STAT_PATH, strerror (errno));
57 note (N_ERROR, "fsync (%s): %s", SM_STAT_PATH, strerror (errno));
60 note (N_ERROR, "close (%s): %s", SM_STAT_PATH, strerror (errno));
62 if (MY_NAME == NULL) {
63 char fullhost[SM_MAXSTRLEN + 1];
64 struct hostent *hostinfo;
66 if (gethostname (fullhost, SM_MAXSTRLEN) == -1)
67 die ("gethostname: %s", strerror (errno));
69 if ((hostinfo = gethostbyname (fullhost)) == NULL)
70 note (N_ERROR, "gethostbyname error for %s", fullhost);
72 strncpy (fullhost, hostinfo->h_name, sizeof (fullhost) - 1);
73 fullhost[sizeof (fullhost) - 1] = '\0';
76 MY_NAME = xstrdup (fullhost);
82 * Fairly traditional use of two directories for this.
93 if (stat (SM_DIR, &st) == -1 && errno != ENOENT)
94 die ("stat (%s): %s", SM_DIR, strerror (errno));
96 if (!S_ISDIR (st.st_mode))
97 if (mkdir (SM_DIR, S_IRWXU) == -1)
98 die ("mkdir (%s): %s", SM_DIR, strerror (errno));
100 memset (&st, 0, sizeof st);
102 if (stat (SM_BAK_DIR, &st) == -1 && errno != ENOENT)
103 die ("stat (%s): %s", SM_BAK_DIR, strerror (errno));
105 if (!S_ISDIR (st.st_mode))
106 if (mkdir (SM_BAK_DIR, S_IRWXU) == -1)
107 die ("mkdir (%s): %s", SM_BAK_DIR, strerror (errno));
109 if (!(nld = opendir (SM_DIR)))
110 die ("opendir (%s): %s", SM_DIR, strerror (errno));
113 len2=strlen(SM_BAK_DIR);
114 while ((de = readdir (nld))) {
115 if (de->d_name[0] == '.')
117 len=strlen(de->d_name);
118 src=xmalloc(len1+len+2);
119 dst=xmalloc(len2+len+2);
120 sprintf (src, "%s/%s", SM_DIR, de->d_name);
121 sprintf (dst, "%s/%s", SM_BAK_DIR, de->d_name);
122 if (rename (src, dst) == -1)
123 die ("rename (%s to %s): %s", SM_DIR, SM_BAK_DIR, strerror (errno));
127 if (closedir (nld) == -1)
128 note (N_ERROR, "closedir (%s): %s", SM_DIR, strerror (errno));