/* End playing and cleanup. */ static void server_shutdown () { logit ("Server exiting..."); audio_exit (); tags_cache_save (&tags_cache, create_file_name("tags_cache")); tags_cache_destroy (&tags_cache); unlink (socket_name()); unlink (create_file_name(PID_FILE)); close (wake_up_pipe[0]); close (wake_up_pipe[1]); logit ("Server exited"); log_close (); }
/* Find path to the theme for the given name. Returned memory is static. */ static char *find_theme_file (const char *name) { static char path[PATH_MAX]; path[sizeof(path)-1] = 0; if (name[0] == '/') { /* Absolute path */ strncpy (path, name, sizeof(path)); if (path[sizeof(path)-1]) interface_fatal ("Theme path too long!"); return path; } /* Try the user directory */ if (snprintf(path, sizeof(path), "%s/%s", create_file_name("themes"), name) >= (int)sizeof(path)) interface_fatal ("Theme path too long!"); if (file_exists(path)) return path; /* Try the system directory */ if (snprintf(path, sizeof(path), "%s/%s", SYSTEM_THEMES_DIR, name) >= (int)sizeof(path)) interface_fatal ("Theme path too long!"); if (file_exists(path)) return path; /* File related to the current directory? */ strncpy (path, name, sizeof(path)); if (path[sizeof(path)-1]) interface_fatal ("Theme path too long!"); return path; }
bool read_image(unsigned char ***file_buffer) { bool ok = true; /* Open input file. */ char *in_file = NULL; if (ok) ok = (in_file = create_file_name(INPUT, -1)) != NULL; FILE *in_fp = NULL; if (ok) { ok = (in_fp = fopen(in_file, "rb")) != NULL; if (!ok) perror(in_file); // else // printf("Input file opened.\n"); } free(in_file); /* Allocate memory for file buffer. */ if (ok) ok = alloc_uchar_array(file_buffer, HEIGHT, WIDTH, CHANNELS); /* Read image data one line at a time. */ unsigned int i; for (i = 0; ok && i < HEIGHT; i++) { ok = fread((*file_buffer)[i], 1, WIDTH * CHANNELS, in_fp) == WIDTH * CHANNELS; if (!ok) perror("read_image"); } /* Close input file. */ fclose(in_fp); // if (ok) // printf("Image read.\n"); /* If an error occurs, free allocated memory. */ if (!ok) dealloc_uchar_array(file_buffer); return ok; }
static void write_pid_file () { char *fname = create_file_name (PID_FILE); FILE *file; if ((file = fopen(fname, "w")) == NULL) fatal ("Can't open pid file for writing: %s", xstrerror (errno)); fprintf (file, "%d\n", getpid()); fclose (file); }
/* End playing and cleanup. */ static void server_shutdown () { logit ("Server exiting..."); audio_exit (); tags_cache_free (tags_cache); tags_cache = NULL; unlink (socket_name()); unlink (create_file_name(PID_FILE)); close (wake_up_pipe[0]); close (wake_up_pipe[1]); logit ("Server exited"); log_close (); }
/* Check if there is a pid file and if it is valid, return the pid, else 0 */ static pid_t check_pid_file () { FILE *file; pid_t pid; char *fname = create_file_name (PID_FILE); /* Read the pid file */ if ((file = fopen(fname, "r")) == NULL) return 0; if (fscanf(file, "%d", &pid) != 1) { fclose (file); return 0; } fclose (file); return pid; }
static void softmixer_write_config() { char *cfname = create_file_name(SOFTMIXER_SAVE_FILE); FILE *cf = fopen(cfname, "w"); if(cf==NULL) { logit ("Unable to write softmixer configuration"); return; } fprintf(cf, "%s %i\n", SOFTMIXER_CFG_ACTIVE, active); fprintf(cf, "%s %i\n", SOFTMIXER_CFG_AMP, mixer_amp); fprintf(cf, "%s %i\n", SOFTMIXER_CFG_VALUE, mixer_val); fprintf(cf, "%s %i\n", SOFTMIXER_CFG_MONO, mix_mono); fclose(cf); logit ("Softmixer configuration written"); }
/* Check if a directory ./.moc exists and create if needed. */ static void check_moc_dir () { char *dir_name = create_file_name (""); struct stat file_stat; /* strip trailing slash */ dir_name[strlen(dir_name)-1] = 0; if (stat (dir_name, &file_stat) == -1) { if (errno != ENOENT) fatal ("Error trying to check for "CONFIG_DIR" directory: %s", xstrerror (errno)); if (mkdir (dir_name, 0700) == -1) fatal ("Can't create directory %s: %s", dir_name, xstrerror (errno)); } else { if (!S_ISDIR(file_stat.st_mode) || access (dir_name, W_OK)) fatal ("%s is not a writable directory!", dir_name); } }
/* Initialize the server - return fd of the listening socket or -1 on error */ void server_init (int debugging, int foreground) { struct sockaddr_un sock_name; pid_t pid; logit ("Starting MOC Server"); assert (server_sock == -1); pid = check_pid_file (); if (pid && valid_pid(pid)) { fprintf (stderr, "\nIt seems that the server is already running" " with pid %d.\n", pid); fprintf (stderr, "If it is not true, remove the pid file (%s)" " and try again.\n", create_file_name(PID_FILE)); fatal ("Exiting!"); } if (foreground) log_init_stream (stdout, "stdout"); else { FILE *logfp; logfp = NULL; if (debugging) { logfp = fopen (SERVER_LOG, "a"); if (!logfp) fatal ("Can't open server log file: %s", xstrerror (errno)); } log_init_stream (logfp, SERVER_LOG); } if (pipe(wake_up_pipe) < 0) fatal ("pipe() failed: %s", xstrerror (errno)); unlink (socket_name()); /* Create a socket. * For reasons why AF_UNIX is the correct constant to use in both * cases, see the commentary the SVN log for commit r9999. */ server_sock = socket (AF_UNIX, SOCK_STREAM, 0); if (server_sock == -1) fatal ("Can't create socket: %s", xstrerror (errno)); sock_name.sun_family = AF_UNIX; strcpy (sock_name.sun_path, socket_name()); /* Bind to socket */ if (bind(server_sock, (struct sockaddr *)&sock_name, SUN_LEN(&sock_name)) == -1) fatal ("Can't bind() to the socket: %s", xstrerror (errno)); if (listen(server_sock, 1) == -1) fatal ("listen() failed: %s", xstrerror (errno)); /* Log stack sizes so stack overflows can be debugged. */ log_process_stack_size (); log_pthread_stack_size (); clients_init (); audio_initialize (); tags_cache = tags_cache_new (options_get_int("TagsCacheSize")); tags_cache_load (tags_cache, create_file_name("cache")); server_tid = pthread_self (); xsignal (SIGTERM, sig_exit); xsignal (SIGINT, foreground ? sig_exit : SIG_IGN); xsignal (SIGHUP, SIG_IGN); xsignal (SIGQUIT, sig_exit); xsignal (SIGPIPE, SIG_IGN); xsignal (SIGCHLD, sig_chld); write_pid_file (); if (!foreground) { setsid (); redirect_output (stdin); redirect_output (stdout); redirect_output (stderr); } return; }
/* CAMqa54502 - pass in currency_struct */ int create_frn_send(CURRENCY_STRUCT *currency_struct) { int status = SUCCESS; int num_trans = 0; int num_closed = 0; Arb_numeric debit_amt = ARB_NUMERIC_ZERO; /* total amount of debits */ Arb_numeric credit_amt = ARB_NUMERIC_ZERO; /* total amount of credits */ Arb_numeric result = ARB_NUMERIC_ZERO; CC_FILE_STATUS frec; char buff1[ARB_NUMERIC_LEN]; char buff2[ARB_NUMERIC_LEN]; int retval; /*DENqa62462, created file type*/ int file_type = FILE_FRN_EFT_OUT; memset(&frec, 0, sizeof(CC_FILE_STATUS)); gFile_id = 0; /*gCycle_id = 0;*/ /* Check for, and reprocess any previous failures; i.e., any ** outgoing French files (file_type == FILE_FRN_EFT_OUT) where the ** file_status is FILE_STAT_INUSE or FILE_STAT_PROC_ERR. Since ** we are just starting, and no other task can be running, ** then anything "INUSE" must be from a previous failure. */ /* BUG: Future version should check for previous CREATE failures here. */ /* Create unique file name for new send file using timestamp */ /* Sets globals gFile_date_time and gFile_name */ create_file_name(); /* Create FILE_STATUS entry, Sets global gFile_id */ if (insert_file_status(&gFile_id, gCycle_id, gFile_name) == FAILURE) { emit(FILE_LINE, EFT_FS_FILE_ERR, gFile_name, gFile_id); gContinue_create = 0; return(CREATE_FILE_FAILURE); } /*****************************/ /* *** BEGIN TRANSACTION *** */ /*****************************/ if (arb_begin_tran(Dbpcust1, (char *) arb_get_process_id()) == FAILURE) { emit(FILE_LINE, EFT_BEGIN_TRAN_ERR, "send file"); arb_cancel(Dbpcust1); rollback_file_status(); return(CREATE_FILE_FAILURE); } if (gBalance_billing == 1) { /* Prepare PAYMENT_TRANS table: create rows with the current profiles */ if (create_trans_current_profile() == FAILURE) { arb_rollback_tran(Dbpcust1, (char *) arb_get_process_id()); rollback_file_status(); emit(FILE_LINE, EFT_CURR_PROF_TRANS); return(CREATE_FILE_FAILURE); } } /* Lock and Count sendable transactions */ if (lock_frn_trans(currency_struct) == FAILURE) { arb_rollback_tran(Dbpcust1, (char *) arb_get_process_id()); rollback_file_status(); emit(FILE_LINE, EFT_LOCK_TRANS); return(CREATE_FILE_FAILURE); } emit(FILE_LINE, EFT_CREATE_SEND, gFile_name, gFile_id, gCycle_id); if (create_cycle_entry(gCH_curr.clearing_house_id, gCycle_id, CYCLE_INUSE) <= 0) { arb_rollback_tran(Dbpcust1, (char *) arb_get_process_id()); rollback_file_status(); emit(FILE_LINE, EFT_CYCLE_CREATE_ERR, gCycle_id); return(CREATE_FILE_FAILURE); } if (arb_commit_tran(Dbpcust1) == FAILURE) /*** COMMIT TRANSACTION ***/ { emit(FILE_LINE, EFT_COMMIT_TRAN_ERR, "send file"); arb_cancel(Dbpcust1); rollback_file_status(); return(CREATE_FILE_FAILURE); } emit(FILE_LINE, EFT_BEGIN_PROCESSING, gTransactions, gFile_id); /* Create file, read TRANS records, write to the file, close file */ /* CAMqa54502 - pass in currency_struct */ if ((retval = create_frn_file(&num_trans, &num_closed, &debit_amt, &credit_amt,currency_struct)) <= 0) { emit(FILE_LINE, EFT_FILE_CREATE_ERR, gFile_id); if (unlock_file_status(Dbpadm, gFile_id, gServer_id, file_type, FILE_STAT_PROC_ERR, 0) != 1) { emit(FILE_LINE, EFT_FS_UPDATE_ERR, gFile_id, FILE_STAT_PROC_ERR); } /* update CYCLE_STATUS */ if (update_cycle_status(gCH_curr.clearing_house_id, gCycle_id, CYCLE_ERROR) <= 0) { emit(FILE_LINE, EFT_CYCLE_ERR, "updating", gCH_curr.clearing_house_id, gCycle_id); } if (manual_rollback_trans(gFile_id) != 1) { /* SERIOUS problem. Cannot unset TRANS entries. They may remain ** marked as "PEN" (pending) and will NOT be sent out to clearing ** house until status is changed. */ emit(FILE_LINE, EFT_ROLLBACK_TRANS, gFile_id); } return(CREATE_FILE_FAILURE); } else if (retval == 2) { if (unlock_file_status(Dbpadm, gFile_id, gServer_id, file_type, FILE_STAT_PROC_ERR, 0) != 1) { emit(FILE_LINE, EFT_FS_UPDATE_ERR, gFile_id, FILE_STAT_NEW); } else if (update_cycle_status(gCH_curr.clearing_house_id, gCycle_id, CYCLE_ERROR) <= 0) { emit(FILE_LINE, EFT_CYCLE_ERR, "updating", gCH_curr.clearing_house_id, gCycle_id); } emit(FILE_LINE, EFT_NO_SEND_FILE); return(CREATE_FILE_NODATA); } /* Successfully created output (send) file. Update file & cycle status. */ status = CREATE_FILE_SUCCESS; arb_num_arith(&result, &debit_amt, ARB_NUM_ADD, &credit_amt); if (unlock_file_status(Dbpadm, gFile_id, gServer_id, file_type, FILE_STAT_NEW, 0) != 1) { emit(FILE_LINE, EFT_FS_UPDATE_ERR, gFile_id, FILE_STAT_NEW); status = CREATE_FILE_FAILURE; } else if (update_cycle(gCH_curr.clearing_house_id, gCycle_id, CYCLE_NEW, CYCLE_NEW_STATS, num_trans - num_closed, &result) <= 0) { /* update of CYCLE_STATUS to NEW failed; try to update status for ** both cycle and file to ERROR. Ignore errors updating since if we ** got here, additional updates will probably fail. */ emit(FILE_LINE, EFT_CYCLE_ERR, "updating new", gCH_curr.clearing_house_id, gCycle_id); status = CREATE_FILE_FAILURE; } if (status == CREATE_FILE_FAILURE) { /* We were doing so well! TRANS entries were successfully locked, ** we successfully created output file. And then at the last step, we get ** an error trying to update the FILE_STATUS and/or CYCLE_STATUS ** entries. Let's try mark the file & cycle to ERROR and attempt to manually ** rollback TRANS changes (i.e., reset the current "PEN" transactions ** back to status = NULL so they can be sent out next time around). ** NOTE: Most likely, the following updates and manual rollback will fail. ** A likely problem to have gotten us here is full transaction log. Manual ** intervention may be required! Don't bother checking return status from ** these updates. */ if (unlock_file_status(Dbpadm, gFile_id, gServer_id, file_type, FILE_STAT_NEW, 0) != 1) { emit(FILE_LINE, EFT_FS_UPDATE_ERR, gFile_id, FILE_STAT_PROC_ERR); } /* update CYCLE_STATUS */ if ((status = update_cycle_status(gCH_curr.clearing_house_id, gCycle_id, CYCLE_ERROR)) <= 0) { emit(FILE_LINE, EFT_CYCLE_ERR, "updating", gCH_curr.clearing_house_id, gCycle_id); } if (manual_rollback_trans(gFile_id) != 1) { emit(FILE_LINE, EFT_ROLLBACK_TRANS, gFile_id); } return(CREATE_FILE_FAILURE); } arb_numeric_to_string(&debit_amt, buff1); arb_numeric_to_string(&credit_amt, buff2); if(!VerifyStringLength(buff1,REPORT_AMOUNT_MAX_LENGTH) || !VerifyStringLength(buff2,REPORT_AMOUNT_MAX_LENGTH)) return (CREATE_FILE_FAILURE); /* CONTROL REPORT */ fprintf(RptFileFD,"\n Send File Name: %s\n", gFile_name); fprintf(RptFileFD," ClearHouse Cycle ID File ID # Trans Debit Amt Credit Amt\n"); fprintf(RptFileFD," %10s %8d %8d %8d %18.18s %18.18s\n", gCH_curr.clearing_house_id, gCycle_id, gFile_id, num_trans - num_closed, buff1, buff2); return(CREATE_FILE_SUCCESS); }
bool write_channels(unsigned char (**file_buffer)[CHANNELS], int height, int width) { bool ok = true; /* Create one output buffer per channel. */ unsigned char **channel_buffer[CHANNELS]; unsigned int c; for (c = 0; ok && c < CHANNELS; c++) ok = alloc_uchar_array(&(channel_buffer[c]), height, width, 1); /* Copy each channel from image, with conversion to byte. */ unsigned int i, j; if (ok) for (i = 0; i < height; i++) for (j = 0; j < width; j++) for (c = 0; c < CHANNELS; c++) channel_buffer[c][i][j] = file_buffer[i][j][c]; /* Create filename for each output channel. */ char *out_filename[CHANNELS]; for (c = 0; c < CHANNELS; c++) out_filename[c] = NULL; for (c = 0; ok && c < CHANNELS; c++) { out_filename[c] = create_file_name(OUTPUT, c); ok = out_filename[c] != NULL; } /* Write out each channel to a separate raw file. */ FILE * out_fp[CHANNELS]; for (c = 0; c < CHANNELS; c++) out_fp[c] = NULL; for (c = 0; ok && c < CHANNELS; c++) { out_fp[c] = fopen(out_filename[c], "wb"); ok = out_fp[c] != NULL; if (!ok) perror(out_filename[c]); } for (c = 0; ok && c < CHANNELS; c++) for (i = 0; ok && i < height; i++) ok = fwrite(channel_buffer[c][i], 1, width, out_fp[c]) == width; for (c = 0; c < CHANNELS; c++) { ok = fclose(out_fp[c]) == 0; if (!ok) fprintf(stderr, "Could not close file: %s\n", out_filename[c]); } /* Free memory allocated for channel buffers. */ for (c = 0; c < CHANNELS; c++) dealloc_uchar_array(&(channel_buffer[c])); /* Calculate md5sums. */ // printf("\n"); char command[STRSIZE]; for (c = 0; ok && c < CHANNELS; c++) { // sprintf(command, "md5sum %s %s.tiff", out_filename[c], out_filename[c]); sprintf(command, "md5sum %s", out_filename[c]); // printf("%s\n", command); ok = system(command) == 0; } if (MAKETIFF) { /* Convert output files to tiff format (ImageMagick). */ // printf("\n"); for (c = 0; ok && c < CHANNELS; c++) { // sprintf(command, "raw2tiff -l %d -w %d %s %s.tiff", height, width, out_filename[c], out_filename[c]); sprintf(command, "convert -depth 8 -size %dx%d gray:%s -compress lzw %s.tiff", width, height, out_filename[c], out_filename[c]); // printf("%s\n", command); ok = system(command) == 0; } /* Merge individual channel tiffs to a single tiff (ImageMagick). */ sprintf(command, "convert"); if (ok) { for (c = 0; c < CHANNELS; c++) { strcat(command, " "); strcat(command, out_filename[c]); strcat(command, ".tiff"); } strcat(command, " -combine "); strcat(command, OUTDIR); strcat(command, OUTFILENAME); strcat(command, ".tiff"); // printf("%s\n", command); ok = system(command) == 0; } // printf("\n"); } /* Free memory allocated for filenames. */ for (c = 0; c < CHANNELS; c++) { free(out_filename[c]); out_filename[c] = NULL; } return ok; }
/* Initialize the server - return fd of the listening socket or -1 on error */ int server_init (int debugging, int foreground) { struct sockaddr_un sock_name; int server_sock; int pid; logit ("Starting MOC Server"); pid = check_pid_file (); if (pid && valid_pid(pid)) { fprintf (stderr, "\nIt seems that the server is already running" " with pid %d.\n", pid); fprintf (stderr, "If it is not true, remove the pid file (%s)" " and try again.\n", create_file_name(PID_FILE)); fatal ("Exiting!"); } if (foreground) log_init_stream (stdout, "stdout"); else { FILE *logfp; logfp = NULL; if (debugging) { logfp = fopen (SERVER_LOG, "a"); if (!logfp) fatal ("Can't open server log file: %s", strerror (errno)); } log_init_stream (logfp, SERVER_LOG); } if (pipe(wake_up_pipe) < 0) fatal ("pipe() failed: %s", strerror(errno)); unlink (socket_name()); /* Create a socket */ if ((server_sock = socket (PF_LOCAL, SOCK_STREAM, 0)) == -1) fatal ("Can't create socket: %s", strerror(errno)); sock_name.sun_family = AF_LOCAL; strcpy (sock_name.sun_path, socket_name()); /* Bind to socket */ if (bind(server_sock, (struct sockaddr *)&sock_name, SUN_LEN(&sock_name)) == -1) fatal ("Can't bind() to the socket: %s", strerror(errno)); if (listen(server_sock, 1) == -1) fatal ("listen() failed: %s", strerror(errno)); audio_initialize (); tags_cache_init (&tags_cache, options_get_int("TagsCacheSize")); tags_cache_load (&tags_cache, create_file_name("cache")); clients_init (); server_tid = pthread_self (); thread_signal (SIGTERM, sig_exit); thread_signal (SIGINT, foreground ? sig_exit : SIG_IGN); thread_signal (SIGHUP, SIG_IGN); thread_signal (SIGQUIT, sig_exit); thread_signal (SIGPIPE, SIG_IGN); write_pid_file (); if (!foreground) { setsid (); redirect_output (stdin); redirect_output (stdout); redirect_output (stderr); } return server_sock; }
int main (int argc, char *argv[]) { struct parameters params; lists_t_strs *deferred_overrides; lists_t_strs *args; #ifdef HAVE_UNAME_SYSCALL int rc; struct utsname uts; #endif #ifdef PACKAGE_REVISION logit ("This is Music On Console (revision %s)", PACKAGE_REVISION); #else logit ("This is Music On Console (version %s)", PACKAGE_VERSION); #endif #ifdef CONFIGURATION logit ("Configured:%s", CONFIGURATION); #endif #ifdef HAVE_UNAME_SYSCALL rc = uname (&uts); if (rc == 0) logit ("Running on: %s %s %s", uts.sysname, uts.release, uts.machine); #endif log_command_line (argc, argv); files_init (); if (get_home () == NULL) fatal ("Could not determine user's home directory!"); memset (¶ms, 0, sizeof(params)); options_init (); deferred_overrides = lists_strs_new (4); /* set locale according to the environment variables */ if (!setlocale(LC_ALL, "")) logit ("Could not set locale!"); args = process_command_line (argc, argv, ¶ms, deferred_overrides); if (params.dont_run_iface && params.only_server) fatal ("-c, -a and -p options can't be used with --server!"); if (!params.config_file) params.config_file = xstrdup (create_file_name ("config")); options_parse (params.config_file); if (params.config_file) free (params.config_file); params.config_file = NULL; process_deferred_overrides (deferred_overrides); lists_strs_free (deferred_overrides); deferred_overrides = NULL; check_moc_dir (); io_init (); rcc_init (); decoder_init (params.debug); srand (time(NULL)); if (!params.only_server && params.dont_run_iface) server_command (¶ms, args); else start_moc (¶ms, args); lists_strs_free (args); options_free (); decoder_cleanup (); io_cleanup (); rcc_cleanup (); files_cleanup (); compat_cleanup (); exit (EXIT_SUCCESS); }
static void softmixer_read_config() { char *cfname = create_file_name(SOFTMIXER_SAVE_FILE); FILE *cf = fopen(cfname, "r"); if(cf==NULL) { logit ("Unable to read softmixer configuration"); return; } char *linebuffer=NULL; int tmp; while((linebuffer=read_line(cf))) { if( strncasecmp ( linebuffer , SOFTMIXER_CFG_ACTIVE , strlen(SOFTMIXER_CFG_ACTIVE) ) == 0 ) { if(sscanf(linebuffer, "%*s %i", &tmp)>0) { if(tmp>0) { active = 1; } else { active = 0; } } } if( strncasecmp ( linebuffer , SOFTMIXER_CFG_AMP , strlen(SOFTMIXER_CFG_AMP) ) == 0 ) { if(sscanf(linebuffer, "%*s %i", &tmp)>0) { if(RANGE(SOFTMIXER_MIN, tmp, SOFTMIXER_MAX)) { mixer_amp = tmp; } else { logit ("Tried to set softmixer amplification out of range."); } } } if( strncasecmp ( linebuffer , SOFTMIXER_CFG_VALUE , strlen(SOFTMIXER_CFG_VALUE) ) == 0 ) { if(sscanf(linebuffer, "%*s %i", &tmp)>0) { if(RANGE(0, tmp, 100)) { softmixer_set_value(tmp); } else { logit ("Tried to set softmixer value out of range."); } } } if( strncasecmp ( linebuffer , SOFTMIXER_CFG_MONO , strlen(SOFTMIXER_CFG_MONO) ) == 0 ) { if(sscanf(linebuffer, "%*s %i", &tmp)>0) { if(tmp>0) { mix_mono = 1; } else { mix_mono = 0; } } } free(linebuffer); } fclose(cf); }