void power(const char *arg) { int power; dev_open(); if (arg) { if (sscanf(arg, "%u", &power) < 1) { ERROR_MSG("Invalid power level. Got \"%s\", expected integer in the range of %d-%d.", arg, PCIMAXFM_POWER_MIN, PCIMAXFM_POWER_MAX); } if (power < PCIMAXFM_POWER_MIN || power > PCIMAXFM_POWER_MAX) { ERROR_MSG("Power level out of range. Got %d, expected %d-%d.", power, PCIMAXFM_POWER_MIN, PCIMAXFM_POWER_MAX); } if (ioctl(fd, PCIMAXFM_POWER_SET, &power) == -1) { ERROR_MSG("Setting power level failed."); } } else { if (ioctl(fd, PCIMAXFM_POWER_GET, &power) == -1) { ERROR_MSG("Reading power level failed."); } if (power == PCIMAXFM_POWER_NA) { NOTICE_MSG("Power level not set yet."); return; } } NOTICE_MSG("Power level: %d/%d", power, PCIMAXFM_POWER_MAX); }
void rds(char *arg) { int c; char *val, err[0xff]; struct pcimaxfm_rds_set rds_set; while (*arg != '\0') { if ((c = (getsubopt(&arg, rds_params_name, &val))) == -1) ERROR_MSG("Invalid RDS parameter \"%s\".", val); if (validate_rds(c, val, sizeof(err), err)) { ERROR_MSG("%s", err); } dev_open(); rds_set.param = c; rds_set.value = val; if(ioctl(fd, PCIMAXFM_RDS_SET, &rds_set) == -1) { ERROR_MSG("Writing RDS parameter %s = \"%s\" failed.", rds_params_name[rds_set.param], rds_set.value); } NOTICE_MSG("RDS: %-4s = \"%s\"", rds_params_name[rds_set.param], rds_set.value); } }
int main(int argc, char *argv[]) { int error = EXIT_SUCCESS; pthread_t threads[nb_threads]; unsigned int i; program = argv[0]; initializeLogger(0); DEBUG_VAR(program,"%s"); for(i=0;i<nb_threads;i++) { DEBUG_VAR(i,"%d"); error = pthread_create(&threads[i],NULL,thread,(void*)nb_loop); if (error != 0) { ERROR_MSG("pthread_create %d error %d",i,error); threads[i] = 0; } } for(i=0;i<nb_threads;i++) { if (threads[i] != 0) { void *thread_return = NULL; NOTICE_MSG("waiting for %d ending...",i); error = pthread_join(threads[i],&thread_return); DEBUG_VAR(error,"%d"); } } return error; }
void freq(const char *arg) { int freq; double ffreq; dev_open(); if (arg) { if (sscanf(arg, "%lf", &ffreq) < 1) { ERROR_MSG("Invalid frequency. Got \"%s\", expected floating point number in the range of %.2f-%.2f or integer in the range of %d-%d.", arg, FREQ(PCIMAXFM_FREQ_MIN), FREQ(PCIMAXFM_FREQ_MAX), PCIMAXFM_FREQ_MIN, PCIMAXFM_FREQ_MAX); } freq = (int)ffreq; if (freq < PCIMAXFM_FREQ_MIN) { freq = (int)(ffreq * 20.0f); } if (freq < PCIMAXFM_FREQ_MIN || freq > PCIMAXFM_FREQ_MAX) { ERROR_MSG("Frequency out of range. Got %.2f, expected %.2f-%.2f or %d-%d.", ffreq, FREQ(PCIMAXFM_FREQ_MIN), FREQ(PCIMAXFM_FREQ_MAX), PCIMAXFM_FREQ_MIN, PCIMAXFM_FREQ_MAX); } if (ioctl(fd, PCIMAXFM_FREQ_SET, &freq) == -1) { ERROR_MSG("Setting frequency failed."); } } else { if (ioctl(fd, PCIMAXFM_FREQ_GET, &freq) == -1) { ERROR_MSG("Reading frequency failed."); } if (freq == PCIMAXFM_FREQ_NA) { NOTICE_MSG("Frequency not set yet."); return; } } NOTICE_MSG("Frequency: %.2f MHz (%d 50 KHz steps)", FREQ(freq), freq); }
static inline int sendChildrenOutputsToSyslog(const pid_t child, int stdoutReadPipe, int stderrReadPipe, int eventsReadPipe, int stdOutLogLevel, int stdErrLogLevel) { int error = EXIT_SUCCESS; outputToSyslogParam stdoutParams, stderrParams; pthread_t stdoutThread, stderrThread; stdoutParams.pid = child; stdoutParams.pipe = stdoutReadPipe; stdoutParams.level = stdOutLogLevel; stderrParams.pid = child; stderrParams.pipe = stderrReadPipe; stderrParams.level = stdErrLogLevel; error = pthread_create(&stdoutThread, NULL, sendChildOutputToSyslogThread, &stdoutParams); if (0 == error) { error = pthread_create(&stderrThread, NULL, sendChildOutputToSyslogThread, &stderrParams); if (0 == error) { int childStatus = -1; const pid_t pid = waitpid(child, &childStatus, 0); if (pid != -1) { void *pthreadReturn = NULL; DEBUG_MSG("child ended"); if (WIFEXITED(childStatus)) { const int exitStatus = WEXITSTATUS(childStatus); INFO_MSG("process %d ended with status %d", child, exitStatus); } else if WIFSIGNALED(childStatus) { const int signalNumber = WTERMSIG(childStatus); if (WCOREDUMP(childStatus)) { NOTICE_MSG("child process %d terminated by signal %d (core dumped)", child, signalNumber); } else { NOTICE_MSG("child process %d terminated by signal %d", child, signalNumber); } } error = pthread_join(stdoutThread, &pthreadReturn); if (error != 0) { ERROR_MSG("pthread_join stdoutThread error %d (%m)", error); } error = pthread_join(stderrThread, &pthreadReturn); if (error != 0) { ERROR_MSG("pthread_join stderrThread error %d (%m)", error); } } else {
void device(char *arg) { if (arg) { if (fd) { ERROR_MSG("File descriptor in use. Set device before any query options."); } dev = arg; DEBUG_MSG("Using device \"%s\".", dev); } else { NOTICE_MSG("Using device \"%s\".", dev); } }
void rds_signal(char *arg) { int signal; dev_open(); if (arg) { if (sscanf(arg, "%u", &signal) < 1 || signal < 0 || signal > 1) { ERROR_MSG("Invalid RDS signal state. Got \"%s\", expected integer 1 or 0.", arg); } if (ioctl(fd, PCIMAXFM_RDSSIGNAL_SET, &signal) == -1) { ERROR_MSG("Setting RDS signal state failed."); } } else { if (ioctl(fd, PCIMAXFM_RDSSIGNAL_GET, &signal) == -1) { ERROR_MSG("Reading RDS signal state failed."); } } NOTICE_MSG("RDS signal: %s", PCIMAXFM_STR_BOOL(signal)); }
void stereo(char *arg) { int stereo; dev_open(); if (arg) { if (sscanf(arg, "%u", &stereo) < 1 || stereo < 0 || stereo > 1) { ERROR_MSG("Invalid stereo encoder state. Got \"%s\", expected integer 1 or 0.", arg); } if (ioctl(fd, PCIMAXFM_STEREO_SET, &stereo) == -1) { ERROR_MSG("Setting stereo encoder state failed."); } } else { if (ioctl(fd, PCIMAXFM_STEREO_GET, &stereo) == -1) { ERROR_MSG("Reading stereo encoder state failed."); } } NOTICE_MSG("Stereo encoder: %s", PCIMAXFM_STR_BOOL(stereo)); }
void tx(char *arg) { int tx; dev_open(); if (arg) { if (sscanf(arg, "%u", &tx) < 1 || tx < 0 || tx > 1) { ERROR_MSG("Invalid transmitter power state. Got \"%s\", expected integer 1 or 0.", arg); } if (ioctl(fd, PCIMAXFM_TX_SET, &tx) == -1) { ERROR_MSG("Setting transmitter power state failed."); } } else { if (ioctl(fd, PCIMAXFM_TX_GET, &tx) == -1) { ERROR_MSG("Reading transmitter power state failed."); } } NOTICE_MSG("Transmitter: %s", PCIMAXFM_STR_BOOL(tx)); }
static int jffs3_rename (struct inode *old_dir_i, struct dentry *old_dentry, struct inode *new_dir_i, struct dentry *new_dentry) { int ret; struct jffs3_sb_info *c = JFFS3_SB_INFO(old_dir_i->i_sb); struct jffs3_inode_info *victim_f = NULL; uint8_t type; /* The VFS will check for us and prevent trying to rename a * file over a directory and vice versa, but if it's a directory, * the VFS can't check whether the victim is empty. The filesystem * needs to do that for itself. */ DBG_VFS(1, "entering\n"); if (new_dentry->d_inode) { victim_f = JFFS3_INODE_INFO(new_dentry->d_inode); if (S_ISDIR(new_dentry->d_inode->i_mode)) { struct jffs3_full_dirent *fd; down(&victim_f->sem); for (fd = victim_f->dents; fd; fd = fd->next) { if (fd->ino) { up(&victim_f->sem); return -ENOTEMPTY; } } up(&victim_f->sem); } } /* XXX: We probably ought to alloc enough space for both nodes at the same time. Writing the new link, then getting -ENOSPC, is quite bad :) */ /* Make a hard link */ /* XXX: This is ugly */ type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12; if (!type) type = DT_REG; ret = jffs3_do_link(c, JFFS3_INODE_INFO(new_dir_i), old_dentry->d_inode->i_ino, type, new_dentry->d_name.name, new_dentry->d_name.len); if (ret) return ret; if (victim_f) { /* There was a victim. Kill it off nicely */ new_dentry->d_inode->i_nlink--; /* Don't oops if the victim was a dirent pointing to an inode which didn't exist. */ if (victim_f->inocache) { down(&victim_f->sem); victim_f->inocache->nlink--; up(&victim_f->sem); } } /* If it was a directory we moved, and there was no victim, increase i_nlink on its new parent */ if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f) new_dir_i->i_nlink++; /* Unlink the original */ ret = jffs3_do_unlink(c, JFFS3_INODE_INFO(old_dir_i), old_dentry->d_name.name, old_dentry->d_name.len, NULL); /* We don't touch inode->i_nlink */ if (ret) { /* Oh shit. We really ought to make a single node which can do both atomically */ struct jffs3_inode_info *f = JFFS3_INODE_INFO(old_dentry->d_inode); down(&f->sem); old_dentry->d_inode->i_nlink++; if (f->inocache) f->inocache->nlink++; up(&f->sem); NOTICE_MSG("(rename): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret); /* Might as well let the VFS know */ d_instantiate(new_dentry, old_dentry->d_inode); atomic_inc(&old_dentry->d_inode->i_count); return ret; } if (S_ISDIR(old_dentry->d_inode->i_mode)) old_dir_i->i_nlink--; return 0; }
void vfileLogger(int priority, const char *format, va_list optional_arguments) { int n = 0; int error = EXIT_SUCCESS; const int saved_errno = errno; int internalError = EXIT_SUCCESS; const int LogMask = setlogmask(0); /* Check priority against setlogmask values. */ if ((LOG_MASK (LOG_PRI (priority)) & LogMask) != 0) { //char logMsg[2048]; //char logFormat[1024]; //char *cursor = logFormat; char *buf = 0; size_t bufsize = 1024; FILE *f = open_memstream(&buf, &bufsize); if (f != NULL) { struct tm now_tm; time_t now; (void) time(&now); /*cursor += strftime(cursor,sizeof(logFormat),"%h %e %T ",localtime_r(&now, &now_tm));*/ n = strftime(f->_IO_write_ptr,f->_IO_write_end - f->_IO_write_ptr,"%h %e %T ",localtime_r(&now, &now_tm)); //f->_IO_write_ptr += strftime(f->_IO_write_ptr,f->_IO_write_end - f->_IO_write_ptr,"%h %e %T ",localtime_r(&now, &now_tm)); f->_IO_write_ptr += n; if (LogTag) { /*cursor += sprintf (cursor,"%s: ",LogTag);*/ n += fprintf(f,"%s: ",LogTag); } if (LogStat & LOG_PID) { if (LogStat & LOG_TID) { const pid_t tid = gettid(); /*cursor += sprintf (cursor, "[%d:%d]", (int) getpid (),(int) tid);*/ n += fprintf(f,"[%d:%d]", (int) getpid (),(int) tid); } else { /*cursor += sprintf (cursor, "[%d]", (int) getpid ());*/ n += fprintf(f,"[%d]", (int) getpid ()); } } if (LogStat & LOG_RDTSC) { const unsigned long long int t = rdtsc(); /*cursor += sprintf (cursor, "(%llu)",t);*/ n += fprintf(f,"(%llu)",t); } /* (LogStat & LOG_RDTSC) */ if (LogStat & LOG_CLOCK) { #if HAVE_CLOCK_GETTIME struct timespec timeStamp; if (clock_gettime(CLOCK_MONOTONIC,&timeStamp) == 0) { /*cursor += sprintf (cursor, "(%lu.%.9d)",timeStamp.tv_sec,timeStamp.tv_nsec);*/ n += fprintf(f,"(%lu.%.9d)",timeStamp.tv_sec,timeStamp.tv_nsec); } else { const int error = errno; ERROR_MSG("clock_gettime CLOCK_MONOTONIC error %d (%m)",error); } #else static unsigned int alreadyPrinted = 0; /* to avoid to print this error msg on each call */ if (unlikely(0 == alreadyPrinted)) { ERROR_MSG("clock_gettime not available on this system"); alreadyPrinted = 1; } #endif } /* (LogStat & LOG_CLOCK) */ if (LogStat & LOG_LEVEL) { switch(LOG_PRI(priority)) { case LOG_EMERG: /*cursor += snprintf(cursor,sizeof(logFormat) - (cursor - logFormat),"* Emergency * %s",format);*/ n += fprintf(f, "[EMERG]"); break; case LOG_ALERT: /*cursor += snprintf(cursor,sizeof(logFormat) - (cursor - logFormat),"* Alert * %s",format);*/ n += fprintf(f, "[ALERT]"); break; case LOG_CRIT: /*cursor += snprintf(cursor,sizeof(logFormat) - (cursor - logFormat),"* Critical * %s",format);*/ n += fprintf(f, "[CRIT]"); break; case LOG_ERR: /*cursor += snprintf(cursor,sizeof(logFormat) - (cursor - logFormat),"* Error * %s",format);*/ n += fprintf(f, "[ERROR]"); break; case LOG_WARNING: /*cursor += snprintf(cursor,sizeof(logFormat) - (cursor - logFormat),"* Warning * %s",format);*/ n += fprintf(f, "[WARNING]"); break; case LOG_NOTICE: /*cursor += snprintf(cursor,sizeof(logFormat) - (cursor - logFormat),"* Notice * %s",format); */ n += fprintf(f, "[NOTICE]"); break; case LOG_INFO: /*cursor += snprintf(cursor,sizeof(logFormat) - (cursor - logFormat),"* Info * %s",format);*/ n += fprintf(f, "[INFO]"); break; case LOG_DEBUG: /*cursor += snprintf(cursor,sizeof(logFormat) - (cursor - logFormat),"* Debug * %s",format);*/ n += fprintf(f, "[DEBUG]"); break; default: /*cursor += sprintf(cursor,"* <%d> * %s",priority,format);*/ n += fprintf(f,"[<%d>]",priority); } /* switch(priority) */ } /* (LogStat & LOG_LEVEL) */ errno = saved_errno; /* restore errno for %m format */ n += vfprintf(f, format, optional_arguments); /* Close the memory stream; this will finalize the data into a malloc'd buffer in BUF. */ fclose(f); /* * begin of the critical section. * Some of the function used below are thread cancellation points * (according to Advanced Programming in the Unix Environment 2nd ed p411) * so set the handler to avoid deadlocks and memory leaks */ cleanup_args cancelArgs; cancelArgs.buffer = buf; pthread_cleanup_push(cancel_handler, &cancelArgs); error = pthread_mutex_lock(&fileLock); if (likely(EXIT_SUCCESS == error)) { if (unlikely(LogStat & LOG_PERROR)) { /* add the format argument to the header */ const size_t n = strlen(format); const size_t size = bufsize + n + 1; char *logFormatBuffer = (char *)malloc(size); if (logFormatBuffer) { pthread_cleanup_push(free,logFormatBuffer); strcpyNcat(logFormatBuffer,buf,format); vfprintf(stderr, logFormatBuffer, optional_arguments); pthread_cleanup_pop(1); /* pop the handler and free the allocated memory */ } else { ERROR_MSG("failed to allocate %u bytes to print the msg on stderr",size); } } /* (LogStat & LOG_PERROR) */ /* file management */ if (unlikely(LOG_FILE_DURATION & LogStat)) { const time_t currentTime = time(NULL); const time_t elapsedTime = currentTime - startTime; if (unlikely(elapsedTime >= maxDuration)) { close(logFile); logFile = -1; } } /* (LOG_FILE_DURATION & LogStat) */ if (unlikely(-1 == logFile)) { error = createFile(); /* error already logged */ } if (likely(EXIT_SUCCESS == error)) { const ssize_t written = write(logFile,buf,n); if (written > 0) { if (unlikely(written != n)) { ERROR_MSG("only %d byte(s) of %d has been written to %s",written,n,fullFileName); if (LogStat & LOG_CONS) { int fd = open("/dev/console", O_WRONLY | O_NOCTTY, 0); if (fd >= 0 ) { dprintf(fd,"logMsg"); close(fd); fd = -1; } } /* (LogStat & LOG_CONS) */ if (unlikely((LogStat & LOG_FILE_SYNC_ON_ERRORS_ONLY) && (LOG_PRI(priority) <= LOG_ERR))) { /* flush data if the log priority is "upper" or equal to error */ error = fdatasync(logFile); if (error != 0) { error = errno; ERROR_MSG("fdatasync to %s error %d (%m)",fullFileName,error); } error = posix_fadvise(logFile, 0,0,POSIX_FADV_DONTNEED); /* tell the OS that log message bytes could be released from the file system cache */ if (error != 0) { ERROR_MSG("posix_fadvise to %s error %d (%m)",fullFileName,error); } } /* (unlikely((LogStat & LOG_FILE_SYNC_ON_ERRORS_ONLY) && (LOG_PRI(priority) <= LOG_ERR))) */ } /* (unlikely(written != n)) */ fileSize += written; #ifdef FILESYSTEM_PAGE_CACHE_FLUSH_THRESHOLD static size_t currentPageCacheMaxSize = 0; currentPageCacheMaxSize += written; if (currentPageCacheMaxSize >= FILESYSTEM_PAGE_CACHE_FLUSH_THRESHOLD) { /* tell the OS that log message bytes could be released from the file system cache */ if (likely(posix_fadvise(logFile, 0,0,POSIX_FADV_DONTNEED) == 0)) { currentPageCacheMaxSize = 0; DEBUG_MSG("used file system cache allowed to be flushed (size was %u)",currentPageCacheMaxSize); } else { NOTICE_MSG("posix_fadvise to %s error %d (%m), current page cache max size is %u",fullFileName,error,currentPageCacheMaxSize); } } #endif /* FILESYSTEM_PAGE_CACHE_FLUSH_THRESHOLD */ if ((LOG_FILE_ROTATE & LogStat) || (LOG_FILE_HISTO & LogStat)) { if (fileSize >= maxSize) { close(logFile); logFile = -1; } } } else if (0 == written) { WARNING_MSG("nothing has been written in %s", fullFileName); } else { error = errno; ERROR_MSG("write to %s error %d (%m)", fullFileName, error); } } /*(likely(EXIT_SUCCESS == error)) */ /* End of critical section. */ /*pthread_cleanup_pop(0); implementation can't allow to set this instruction here */ /* free allocated ressources */ internalError = pthread_mutex_unlock(&fileLock); if (internalError != EXIT_SUCCESS) { ERROR_MSG("pthread_mutex_lock fileLock error %d (%s)", internalError, strerror(internalError)); if (EXIT_SUCCESS == error) { error = internalError; } } /* (internalError != EXIT_SUCCESS) */ } else { ERROR_MSG("pthread_mutex_lock fileLock error %d (%s)", error, strerror(error)); } pthread_cleanup_pop(0); /* moved to avoid a build error, use the preprocessor to understand why * or have a look on man 3 pthread_cleanup_push: * push & pop MUST BE in the SAME lexical nesting level !!! */ free(buf); cancelArgs.buffer = buf = NULL; } else { /* open_memstream(&buf, &bufsize) == NULL */ ERROR_MSG("failed to get stream buffer"); /* TMP! display the raw message to the console */ vfprintf(stderr,format,optional_arguments); /* TODO: write the raw message to the file */ } } /* ((LOG_MASK (LOG_PRI (priority)) & LogMask) != 0) */ /* message has not to be displayed because of the current LogMask and its priority */ /*return n;*/ }
int InternalDatabaseReader::getQueryResults(CorbaClientAdaptor &adaptor) { int error(EXIT_SUCCESS); MutexMgr mutexMgr(mutex); const unsigned int numberOfColumns(sqlite3_column_count(SQLQueryStatement)); adaptor.setNumberOfSubItems(numberOfColumns); // set Labels'name corresponding to the current query'fields for(unsigned int i=0;i<numberOfColumns;i++) { const char *label = sqlite3_column_name(SQLQueryStatement,i); DEBUG_MSG("label %u = %s",i,label); adaptor.setLabel(i,label); } // retrieve the result of the current query unsigned int row(0); std::string errorMsg; unsigned int nbRetries(0); do { error = sqlite3_step(SQLQueryStatement); DEBUG_VAR(error,"%d"); switch(error) { case SQLITE_DONE: DEBUG_MSG("SQLITE_DONE"); break; case SQLITE_ROW: { for(unsigned int column=0;column<numberOfColumns;column++) { const int cellDataType = sqlite3_column_type(SQLQueryStatement,column); DEBUG_VAR(cellDataType,"%d"); switch(cellDataType) { case SQLITE_INTEGER: { const int value = sqlite3_column_int(SQLQueryStatement,column); DEBUG_VAR(value,"%d"); adaptor.setValue(row,column,value); } break; case SQLITE_FLOAT: { const double value = sqlite3_column_double(SQLQueryStatement,column); DEBUG_VAR(value,"%f"); adaptor.setValue(row,column,value); } break; case SQLITE_TEXT: { const char *value = (char *)sqlite3_column_text(SQLQueryStatement,column); DEBUG_VAR(value,"%s"); adaptor.setValue(row,column,value); } break; case SQLITE_BLOB: NOTICE_MSG("BLOB (not yet supported)"); break; case SQLITE_NULL: DEBUG_MSG("NULL"); // do Nothing break; } //switch(cellDataType) } //for(unsigned int column=0;column<numberOfColumns;column++) } //case SQLITE_ROW break; case SQLITE_BUSY: DEBUG_MSG("SQLITE_BUSY"); nbRetries++; WARNING_MSG("error during query execution, database is lock, retrying...(%u)",nbRetries); break; case SQLITE_ERROR: { const char *msg = sqlite3_errmsg(database); ERROR_MSG("SQLITE_ERROR %s",msg); errorMsg = msg; } break; case SQLITE_MISUSE: { const char *msg = sqlite3_errmsg(database); ERROR_MSG("SQLITE_MISUSE %s",msg); errorMsg = msg; } break; default: { const char *msg = sqlite3_errmsg(database); ERROR_MSG("SQLite return code %d, %s",msg); errorMsg = msg; } break; } // switch(error) ++row; DEBUG_MSG("new line (%u)",row); } while((SQLITE_ROW == error) || ((SQLITE_BUSY == error) && (nbRetries < NB_MAX_RETRIES))); sqlite3_reset(SQLQueryStatement); if (error != SQLITE_DONE) { throw exception(error,errorMsg); } return error; }