static gboolean metadata_file_write(gchar *path, const GList *keywords, const gchar *comment) { SecureSaveInfo *ssi; ssi = secure_open(path); if (!ssi) return FALSE; secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION); secure_fprintf(ssi, "[keywords]\n"); while (keywords && secsave_errno == SS_ERR_NONE) { const gchar *word = keywords->data; keywords = keywords->next; secure_fprintf(ssi, "%s\n", word); } secure_fputc(ssi, '\n'); secure_fprintf(ssi, "[comment]\n"); secure_fprintf(ssi, "%s\n", (comment) ? comment : ""); secure_fprintf(ssi, "#end\n"); return (secure_close(ssi) == 0); }
gboolean cache_sim_data_save(CacheData *cd) { SecureSaveInfo *ssi; gchar *pathl; if (!cd || !cd->path) return FALSE; pathl = path_from_utf8(cd->path); ssi = secure_open(pathl); g_free(pathl); if (!ssi) { log_printf("Unable to save sim cache data: %s\n", cd->path); return FALSE; } secure_fprintf(ssi, "SIMcache\n#%s %s\n", PACKAGE, VERSION); cache_sim_write_dimensions(ssi, cd); cache_sim_write_date(ssi, cd); cache_sim_write_checksum(ssi, cd); cache_sim_write_md5sum(ssi, cd); cache_sim_write_similarity(ssi, cd); if (secure_close(ssi)) { log_printf(_("error saving sim cache data: %s\nerror: %s\n"), cd->path, secsave_strerror(secsave_errno)); return FALSE; } return TRUE; }
void StdCapture::EndCapture() { std::lock_guard<std::mutex> lock(m_mutex); if (!m_capturing) return; m_captured.clear(); secure_dup2(m_oldStdOut, STD_OUT_FD); secure_dup2(m_oldStdErr, STD_ERR_FD); const int bufSize = 1025; char buf[bufSize]; int bytesRead = 0; bool fd_blocked(false); do { bytesRead = 0; fd_blocked = false; #ifdef _MSC_VER if (!eof(m_pipe[READ])) bytesRead = read(m_pipe[READ], buf, bufSize - 1); #else bytesRead = read(m_pipe[READ], buf, bufSize - 1); #endif if (bytesRead > 0) { buf[bytesRead] = 0; m_captured += buf; } else if (bytesRead < 0) { fd_blocked = (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR); if (fd_blocked) std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } while (fd_blocked || bytesRead == (bufSize - 1)); secure_close(m_oldStdOut); secure_close(m_oldStdErr); secure_close(m_pipe[READ]); #ifdef _MSC_VER secure_close(m_pipe[WRITE]); #endif m_capturing = false; }
void FileRepConnServer_CloseConnection(void) { if (port != NULL) { secure_close(port); if (port->sock >= 0) { /* to close socket (file descriptor) */ StreamClose(port->sock); port->sock = -1; } ConnFree(); } }
/* -------------------------------- * pq_comm_close_fatal - shutdown libpq at backend fatal error exit * -------------------------------- */ void pq_comm_close_fatal(void) { if (MyProcPort != NULL) { /* Cleanly shut down SSL layer */ secure_close(MyProcPort); if (MyProcPort->sock >= 0) closesocket(MyProcPort->sock); MyProcPort->sock = -1; } } /* pq_comm_close_fatal */
/* -------------------------------- * socket_close - shutdown libpq at backend exit * * This is the one pg_on_exit_callback in place during BackendInitialize(). * That function's unusual signal handling constrains that this callback be * safe to run at any instant. * -------------------------------- */ static void socket_close(int code, Datum arg) { /* Nothing to do in a standalone backend, where MyProcPort is NULL. */ if (MyProcPort != NULL) { #if defined(ENABLE_GSS) || defined(ENABLE_SSPI) #ifdef ENABLE_GSS OM_uint32 min_s; /* * Shutdown GSSAPI layer. This section does nothing when interrupting * BackendInitialize(), because pg_GSS_recvauth() makes first use of * "ctx" and "cred". */ if (MyProcPort->gss->ctx != GSS_C_NO_CONTEXT) gss_delete_sec_context(&min_s, &MyProcPort->gss->ctx, NULL); if (MyProcPort->gss->cred != GSS_C_NO_CREDENTIAL) gss_release_cred(&min_s, &MyProcPort->gss->cred); #endif /* ENABLE_GSS */ /* * GSS and SSPI share the port->gss struct. Since nowhere else does a * postmaster child free this, doing so is safe when interrupting * BackendInitialize(). */ free(MyProcPort->gss); #endif /* ENABLE_GSS || ENABLE_SSPI */ /* * Cleanly shut down SSL layer. Nowhere else does a postmaster child * call this, so this is safe when interrupting BackendInitialize(). */ secure_close(MyProcPort); /* * Formerly we did an explicit close() here, but it seems better to * leave the socket open until the process dies. This allows clients * to perform a "synchronous close" if they care --- wait till the * transport layer reports connection closure, and you can be sure the * backend has exited. * * We do set sock to PGINVALID_SOCKET to prevent any further I/O, * though. */ MyProcPort->sock = PGINVALID_SOCKET; } }
void StdCapture::BeginCapture() { std::lock_guard<std::mutex> lock(m_mutex); if (m_capturing) return; secure_pipe(m_pipe); m_oldStdOut = secure_dup(STD_OUT_FD); m_oldStdErr = secure_dup(STD_ERR_FD); secure_dup2(m_pipe[WRITE], STD_OUT_FD); secure_dup2(m_pipe[WRITE], STD_ERR_FD); m_capturing = true; #ifndef _MSC_VER secure_close(m_pipe[WRITE]); #endif }
/* -------------------------------- * pq_close - shutdown libpq at backend exit * * Note: in a standalone backend MyProcPort will be null, * don't crash during exit... * -------------------------------- */ static void pq_close(int code, Datum arg) { if (MyProcPort != NULL) { /* Cleanly shut down SSL layer */ secure_close(MyProcPort); /* * Formerly we did an explicit close() here, but it seems better to * leave the socket open until the process dies. This allows clients * to perform a "synchronous close" if they care --- wait till the * transport layer reports connection closure, and you can be sure the * backend has exited. * * We do set sock to -1 to prevent any further I/O, though. */ MyProcPort->sock = -1; } }
/* -------------------------------- * pq_close - shutdown libpq at backend exit * * Note: in a standalone backend MyProcPort will be null, * don't crash during exit... * -------------------------------- */ static void pq_close(int code, Datum arg) { if (MyProcPort != NULL) { #if defined(ENABLE_GSS) || defined(ENABLE_SSPI) #ifdef ENABLE_GSS OM_uint32 min_s; /* Shutdown GSSAPI layer */ if (MyProcPort->gss->ctx != GSS_C_NO_CONTEXT) gss_delete_sec_context(&min_s, &MyProcPort->gss->ctx, NULL); if (MyProcPort->gss->cred != GSS_C_NO_CREDENTIAL) gss_release_cred(&min_s, &MyProcPort->gss->cred); #endif /* ENABLE_GSS */ /* GSS and SSPI share the port->gss struct */ free(MyProcPort->gss); #endif /* ENABLE_GSS || ENABLE_SSPI */ /* Cleanly shut down SSL layer */ secure_close(MyProcPort); /* * Formerly we did an explicit close() here, but it seems better to * leave the socket open until the process dies. This allows clients * to perform a "synchronous close" if they care --- wait till the * transport layer reports connection closure, and you can be sure the * backend has exited. * * We do set sock to PGINVALID_SOCKET to prevent any further I/O, * though. */ MyProcPort->sock = PGINVALID_SOCKET; } }
static gboolean collection_save_private(CollectionData *cd, const gchar *path) { SecureSaveInfo *ssi; GList *work; gchar *pathl; if (!path && !cd->path) return FALSE; if (!path) { path = cd->path; } pathl = path_from_utf8(path); ssi = secure_open(pathl); g_free(pathl); if (!ssi) { log_printf(_("failed to open collection (write) \"%s\"\n"), path); return FALSE; } secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER); secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION); collection_update_geometry(cd); if (cd->window_read) { secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h); } work = cd->list; while (work && secsave_errno == SS_ERR_NONE) { CollectInfo *ci = work->data; secure_fprintf(ssi, "\"%s\"\n", ci->fd->path); work = work->next; } secure_fprintf(ssi, "#end\n"); if (secure_close(ssi)) { log_printf(_("error saving collection file: %s\nerror: %s\n"), path, secsave_strerror(secsave_errno)); return FALSE; } if (!cd->path || strcmp(path, cd->path) != 0) { gchar *buf = cd->path; cd->path = g_strdup(path); path = cd->path; g_free(buf); g_free(cd->name); cd->name = g_strdup(filename_from_path(cd->path)); collection_path_changed(cd); } cd->changed = FALSE; return TRUE; }
gboolean save_config_to_file(const gchar *utf8_path, ConfOptions *options) { SecureSaveInfo *ssi; gchar *rc_pathl; GString *outstr; gint indent = 0; GList *work; rc_pathl = path_from_utf8(utf8_path); ssi = secure_open(rc_pathl); g_free(rc_pathl); if (!ssi) { log_printf(_("error saving config file: %s\n"), utf8_path); return FALSE; } outstr = g_string_new(""); g_string_append_printf(outstr, "<!--\n"); g_string_append_printf(outstr, "######################################################################\n"); g_string_append_printf(outstr, "# %30s config file version %-10s #\n", GQ_APPNAME, VERSION); g_string_append_printf(outstr, "######################################################################\n"); WRITE_SEPARATOR(); WRITE_STRING("# Note: This file is autogenerated. Options can be changed here,\n"); WRITE_STRING("# but user comments and formatting will be lost.\n"); WRITE_SEPARATOR(); WRITE_STRING("-->\n"); WRITE_SEPARATOR(); WRITE_STRING("<gq>\n"); indent++; WRITE_NL(); WRITE_STRING("<global\n"); indent++; write_global_attributes(outstr, indent + 1); indent--; WRITE_STRING(">\n"); indent++; write_color_profile(outstr, indent); WRITE_SEPARATOR(); filter_write_list(outstr, indent); WRITE_SEPARATOR(); keyword_tree_write_config(outstr, indent); indent--; WRITE_NL(); WRITE_STRING("</global>\n"); WRITE_SEPARATOR(); /* Layout Options */ work = layout_window_list; while (work) { LayoutWindow *lw = work->data; layout_write_config(lw, outstr, indent); work = work->next; } indent--; WRITE_NL(); WRITE_STRING("</gq>\n"); WRITE_SEPARATOR(); secure_fputs(ssi, outstr->str); g_string_free(outstr, TRUE); if (secure_close(ssi)) { log_printf(_("error saving config file: %s\nerror: %s\n"), utf8_path, secsave_strerror(secsave_errno)); return FALSE; } return TRUE; }