2 * $Id: main.c,v 1.1.4.10.2.3 2005-01-31 17:01:05 didg Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * All Rights Reserved. See COPYING.
10 #endif /* HAVE_CONFIG_H */
14 #endif /* HAVE_UNISTD_H */
17 #endif /* HAVE_FCNTL_H */
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif /* HAVE_SYS_TYPES_H */
26 #include <sys/param.h>
27 #ifdef HAVE_SYS_STAT_H
29 #endif /* HAVE_SYS_STAT_H */
33 #include <netatalk/endian.h>
34 #include <atalk/cnid_dbd_private.h>
35 #include <atalk/logger.h>
43 #define LOCKFILENAME "lock"
45 static int exit_sig = 0;
48 static void sig_exit(int signo)
54 static void block_sigs_onoff(int block)
59 sigaddset(&set, SIGINT);
60 sigaddset(&set, SIGTERM);
62 sigprocmask(SIG_BLOCK, &set, NULL);
64 sigprocmask(SIG_UNBLOCK, &set, NULL);
69 The dbd_XXX and comm_XXX functions all obey the same protocol for return values:
71 1: Success, if transactions are used commit.
72 0: Failure, but we continue to serve requests. If transactions are used abort/rollback.
73 -1: Fatal error, either from the database or from the socket. Abort the transaction if applicable
74 (which might fail as well) and then exit.
76 We always try to notify the client process about the outcome, the result field
77 of the cnid_dbd_rply structure contains further details.
81 static int loop(struct db_param *dbp)
83 struct cnid_dbd_rqst rqst;
84 struct cnid_dbd_rply rply;
86 time_t now, time_next_flush, time_last_rqst;
88 static char namebuf[MAXPATHLEN + 1];
89 u_int32_t checkp_flags;
93 time_next_flush = now + dbp->flush_interval;
96 checkp_flags = DB_FORCE;
103 if ((cret = comm_rcv(&rqst)) < 0)
108 if (count > dbp->flush_frequency || now > time_next_flush) {
109 #ifdef CNID_BACKEND_DBD_TXN
110 if (dbif_txn_checkpoint(0, 0, checkp_flags) < 0)
117 time_next_flush = now + dbp->flush_interval;
125 if (dbp->idle_timeout && comm_nbe() <= 0 && (now - time_last_rqst) > dbp->idle_timeout)
129 /* We got a request */
130 time_last_rqst = now;
133 #ifdef CNID_BACKEND_DBD_TXN
134 if (dbif_txn_begin() < 0)
136 #endif /* CNID_BACKEND_DBD_TXN */
138 memset(&rply, 0, sizeof(rply));
140 /* ret gets set here */
141 case CNID_DBD_OP_OPEN:
142 case CNID_DBD_OP_CLOSE:
143 /* open/close are noops for now. */
147 case CNID_DBD_OP_ADD:
148 ret = dbd_add(&rqst, &rply);
150 case CNID_DBD_OP_GET:
151 ret = dbd_get(&rqst, &rply);
153 case CNID_DBD_OP_RESOLVE:
154 ret = dbd_resolve(&rqst, &rply);
156 case CNID_DBD_OP_LOOKUP:
157 ret = dbd_lookup(&rqst, &rply);
159 case CNID_DBD_OP_UPDATE:
160 ret = dbd_update(&rqst, &rply);
162 case CNID_DBD_OP_DELETE:
163 ret = dbd_delete(&rqst, &rply);
165 case CNID_DBD_OP_GETSTAMP:
166 ret = dbd_getstamp(&rqst, &rply);
169 LOG(log_error, logtype_cnid, "loop: unknow op %d", rqst.op);
174 if ((cret = comm_snd(&rply)) < 0 || ret < 0) {
175 #ifdef CNID_BACKEND_DBD_TXN
177 #endif /* CNID_BACKEND_DBD_TXN */
180 #ifdef CNID_BACKEND_DBD_TXN
181 if (ret == 0 || cret == 0) {
182 if (dbif_txn_abort() < 0)
185 if (dbif_txn_commit() < 0)
188 #endif /* CNID_BACKEND_DBD_TXN */
192 /* ------------------------ */
193 static void switch_to_user(char *dir)
197 if (chdir(dir) < 0) {
198 LOG(log_error, logtype_cnid, "chdir to %s failed: %s", dir, strerror(errno));
202 if (stat(".", &st) < 0) {
203 LOG(log_error, logtype_cnid, "error in stat for %s: %s", dir, strerror(errno));
207 LOG(log_info, logtype_cnid, "Setting uid/gid to %i/%i", st.st_uid, st.st_gid);
208 if (setgid(st.st_gid) < 0 || setuid(st.st_uid) < 0) {
209 LOG(log_error, logtype_cnid, "uid/gid: %s", strerror(errno));
215 /* ------------------------ */
221 if ((lockfd = open(LOCKFILENAME, O_RDWR | O_CREAT, 0644)) < 0) {
222 LOG(log_error, logtype_cnid, "main: error opening lockfile: %s", strerror(errno));
227 lock.l_whence = SEEK_SET;
229 lock.l_type = F_WRLCK;
231 if (fcntl(lockfd, F_SETLK, &lock) < 0) {
232 if (errno == EACCES || errno == EAGAIN) {
235 LOG(log_error, logtype_cnid, "main: fcntl F_WRLCK lockfile: %s", strerror(errno));
243 /* ----------------------- */
244 void set_signal(void)
248 sv.sa_handler = sig_exit;
250 sigemptyset(&sv.sa_mask);
251 sigaddset(&sv.sa_mask, SIGINT);
252 sigaddset(&sv.sa_mask, SIGTERM);
253 if (sigaction(SIGINT, &sv, NULL) < 0 || sigaction(SIGTERM, &sv, NULL) < 0) {
254 LOG(log_error, logtype_cnid, "main: sigaction: %s", strerror(errno));
257 sv.sa_handler = SIG_IGN;
258 sigemptyset(&sv.sa_mask);
259 if (sigaction(SIGPIPE, &sv, NULL) < 0) {
260 LOG(log_error, logtype_cnid, "main: sigaction: %s", strerror(errno));
265 /* ----------------------- */
266 void free_lock(int lockfd)
271 lock.l_whence = SEEK_SET;
273 lock.l_type = F_UNLCK;
274 fcntl(lockfd, F_SETLK, &lock);
278 /* ------------------------ */
279 int main(int argc, char *argv[])
281 struct db_param *dbp;
284 int lockfd, ctrlfd, clntfd;
287 set_processname("cnid_dbd");
288 syslog_setup(log_debug, logtype_default, logoption_ndelay | logoption_pid, logfacility_daemon);
291 LOG(log_error, logtype_cnid, "main: not enough arguments");
296 ctrlfd = atoi(argv[2]);
297 clntfd = atoi(argv[3]);
301 /* Before we do anything else, check if there is an instance of cnid_dbd
302 running already and silently exit if yes. */
305 LOG(log_info, logtype_cnid, "Startup, DB dir %s", dir);
309 /* SIGINT and SIGTERM are always off, unless we get a return code of 0 from
310 comm_rcv (no requests for one second, see above in loop()). That means we
311 only shut down after one second of inactivity. */
314 if ((dbp = db_param_read(dir)) == NULL)
317 if (dbif_env_init(dbp) < 0)
318 exit(2); /* FIXME: same exit code as failure for dbif_open() */
320 #ifdef CNID_BACKEND_DBD_TXN
321 if (dbif_txn_begin() < 0)
325 if (dbif_open(dbp, 0) < 0) {
326 #ifdef CNID_BACKEND_DBD_TXN
333 #ifndef CNID_BACKEND_DBD_TXN
334 if (dbp->check && (ret = dbd_check(dir))) {
340 LOG(log_info, logtype_cnid, "main: re-opening, secondaries will be rebuilt. This may take some time");
341 if (dbif_open(dbp, 1) < 0) {
342 LOG(log_info, logtype_cnid, "main: re-opening databases failed");
346 LOG(log_info, logtype_cnid, "main: rebuilt done");
350 if (dbd_stamp() < 0) {
351 #ifdef CNID_BACKEND_DBD_TXN
357 #ifdef CNID_BACKEND_DBD_TXN
358 if (dbif_txn_commit() < 0)
362 if (comm_init(dbp, ctrlfd, clntfd) < 0) {
370 #ifndef CNID_BACKEND_DBD_TXN
371 /* FIXME: Do we really need to sync before closing the DB? Just closing it
377 if (dbif_close() < 0)
385 LOG(log_info, logtype_cnid, "main: Exiting on signal %i", exit_sig);
387 LOG(log_info, logtype_cnid, "main: Idle timeout, exiting");