int accept(int socket, struct sockaddr *addr, socklen_t *addrlen) { int fd, index, ret; if (fd_get(socket, &fd) == fd_rsocket) { index = fd_open(); if (index < 0) return index; ret = raccept(fd, addr, addrlen); if (ret < 0) { fd_close(index, &fd); return ret; } fd_store(index, ret, fd_rsocket, fd_ready); return index; } else if (fd_gets(socket) == fd_fork_listen) { index = fd_open(); if (index < 0) return index; ret = real.accept(fd, addr, addrlen); if (ret < 0) { fd_close(index, &fd); return ret; } fd_store(index, ret, fd_normal, fd_fork_passive); return index; } else { return real.accept(fd, addr, addrlen); } }
static int trunk_binlog_open_read(const char *filename, const bool skipFirstLine) { int result; int fd; char buff[32]; fd = open(filename, O_RDONLY); if (fd < 0) { result = errno != 0 ? errno : EACCES; logError("file: "__FILE__", line: %d, " \ "open file \"%s\" fail, " \ "errno: %d, error info: %s", \ __LINE__, filename, result, STRERROR(result)); return -1; } if (skipFirstLine) { if (fd_gets(fd, buff, sizeof(buff), 16) <= 0) { logWarning("file: "__FILE__", line: %d, " \ "skip first line fail!", __LINE__); } } return fd; }
static void received_event_delete_request(gint fd) { gchar buf[BUFFSIZE]; gboolean delete_successful = FALSE; if (fd_gets(fd, buf, sizeof(buf)) != -1) { gchar *id; id = g_strchomp(buf); if(vcal_event_exists(id)) { AlertValue val; val = G_ALERTALTERNATE; if(opensync_config.event_ask_delete) { gchar *msg; msg = g_strdup_printf(_("Really delete event with id '%s'?"), id); val = alertpanel(_("OpenSync plugin"),msg, GTK_STOCK_CANCEL,GTK_STOCK_DELETE,NULL); g_free(msg); } if(((!opensync_config.event_ask_delete) || (val != G_ALERTDEFAULT)) && vcal_delete_event(id)) { g_print("Deleted id: '%s'\n", id); delete_successful = TRUE; } } } if(delete_successful) { sock_send(fd, ":ok:\n"); } else { sock_send(fd, ":failure:\n"); } }
gint sock_gets(SockInfo *sock, gchar *buf, gint len) { g_return_val_if_fail(sock != NULL, -1); #if USE_SSL if (sock->ssl) return ssl_gets(sock->ssl, buf, len); #endif return fd_gets(sock->sock, buf, len); }
int listen(int socket, int backlog) { int fd, ret; if (fd_get(socket, &fd) == fd_rsocket) { ret = rlisten(fd, backlog); } else { ret = real.listen(fd, backlog); if (!ret && fd_gets(socket) == fd_fork) fd_store(socket, fd, fd_normal, fd_fork_listen); } return ret; }
/* smtp_read() -- Get a line and return the initial digit */ int smtp_read(int fd, char *response) { do { if(fd_gets(response, BUF_SZ, fd) == NULL) { return(0); } } while(response[3] == '-'); if(log_level > 0) { log_event(LOG_INFO, "%s\n", response); } if(minus_v) { (void)fprintf(stderr, "[<-] %s\n", response); } return(atoi(response) / 100); }
static gboolean listen_channel_input_cb(GIOChannel *chan, GIOCondition cond, gpointer data) { gint sock; gchar buf[BUFFSIZE]; if (answer_sock != -1) return TRUE; sock = g_io_channel_unix_get_fd(chan); answer_sock = fd_accept(sock); while (fd_gets(answer_sock, buf, sizeof(buf)) != -1) { g_print("Received request: %s", buf); if(g_str_has_prefix(buf,":request_contacts:")) received_contacts_request(answer_sock); else if(g_str_has_prefix(buf, ":modify_contact:")) received_contact_modify_request(answer_sock); else if(g_str_has_prefix(buf, ":delete_contact:")) received_contact_delete_request(answer_sock); else if(g_str_has_prefix(buf, ":add_contact:")) received_contact_add_request(answer_sock); else if(g_str_has_prefix(buf, ":request_events:")) received_events_request(answer_sock); else if(g_str_has_prefix(buf, ":modify_event:")) received_event_modify_request(answer_sock); else if(g_str_has_prefix(buf, ":delete_event:")) received_event_delete_request(answer_sock); else if(g_str_has_prefix(buf, ":add_event:")) received_event_add_request(answer_sock); else if(g_str_has_prefix(buf,":finished:")) { received_finished_notification(answer_sock); break; } } fd_close(answer_sock); answer_sock = -1; g_print("closed answer sock\n"); return TRUE; }
int connect(int socket, const struct sockaddr *addr, socklen_t addrlen) { int fd, ret; if (fd_get(socket, &fd) == fd_rsocket) { ret = rconnect(fd, addr, addrlen); if (!ret || errno == EINPROGRESS) return ret; ret = transpose_socket(socket, fd_normal); if (ret < 0) return ret; rclose(fd); fd = ret; } else if (fd_gets(socket) == fd_fork) { fd_store(socket, fd, fd_normal, fd_fork_active); } return real.connect(fd, addr, addrlen); }
static void received_contact_delete_request(gint fd) { gchar buf[BUFFSIZE]; gboolean delete_successful= FALSE; if (fd_gets(fd, buf, sizeof(buf)) != -1) { gchar *id; ContactHashVal *hash_val; id = g_strchomp(buf); hash_val = g_hash_table_lookup(contact_hash, id); if (hash_val) { AlertValue val; val = G_ALERTALTERNATE; if(opensync_config.contact_ask_delete) { gchar *msg; msg = g_strdup_printf(_("Really delete contact for '%s'?"), ADDRITEM_NAME(hash_val->person)); val = alertpanel(_("OpenSync plugin"),msg, GTK_STOCK_CANCEL,GTK_STOCK_DELETE,NULL); g_free(msg); } if(((!opensync_config.contact_ask_delete) || (val != G_ALERTDEFAULT)) && (addrduplicates_delete_item_person(hash_val->person,hash_val->ds))) { g_print("Deleted id: '%s'\n", id); delete_successful = TRUE; } } } if(delete_successful) { sock_send(fd, ":ok:\n"); } else { sock_send(fd, ":failure:\n"); } }
gint fd_getline(gint fd, gchar **line) { gchar buf[BUFFSIZE]; gchar *str = NULL; gint len; gulong size = 0; gulong cur_offset = 0; while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) { size += len; str = g_realloc(str, size + 1); memcpy(str + cur_offset, buf, len + 1); cur_offset += len; if (buf[len - 1] == '\n') break; } *line = str; if (!str) return -1; else return (gint)size; }
static void received_event_modify_request(gint fd) { gchar buf[BUFFSIZE]; gchar *new_vevent = NULL; if (fd_gets(fd, buf, sizeof(buf)) != -1) { gchar *id; id = g_strchomp(buf); g_print("id to change: '%s'\n",id); if(vcal_event_exists(id)) { AlertValue val; val = G_ALERTALTERNATE; if(opensync_config.event_ask_modify) { gchar *msg; msg = g_strdup_printf(_("Really modify event ID '%s'?"), id); val = alertpanel(_("OpenSync plugin"),msg, GTK_STOCK_CANCEL,GTK_STOCK_EDIT,NULL); g_free(msg); } if((!opensync_config.event_ask_modify) || (val != G_ALERTDEFAULT)) { gchar *vevent = g_strdup(""); gchar *tmp; gboolean done = FALSE; while(!done) { if(fd_gets(answer_sock, buf, sizeof(buf)) == -1) { g_print("error receiving event to modify\n"); break; } if(g_str_has_prefix(buf,":done:")) done = TRUE; else { tmp = vevent; vevent = g_strconcat(tmp,buf,NULL); g_free(tmp); } } g_print("Modification to: '%s'\n", vevent); if((new_vevent = vcal_update_event(vevent)) != NULL) g_print("event updated successfully\n"); else g_print("could not update event\n"); g_free(vevent); } else { g_print("Error: User refused to modify event ID '%s'\n", id); } } else g_printf("warning: tried to modify non-existent event\n"); if(new_vevent) { gchar *msg; sock_send(fd, ":start_event:\n"); msg = g_strdup_printf("%s\n", new_vevent); g_free(new_vevent); sock_send(fd, msg); g_free(msg); sock_send(fd, ":end_event:\n"); } else sock_send(fd, ":failure:\n"); } }
static void received_contact_modify_request(gint fd) { gchar buf[BUFFSIZE]; gchar *return_vcard = NULL; if (fd_gets(fd, buf, sizeof(buf)) != -1) { gchar *id; ContactHashVal *hash_val; id = g_strchomp(buf); g_print("id to change: '%s'\n",id); hash_val = g_hash_table_lookup(contact_hash, id); if(hash_val) { AlertValue val; val = G_ALERTALTERNATE; if(opensync_config.contact_ask_modify) { gchar *msg; msg = g_strdup_printf(_("Really modify contact for '%s'?"), ADDRITEM_NAME(hash_val->person)); val = alertpanel(_("OpenSync plugin"),msg, GTK_STOCK_CANCEL,GTK_STOCK_EDIT,NULL); g_free(msg); } if((!opensync_config.contact_ask_modify) || (val != G_ALERTDEFAULT)) { gchar *vcard = g_strdup(""); gchar *tmp; gboolean done = FALSE; AddressBookFile *abf; while(!done) { if(fd_gets(answer_sock, buf, sizeof(buf)) == -1) { g_print("error receiving contact to modify\n"); break; } g_print("buf: %s\n",buf); if(g_str_has_prefix(buf,":done:")) done = TRUE; else { tmp = vcard; vcard = g_strconcat(tmp,buf,NULL); g_free(tmp); } } abf = hash_val->ds->rawDataSource; g_print("Modification to: '%s'\n",vcard); update_ItemPerson_from_vcard(abf, hash_val->person, vcard); g_free(vcard); return_vcard = vcard_get_from_ItemPerson(hash_val->person); } else { g_print("Error: User refused to modify contact '%s'\n", ADDRITEM_NAME(hash_val->person)); } } else g_printf("warning: tried to modify non-existent contact\n"); if(return_vcard) { send_ gchar *msg; sock_send(fd, ":start_contact:\n"); msg = g_strdup_printf("%s\n", return_vcard); g_free(return_vcard); sock_send(fd, msg); g_free(msg); sock_send(fd, ":end_contact:\n"); } else sock_send(fd, ":failure:\n"); } }
void show_backtrace (void) { #if GLIBC_BACKTRACE #define DEPTH 50 #define BUFSIZE 1024 void *trace[DEPTH]; int depth; depth = backtrace (trace, DEPTH); if (depth <= 0) return; #if CAN_PIPE if (addr2line_path == NULL) goto fallback_noerr; /* We attempt to extract file and line information from addr2line. */ do { /* Local variables. */ int f[2], pid, bt[2], inp[2]; char addr_buf[GFC_XTOA_BUF_SIZE], func[BUFSIZE], file[BUFSIZE]; char *p; /* Don't output an error message if something goes wrong, we'll simply fall back to the pstack and glibc backtraces. */ if (pipe (f) != 0) break; if (pipe (inp) != 0) break; if ((pid = fork ()) == -1) break; if (pid == 0) { /* Child process. */ #define NUM_FIXEDARGS 7 char *arg[NUM_FIXEDARGS]; char *newenv[] = { NULL }; close (f[0]); close (inp[1]); if (dup2 (inp[0], STDIN_FILENO) == -1) _exit (1); close (inp[0]); close (STDERR_FILENO); if (dup2 (f[1], STDOUT_FILENO) == -1) _exit (1); close (f[1]); arg[0] = addr2line_path; arg[1] = (char *) "-e"; arg[2] = full_exe_path (); arg[3] = (char *) "-f"; arg[4] = (char *) "-s"; arg[5] = (char *) "-C"; arg[6] = NULL; execve (addr2line_path, arg, newenv); _exit (1); #undef NUM_FIXEDARGS } /* Father process. */ close (f[1]); close (inp[0]); if (pipe (bt) != 0) break; backtrace_symbols_fd (trace, depth, bt[1]); close (bt[1]); estr_write ("\nBacktrace for this error:\n"); for (int j = 0; j < depth; j++) { const char *addr = gfc_xtoa ((GFC_UINTEGER_LARGEST) (intptr_t) trace[j], addr_buf, sizeof (addr_buf)); write (inp[1], addr, strlen (addr)); write (inp[1], "\n", 1); if (! fd_gets (func, sizeof(func), f[0])) goto fallback; if (! fd_gets (file, sizeof(file), f[0])) goto fallback; for (p = func; *p != '\n' && *p != '\r'; p++) ; *p = '\0'; /* If we only have the address, use the glibc backtrace. */ if (func[0] == '?' && func[1] == '?' && file[0] == '?' && file[1] == '?') { bt_header (j); while (1) { char bc; ssize_t nread = read (bt[0], &bc, 1); if (nread != 1 || bc == '\n') break; write (STDERR_FILENO, &bc, 1); } estr_write ("\n"); continue; } else { /* Forward to the next entry in the backtrace. */ while (1) { char bc; ssize_t nread = read (bt[0], &bc, 1); if (nread != 1 || bc == '\n') break; } } /* _start is a setup routine that calls main(), and main() is the frontend routine that calls some setup stuff and then calls MAIN__, so at this point we should stop. */ if (strcmp (func, "_start") == 0 || strcmp (func, "main") == 0) break; bt_header (j); estr_write (full_exe_path ()); estr_write ("[0x"); estr_write (addr); estr_write ("] in "); estr_write (func); if (strncmp (file, "??", 2) == 0) estr_write ("\n"); else { estr_write (" at "); estr_write (file); } } /* Loop over each hex address. */ close (inp[1]); close (bt[0]); wait (NULL); return; fallback: estr_write ("** Something went wrong while running addr2line. **\n" "** Falling back to a simpler backtrace scheme. **\n"); } while (0); #undef DEPTH #undef BUFSIZE #endif /* CAN_PIPE */ fallback_noerr: /* Fallback to the glibc backtrace. */ estr_write ("\nBacktrace for this error:\n"); backtrace_symbols_fd (trace, depth, STDERR_FILENO); return; #elif defined(CAN_FORK) && defined(HAVE_GETPPID) /* Try to call pstack. */ do { /* Local variables. */ int pid; /* Don't output an error message if something goes wrong, we'll simply fall back to the pstack and glibc backtraces. */ if ((pid = fork ()) == -1) break; if (pid == 0) { /* Child process. */ #define NUM_ARGS 2 char *arg[NUM_ARGS+1]; char buf[20]; estr_write ("\nBacktrace for this error:\n"); arg[0] = (char *) "pstack"; snprintf (buf, sizeof(buf), "%d", (int) getppid ()); arg[1] = buf; arg[2] = NULL; execvp (arg[0], arg); #undef NUM_ARGS /* pstack didn't work. */ estr_write (" unable to produce a backtrace, sorry!\n"); _exit (1); } /* Father process. */ wait (NULL); return; } while(0); #else estr_write ("\nBacktrace not yet available on this platform, sorry!\n"); #endif }