#include "citadel_dirs.h"
-
/*
* Replacement for gets() that doesn't throw a compiler warning.
* We're only using it for some simple prompts, so we don't need
}
+// These variables are used by both main() and ctdlmigrate_exit()
+// They are global so that ctdlmigrate_exit can be called from a signal handler
+char socket_path[PATH_MAX];
+pid_t sshpid = (-1);
+
+void ctdlmigrate_exit(int cmdexit) {
+ unlink(socket_path);
+ if (sshpid > 0) {
+ printf("Shutting down the SSH session...\n");
+ kill(sshpid, SIGKILL);
+ }
+ printf("\n\n\033[3%dmExit code %d\033[0m\n", (cmdexit ? 1 : 2), cmdexit);
+ exit(cmdexit);
+}
+
+
int uds_connectsock(char *sockpath) {
int s;
struct sockaddr_un addr;
s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0) {
fprintf(stderr, "sendcommand: Can't create socket: %s\n", strerror(errno));
- exit(3);
+ ctdlmigrate_exit(3);
}
if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
fprintf(stderr, "sendcommand: can't connect: %s\n", strerror(errno));
close(s);
- exit(3);
+ ctdlmigrate_exit(3);
}
return s;
int cmdexit = 0; // when something fails, set cmdexit to nonzero, and skip to the end
char cmd[PATH_MAX];
char buf[PATH_MAX];
- char socket_path[PATH_MAX];
char remote_user[SIZ];
char remote_host[SIZ];
char remote_sendcommand[PATH_MAX];
int linecount = 0;
int a;
int local_admin_socket = (-1);
- pid_t sshpid = (-1);
/* Parse command line */
while ((a = getopt(argc, argv, "h:")) != EOF) {
exit(errno);
}
+ signal(SIGINT, ctdlmigrate_exit);
+ signal(SIGQUIT, ctdlmigrate_exit);
+ signal(SIGTERM, ctdlmigrate_exit);
+ signal(SIGSEGV, ctdlmigrate_exit);
+ signal(SIGHUP, ctdlmigrate_exit);
+ signal(SIGPIPE, ctdlmigrate_exit);
+
printf( "\033[2J\033[H\n"
" \033[32m╔═══════════════════════════════════════════════╗\n"
" ║ ║\n"
printf("Closing the data connection from the source system...\n");
pclose(sourcefp);
}
- printf("Shutting down the socket connection...\n");
- unlink(socket_path);
- printf("Shutting down the SSH session...\n");
- kill(sshpid, SIGKILL);
- printf("\033[3%dmExit code %d\033[0m\n", (cmdexit ? 1 : 2), cmdexit);
- exit(cmdexit);
+
+ ctdlmigrate_exit(cmdexit);
}
* exporting the message multiple times.)
*/
snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
- if (system(cmd) != 0) syslog(LOG_ERR, "Error %d", errno);
+ if (system(cmd) != 0) {
+ syslog(LOG_ERR, "migrate: error %d", errno);
+ }
snprintf(cmd, sizeof cmd, "uniq <%s >%s", migr_tempfilename2, migr_tempfilename1);
- if (system(cmd) != 0) syslog(LOG_ERR, "Error %d", errno);
-
+ if (system(cmd) != 0) {
+ syslog(LOG_ERR, "migrate: error %d", errno);
+ }
snprintf(cmd, sizeof cmd, "wc -l %s", migr_tempfilename1);
FILE *fp = popen(cmd, "r");
else {
total_msgs = 1; // any nonzero just to keep it from barfing
}
- syslog(LOG_DEBUG, "Total messages to be exported: %d", total_msgs);
+ syslog(LOG_DEBUG, "migrate: total messages to be exported: %d", total_msgs);
}
Ctx = CC;
migr_global_message_list = fopen(migr_tempfilename1, "r");
if (migr_global_message_list != NULL) {
- syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
+ syslog(LOG_INFO, "migrate: opened %s", migr_tempfilename1);
while ((Ctx->kill_me == 0) &&
(fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
msgnum = atol(buf);
fclose(migr_global_message_list);
}
if (Ctx->kill_me == 0) {
- syslog(LOG_INFO, "Exported %d messages.", count);
+ syslog(LOG_INFO, "migrate: exported %d messages.", count);
}
else {
- syslog(LOG_ERR, "Export aborted due to client disconnect!");
+ syslog(LOG_ERR, "migrate: export aborted due to client disconnect!");
}
migr_export_message(-1L); /* This frees the encoding buffer */
* This callback stores up the data which appears in between tags.
*/
void migr_xml_chardata(void *data, const XML_Char *s, int len) {
-
- char aaa[65536];
- memcpy(aaa, s, len);
- aaa[len] = 0;
- syslog(LOG_DEBUG, "chardata <\033[35m%s\033[0m>", aaa);
-
StrBufAppendBufPlain(migr_chardata, s, len, 0);
}
}
if (citadel_migrate_data != 1) {
- syslog(LOG_ERR, "Out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
+ syslog(LOG_ERR, "migrate: out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
return;
}
}
if (citadel_migrate_data != 1) {
- syslog(LOG_ERR, "Out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
+ syslog(LOG_ERR, "migrate: out-of-sequence tag <%s> detected. Warning: ODD-DATA!", el);
return;
}
/*** CONFIG ***/
if (!strcasecmp(el, "config")) {
- syslog(LOG_DEBUG, "Imported config key=%s", ikey);
+ syslog(LOG_DEBUG, "migrate: imported config key=%s", ikey);
if (ikey != NULL) {
CtdlSetConfigStr(ikey, (char *)ChrPtr(migr_chardata));
ikey = NULL;
}
else {
- syslog(LOG_INFO, "Closed a <config> tag but no key name was supplied.");
+ syslog(LOG_INFO, "migrate: closed a <config> tag but no key name was supplied.");
}
}
; /* Nothing to do anymore */
else if (!strcasecmp(el, "user")) {
CtdlPutUser(&usbuf);
- syslog(LOG_INFO, "Imported user: %s", usbuf.fullname);
+ syslog(LOG_INFO, "migrate: imported user: %s", usbuf.fullname);
}
/*** OPENID ***/
memcpy(&oid_data[sizeof(long)], openid_url, strlen(openid_url) + 1);
cdb_store(CDB_EXTAUTH, openid_url, strlen(openid_url), oid_data, oid_data_len);
free(oid_data);
- syslog(LOG_INFO, "Imported OpenID: %s (%ld)", openid_url, openid_usernum);
+ syslog(LOG_INFO, "migrate: imported OpenID: %s (%ld)", openid_url, openid_usernum);
}
/*** ROOM ***/
; /* Nothing to do anymore */
else if (!strcasecmp(el, "room")) {
CtdlPutRoom(&qrbuf);
- syslog(LOG_INFO, "Imported room: %s", qrbuf.QRname);
+ syslog(LOG_INFO, "migrate: imported room: %s", qrbuf.QRname);
}
/*** ROOM MESSAGE POINTERS ***/
msglist_alloc = 1000;
msglist = malloc(sizeof(long) * msglist_alloc);
- syslog(LOG_DEBUG, "Message list for: %s", FRname);
+ syslog(LOG_DEBUG, "migrate: message list for: %s", FRname);
ptr = ChrPtr(migr_chardata);
- syslog(LOG_DEBUG, "\033[31m%s\033[0m", ptr);
while (*ptr != 0) {
while ((*ptr != 0) && (!isdigit(*ptr))) {
++ptr;
msglist = realloc(msglist, sizeof(long) * msglist_alloc);
}
msglist[msgcount++] = msgnum;
- syslog(LOG_DEBUG, "\033[32mmsgnum %ld , count is now %d\033[0m", msgnum, msgcount);
}
}
while ((*ptr != 0) && (isdigit(*ptr))) {
free(msglist);
msglist = NULL;
msglist_alloc = 0;
- syslog(LOG_DEBUG, "Imported %d messages.", msgcount);
+ syslog(LOG_DEBUG, "migrate: imported %d messages.", msgcount);
if (server_shutting_down) {
return;
}
else if (!strcasecmp(el, "floor")) {
CtdlPutFloor(&flbuf, floornum);
- syslog(LOG_INFO, "Imported floor #%d (%s)", floornum, flbuf.f_name);
+ syslog(LOG_INFO, "migrate: imported floor #%d (%s)", floornum, flbuf.f_name);
}
/*** VISITS ***/
}
else if (!strcasecmp(el, "visit")) {
put_visit(&vbuf);
- syslog(LOG_INFO, "Imported visit: %ld/%ld/%ld", vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
+ syslog(LOG_INFO, "migrate: imported visit: %ld/%ld/%ld", vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
}
/*** MESSAGES ***/
}
syslog(LOG_INFO,
- "%s message #%ld, size=%d, refcount=%d, bodylength=%ld, content-type: %s",
+ "migrate: %s message #%ld, size=%d, refcount=%d, bodylength=%ld, content-type: %s",
(rc!= 0)?"failed to import ":"Imported ",
import_msgnum,
StrLength(migr_MsgData),
CC->redirect_buffer = NULL;
syslog(LOG_INFO,
- "Setting message #%ld meta data to: refcount=%d, bodylength=%ld, content-type: %s",
+ "migrate: setting message #%ld meta data to: refcount=%d, bodylength=%ld, content-type: %s",
smi.meta_msgnum,
smi.meta_refcount,
smi.meta_rfc822_length,
const char *Pos = NULL;
StrBuf *oneRoom = NewStrBuf();
- syslog(LOG_INFO, "removing message pointer %ld from these rooms: %s", msgnum, ChrPtr(RoomNameVec));
+ syslog(LOG_INFO, "migrate: removing message pointer %ld from these rooms: %s", msgnum, ChrPtr(RoomNameVec));
while (Pos != StrBufNOTNULL){
StrBufExtract_NextToken(oneRoom, RoomNameVec, &Pos, '|');
* exporting the message multiple times.)
*/
snprintf(cmd, sizeof cmd, "sort -n <%s >%s", migr_tempfilename1, migr_tempfilename2);
- if (system(cmd) != 0) syslog(LOG_ERR, "Error %d", errno);
+ if (system(cmd) != 0) {
+ syslog(LOG_ERR, "migrate: error %d", errno);
+ }
RoomNames = NewStrBuf();
Ctx = CC;
migr_global_message_list = fopen(migr_tempfilename2, "r");
if (migr_global_message_list != NULL) {
- syslog(LOG_INFO, "Opened %s", migr_tempfilename1);
+ syslog(LOG_INFO, "migrate: opened %s", migr_tempfilename1);
while ((Ctx->kill_me == 0) &&
(fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) {
msgnum = atol(buf);