diff --git a/python/console/console_output.py b/python/console/console_output.py index 6784c277e95..7e8d67b8577 100644 --- a/python/console/console_output.py +++ b/python/console/console_output.py @@ -138,7 +138,7 @@ class ShellOutputScintilla(QsciScintilla): "## Type help(iface) for more info and list of methods.\n").format(sys.version, socket.gethostname()) ## some translation string for the console header ends without '\n' - ## and the first command in console will be appended at the header text. + ## and the first command in console will be appended at the header text. ## The following code add a '\n' at the end of the string if not present. if txtInit.endswith('\n'): initText = self.setText(txtInit) diff --git a/python/plugins/fTools/tools/doPointDistance.py b/python/plugins/fTools/tools/doPointDistance.py index 4b9cd98b38f..7dad38e6ea0 100644 --- a/python/plugins/fTools/tools/doPointDistance.py +++ b/python/plugins/fTools/tools/doPointDistance.py @@ -151,7 +151,7 @@ class Dialog(QDialog, Ui_Dialog): layer2 = ftools_utils.getVectorLayerByName(line2) if layer1.id() == layer2.id(): if nearest > 0: - nearest = nearest + 1 + nearest = nearest + 1 provider1 = layer1.dataProvider() provider2 = layer2.dataProvider() sindex = QgsSpatialIndex() diff --git a/python/plugins/processing/gui/CouldNotLoadResultsDialog.py b/python/plugins/processing/gui/CouldNotLoadResultsDialog.py index 0b8795b69c1..1b32775142b 100644 --- a/python/plugins/processing/gui/CouldNotLoadResultsDialog.py +++ b/python/plugins/processing/gui/CouldNotLoadResultsDialog.py @@ -40,16 +40,16 @@ class CouldNotLoadResultsDialog(QtGui.QDialog): self.setWindowTitle("Problem loading output layers") layout = QVBoxLayout() browser = QtGui.QTextBrowser() - browser.setOpenLinks(False) + browser.setOpenLinks(False) browser.anchorClicked.connect(self.linkClicked) html = self.alg.getPostProcessingErrorMessage(self.wrongLayers) - browser.setHtml(html) + browser.setHtml(html) button = QPushButton() button.setText("Close") - button.clicked.connect(self.closeButtonPressed) + button.clicked.connect(self.closeButtonPressed) buttonBox = QtGui.QDialogButtonBox() - buttonBox.setOrientation(QtCore.Qt.Horizontal) - buttonBox.addButton(button, QDialogButtonBox.ActionRole) + buttonBox.setOrientation(QtCore.Qt.Horizontal) + buttonBox.addButton(button, QDialogButtonBox.ActionRole) layout.addWidget(browser) layout.addWidget(buttonBox) self.setLayout(layout) diff --git a/python/plugins/processing/gui/HelpEditionDialog.py b/python/plugins/processing/gui/HelpEditionDialog.py index c0583df2b51..4f1a6481e4b 100644 --- a/python/plugins/processing/gui/HelpEditionDialog.py +++ b/python/plugins/processing/gui/HelpEditionDialog.py @@ -38,9 +38,9 @@ class HelpEditionDialog(QDialog, Ui_DlgHelpEdition): def __init__(self, alg): QDialog.__init__(self) - - self.setupUi(self) - + + self.setupUi(self) + self.alg = alg self.descriptions = {} if self.alg.descriptionFile is not None: @@ -68,11 +68,11 @@ class HelpEditionDialog(QDialog, Ui_DlgHelpEdition): pickle.dump(self.descriptions, f) f.close() except Exception, e: - QMessageBox.warning(self, "Error saving help file", + QMessageBox.warning(self, "Error saving help file", "Help file could not be saved." "\nCheck that you have permission to modify the help file.\n" "You might not have permission if you are editing an example\n" - "model or script, since they are stored on the installation folder") + "model or script, since they are stored on the installation folder") QDialog.accept(self) def getHtml(self): diff --git a/python/plugins/processing/gui/MissingDependencyDialog.py b/python/plugins/processing/gui/MissingDependencyDialog.py index c0a7c60d872..871c149ae86 100644 --- a/python/plugins/processing/gui/MissingDependencyDialog.py +++ b/python/plugins/processing/gui/MissingDependencyDialog.py @@ -44,10 +44,10 @@ class MissingDependencyDialog(QtGui.QDialog): browser.setHtml(self.msg) button = QPushButton() button.setText("Close") - button.clicked.connect(self.closeButtonPressed) + button.clicked.connect(self.closeButtonPressed) buttonBox = QtGui.QDialogButtonBox() - buttonBox.setOrientation(QtCore.Qt.Horizontal) - buttonBox.addButton(button, QDialogButtonBox.ActionRole) + buttonBox.setOrientation(QtCore.Qt.Horizontal) + buttonBox.addButton(button, QDialogButtonBox.ActionRole) layout.addWidget(browser) layout.addWidget(buttonBox) self.setLayout(layout) diff --git a/python/plugins/processing/modeler/CalculatorModelerParametersDialog.py b/python/plugins/processing/modeler/CalculatorModelerParametersDialog.py index 17bdb949c23..a0803e31a7f 100644 --- a/python/plugins/processing/modeler/CalculatorModelerParametersDialog.py +++ b/python/plugins/processing/modeler/CalculatorModelerParametersDialog.py @@ -100,7 +100,7 @@ class CalculatorModelerParametersDialog(QtGui.QDialog): self.values = {} self.outputs = {} - name = self.getSafeNameForHarcodedParameter(self.alg.getParameterFromName(self.alg.FORMULA)) + name = self.getSafeNameForHarcodedParameter(self.alg.getParameterFromName(self.alg.FORMULA)) value = AlgorithmAndParameter(AlgorithmAndParameter.PARENT_MODEL_ALGORITHM, name) self.params[self.alg.FORMULA] = value formula = str(self.formulaText.text()) diff --git a/python/plugins/processing/modeler/ModelerParametersDialog.py b/python/plugins/processing/modeler/ModelerParametersDialog.py index 4a159edce72..4d8afb44ec1 100644 --- a/python/plugins/processing/modeler/ModelerParametersDialog.py +++ b/python/plugins/processing/modeler/ModelerParametersDialog.py @@ -283,9 +283,9 @@ class ModelerParametersDialog(QtGui.QDialog): params = self.model.parameters for param in params: if isinstance(param, ParameterExtent): - extents.append(AlgorithmAndParameter(AlgorithmAndParameter.PARENT_MODEL_ALGORITHM, param.name, "", param.description)) - - + extents.append(AlgorithmAndParameter(AlgorithmAndParameter.PARENT_MODEL_ALGORITHM, param.name, "", param.description)) + + if self.algIndex is None: dependent = [] else: @@ -299,7 +299,7 @@ class ModelerParametersDialog(QtGui.QDialog): if isinstance(out, OutputExtent): extents.append(AlgorithmAndParameter(i, out.name, alg.name, out.description)) i+=1 - + return extents def getNumbers(self): diff --git a/python/plugins/processing/modeler/VectorLayerBoundsAlgorithm.py b/python/plugins/processing/modeler/VectorLayerBoundsAlgorithm.py index 1557034c328..b9704d99a37 100644 --- a/python/plugins/processing/modeler/VectorLayerBoundsAlgorithm.py +++ b/python/plugins/processing/modeler/VectorLayerBoundsAlgorithm.py @@ -56,6 +56,6 @@ class VectorLayerBoundsAlgorithm(GeoAlgorithm): self.setOutputValue(self.XMAX, layer.extent().xMaximum()) self.setOutputValue(self.YMIN, layer.extent().yMinimum()) self.setOutputValue(self.YMAX, layer.extent().yMaximum()) - self.setOutputValue(self.EXTENT, + self.setOutputValue(self.EXTENT, (layer.extent().xMinimum(), layer.extent().xMaximum(), layer.extent().yMinimum(), layer.extent().yMaximum())) diff --git a/python/plugins/processing/outputs/OutputExtent.py b/python/plugins/processing/outputs/OutputExtent.py index e082a123135..069b02325e6 100644 --- a/python/plugins/processing/outputs/OutputExtent.py +++ b/python/plugins/processing/outputs/OutputExtent.py @@ -32,7 +32,7 @@ class OutputExtent(Output): self.description = description self.value = None self.hidden = True - + def setValue(self, value): try: if value != None and isinstance(value, basestring): @@ -41,4 +41,4 @@ class OutputExtent(Output): self.value = ",".join([str(v) for v in value]) return True except: - return False + return False diff --git a/python/plugins/processing/outputs/OutputTable.py b/python/plugins/processing/outputs/OutputTable.py index 4a70a8417d3..cbd5a9e4869 100644 --- a/python/plugins/processing/outputs/OutputTable.py +++ b/python/plugins/processing/outputs/OutputTable.py @@ -52,7 +52,7 @@ class OutputTable(Output): return self.value else: if self.compatible is None: - self.compatible = ProcessingUtils.getTempFilenameInTempFolder(self.name + "." + self.getDefaultFileExtension(alg)) + self.compatible = ProcessingUtils.getTempFilenameInTempFolder(self.name + "." + self.getDefaultFileExtension(alg)) return self.compatible; def getTableWriter(self, fields): diff --git a/python/plugins/processing/outputs/OutputVector.py b/python/plugins/processing/outputs/OutputVector.py index 4971a5b634f..09a53bca998 100644 --- a/python/plugins/processing/outputs/OutputVector.py +++ b/python/plugins/processing/outputs/OutputVector.py @@ -55,7 +55,7 @@ class OutputVector(Output): return self.value else: if self.compatible is None: - self.compatible = ProcessingUtils.getTempFilenameInTempFolder(self.name + "." + self.getDefaultFileExtension(alg)) + self.compatible = ProcessingUtils.getTempFilenameInTempFolder(self.name + "." + self.getDefaultFileExtension(alg)) return self.compatible; diff --git a/python/plugins/processing/r/EditRScriptDialog.py b/python/plugins/processing/r/EditRScriptDialog.py index 3f8062ed6d1..4231d6266bf 100644 --- a/python/plugins/processing/r/EditRScriptDialog.py +++ b/python/plugins/processing/r/EditRScriptDialog.py @@ -53,7 +53,7 @@ class EditRScriptDialog(QtGui.QDialog): Qt.WindowMinMaxButtonsHint) self.setWindowTitle("Edit script") layout = QVBoxLayout() - self.text = ScriptEditorWidget(self.alg.script if self.alg is not None else "") + self.text = ScriptEditorWidget(self.alg.script if self.alg is not None else "") self.buttonBox = QtGui.QDialogButtonBox() self.buttonBox.setOrientation(QtCore.Qt.Horizontal) if self.alg != None: @@ -144,7 +144,7 @@ class EditRScriptDialog(QtGui.QDialog): self.close() from PyQt4.Qsci import QsciScintilla - + class ScriptEditorWidget(QsciScintilla): ARROW_MARKER_NUM = 8 @@ -170,6 +170,6 @@ class ScriptEditorWidget(QsciScintilla): self.setCaretLineBackgroundColor(QColor("#ffe4e4")) self.SendScintilla(QsciScintilla.SCI_STYLESETFONT, 1, 'Courier') - + self.setText(text) - + diff --git a/python/plugins/processing/saga/SagaAlgorithm.py b/python/plugins/processing/saga/SagaAlgorithm.py index 65703a71f10..b653003581e 100644 --- a/python/plugins/processing/saga/SagaAlgorithm.py +++ b/python/plugins/processing/saga/SagaAlgorithm.py @@ -306,8 +306,8 @@ class SagaAlgorithm(GeoAlgorithm): if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename2 = ProcessingUtils.tempFolder() + os.sep + os.path.basename(filename) + ".sgrd" - formatIndex = 1 if saga208 else 4 - if ProcessingUtils.isWindows() or ProcessingUtils.isMac() or not saga208: + formatIndex = 1 if saga208 else 4 + if ProcessingUtils.isWindows() or ProcessingUtils.isMac() or not saga208: commands.append("io_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT " + str(formatIndex) +" -TYPE 0 -FILE \"" + filename + "\""); else: commands.append("libio_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\""); @@ -378,12 +378,12 @@ class SagaAlgorithm(GeoAlgorithm): return s - def exportRasterLayer(self, source): + def exportRasterLayer(self, source): layer = QGisLayers.getObjectFromUri(source, False) if layer: filename = str(layer.name()) else: - filename = source.rstrip(".sgrd") + filename = source.rstrip(".sgrd") validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:" filename = ''.join(c for c in filename if c in validChars) if len(filename) == 0: @@ -395,8 +395,8 @@ class SagaAlgorithm(GeoAlgorithm): return "io_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + source+"\"" else: return "libio_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + source + "\"" - - + + def checkBeforeOpeningParametersDialog(self): diff --git a/python/plugins/processing/saga/SagaAlgorithmProvider.py b/python/plugins/processing/saga/SagaAlgorithmProvider.py index 3623bfa5f27..d19fbd6213e 100644 --- a/python/plugins/processing/saga/SagaAlgorithmProvider.py +++ b/python/plugins/processing/saga/SagaAlgorithmProvider.py @@ -45,8 +45,8 @@ class SagaAlgorithmProvider(AlgorithmProvider): def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if ProcessingUtils.isWindows(): - ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_FOLDER, "SAGA folder", SagaUtils.sagaPath())) - ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_208, "Enable SAGA 2.0.8 compatibility", False)) + ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_FOLDER, "SAGA folder", SagaUtils.sagaPath())) + ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_208, "Enable SAGA 2.0.8 compatibility", False)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_AUTO_RESAMPLING, "Use min covering grid system for resampling", True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_LOG_COMMANDS, "Log execution commands", True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_LOG_CONSOLE, "Log console output", True)) diff --git a/python/plugins/processing/script/EditScriptDialog.py b/python/plugins/processing/script/EditScriptDialog.py index e796aea05cd..70e4ca18bad 100644 --- a/python/plugins/processing/script/EditScriptDialog.py +++ b/python/plugins/processing/script/EditScriptDialog.py @@ -52,7 +52,7 @@ class EditScriptDialog(QtGui.QDialog): Qt.WindowMinMaxButtonsHint) self.setWindowTitle("Edit script") layout = QVBoxLayout() - self.text = ScriptEditorWidget(self.alg.script if self.alg is not None else "") + self.text = ScriptEditorWidget(self.alg.script if self.alg is not None else "") self.buttonBox = QtGui.QDialogButtonBox() self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.editHelpButton = QtGui.QPushButton() @@ -120,9 +120,9 @@ class EditScriptDialog(QtGui.QDialog): def cancelPressed(self): #self.update = False self.close() - + from PyQt4.Qsci import QsciScintilla, QsciLexerPython - + class ScriptEditorWidget(QsciScintilla): ARROW_MARKER_NUM = 8 @@ -151,6 +151,6 @@ class ScriptEditorWidget(QsciScintilla): lexer.setDefaultFont(font) self.setLexer(lexer) self.SendScintilla(QsciScintilla.SCI_STYLESETFONT, 1, 'Courier') - + self.setText(text) - + diff --git a/src/app/qgsmeasuredialog.cpp b/src/app/qgsmeasuredialog.cpp index c70c12e19e7..82539ea4bcb 100644 --- a/src/app/qgsmeasuredialog.cpp +++ b/src/app/qgsmeasuredialog.cpp @@ -222,7 +222,7 @@ void QgsMeasureDialog::updateUi() toolTip += "
* " + tr( "Project CRS transformation is turned off." ) + " "; toolTip += tr( "Canvas units setting is taken from project properties setting (%1)." ).arg( QGis::tr( mCanvasUnits ) ); toolTip += "
* " + tr( "Ellipsoidal calculation is not possible, as project CRS is undefined." ); - setWindowTitle( tr("Measure (OTF off)")); + setWindowTitle( tr( "Measure (OTF off)" ) ); } else { @@ -236,7 +236,7 @@ void QgsMeasureDialog::updateUi() toolTip += "
* " + tr( "Project CRS transformation is turned on but ellipsoidal calculation is not selected." ); toolTip += "
* " + tr( "The canvas units setting is taken from the project CRS (%1)." ).arg( QGis::tr( mCanvasUnits ) ); } - setWindowTitle( tr("Measure (OTF on)")); + setWindowTitle( tr( "Measure (OTF on)" ) ); } if (( mCanvasUnits == QGis::Meters && mDisplayUnits == QGis::Feet ) || ( mCanvasUnits == QGis::Feet && mDisplayUnits == QGis::Meters ) ) diff --git a/src/core/qgsfield.h b/src/core/qgsfield.h index f79be608652..33cde7cff28 100644 --- a/src/core/qgsfield.h +++ b/src/core/qgsfield.h @@ -163,7 +163,8 @@ class CORE_EXPORT QgsFields { public: - enum FieldOrigin { + enum FieldOrigin + { OriginUnknown, //!< it has not been specified where the field comes from OriginProvider, //!< field comes from the underlying data provider of the vector layer (originIndex = index in provider's fields) OriginJoin, //!< field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index within the join) diff --git a/src/core/raster/qgsrasterrenderer.cpp b/src/core/raster/qgsrasterrenderer.cpp index 1ff68eed5cc..22a1f294d7d 100644 --- a/src/core/raster/qgsrasterrenderer.cpp +++ b/src/core/raster/qgsrasterrenderer.cpp @@ -195,7 +195,7 @@ QString QgsRasterRenderer::minMaxOriginLabel( int theOrigin ) QString est_exact; QString values; QString extent; - + if ( theOrigin & MinMaxEstimated ) { est_exact = tr( "Estimated" ); @@ -227,11 +227,11 @@ QString QgsRasterRenderer::minMaxOriginLabel( int theOrigin ) extent = tr( "sub extent" ); } - label = QCoreApplication::translate ( "QgsRasterRenderer", "%1 %2 of %3.", - "min/max origin label in raster properties, where %1 - estimated/exact, %2 - values (min/max, stddev, etc.), %3 - extent" ) - .arg(est_exact) - .arg(values) - .arg(extent); + label = QCoreApplication::translate( "QgsRasterRenderer", "%1 %2 of %3.", + "min/max origin label in raster properties, where %1 - estimated/exact, %2 - values (min/max, stddev, etc.), %3 - extent" ) + .arg( est_exact ) + .arg( values ) + .arg( extent ); return label; } diff --git a/tests/src/python/qgis_local_server_spawn/flup/server/ajp_base.py b/tests/src/python/qgis_local_server_spawn/flup/server/ajp_base.py index 52c003db1a7..fef47acab10 100644 --- a/tests/src/python/qgis_local_server_spawn/flup/server/ajp_base.py +++ b/tests/src/python/qgis_local_server_spawn/flup/server/ajp_base.py @@ -717,7 +717,7 @@ class Connection(object): if self._timeout: old_alarm = signal.signal(signal.SIGALRM, self.timeout_handler) signal.alarm(self._timeout) - + # Run Request. req.run() diff --git a/tests/src/python/qgis_local_server_spawn/flup/server/threadedserver.py b/tests/src/python/qgis_local_server_spawn/flup/server/threadedserver.py index 0471ed32b05..b5ae0926ad7 100644 --- a/tests/src/python/qgis_local_server_spawn/flup/server/threadedserver.py +++ b/tests/src/python/qgis_local_server_spawn/flup/server/threadedserver.py @@ -69,7 +69,7 @@ class ThreadedServer(object): # Set close-on-exec setCloseOnExec(sock) - + # Main loop. while self._keepGoing: try: @@ -88,7 +88,7 @@ class ThreadedServer(object): raise setCloseOnExec(clientSock) - + if not self._isClientAllowed(addr): clientSock.close() continue @@ -110,7 +110,7 @@ class ThreadedServer(object): # Return bool based on whether or not SIGHUP was received. return self._hupReceived - + def shutdown(self): """Wait for running threads to finish.""" self._threadPool.shutdown() diff --git a/tests/src/python/qgis_local_server_spawn/flup/server/threadpool.py b/tests/src/python/qgis_local_server_spawn/flup/server/threadpool.py index 61906cbdfa3..51ddfdbea85 100644 --- a/tests/src/python/qgis_local_server_spawn/flup/server/threadpool.py +++ b/tests/src/python/qgis_local_server_spawn/flup/server/threadpool.py @@ -53,14 +53,14 @@ class ThreadPool(object): # Start the minimum number of worker threads. for i in range(maxSpare): self._start_new_thread() - + def _start_new_thread(self): t = threading.Thread(target=self._worker) self._threads.append(t) t.setDaemon(True) t.start() return t - + def shutdown(self): """shutdown all workers.""" self._lock.acquire() @@ -116,7 +116,7 @@ class ThreadPool(object): while True: while not self._workQueue and not self._stop: self._lock.wait() - + if self._stop: return diff --git a/tests/src/python/qgis_local_server_spawn/spawn-fcgi/src/spawn-fcgi.c b/tests/src/python/qgis_local_server_spawn/spawn-fcgi/src/spawn-fcgi.c index 7f3f594105a..1aeb724e6df 100644 --- a/tests/src/python/qgis_local_server_spawn/spawn-fcgi/src/spawn-fcgi.c +++ b/tests/src/python/qgis_local_server_spawn/spawn-fcgi/src/spawn-fcgi.c @@ -60,8 +60,9 @@ typedef int socklen_t; #endif #ifndef HAVE_ISSETUGID -static int issetugid() { - return (geteuid() != getuid() || getegid() != getgid()); +static int issetugid() +{ + return ( geteuid() != getuid() || getegid() != getgid() ); } #endif @@ -79,552 +80,653 @@ static int issetugid() { #define CONST_STR_LEN(s) s, sizeof(s) - 1 -static int bind_socket(const char *addr, unsigned short port, const char *unixsocket, uid_t uid, gid_t gid, int mode, int backlog) { - int fcgi_fd, socket_type, val; +static int bind_socket( const char *addr, unsigned short port, const char *unixsocket, uid_t uid, gid_t gid, int mode, int backlog ) +{ + int fcgi_fd, socket_type, val; - struct sockaddr_un fcgi_addr_un; - struct sockaddr_in fcgi_addr_in; + struct sockaddr_un fcgi_addr_un; + struct sockaddr_in fcgi_addr_in; #ifdef USE_IPV6 - struct sockaddr_in6 fcgi_addr_in6; + struct sockaddr_in6 fcgi_addr_in6; #endif - struct sockaddr *fcgi_addr; + struct sockaddr *fcgi_addr; - socklen_t servlen; + socklen_t servlen; - if (unixsocket) { - memset(&fcgi_addr_un, 0, sizeof(fcgi_addr_un)); + if ( unixsocket ) + { + memset( &fcgi_addr_un, 0, sizeof( fcgi_addr_un ) ); - fcgi_addr_un.sun_family = AF_UNIX; - strcpy(fcgi_addr_un.sun_path, unixsocket); + fcgi_addr_un.sun_family = AF_UNIX; + strcpy( fcgi_addr_un.sun_path, unixsocket ); #ifdef SUN_LEN - servlen = SUN_LEN(&fcgi_addr_un); + servlen = SUN_LEN( &fcgi_addr_un ); #else - /* stevens says: */ - servlen = strlen(fcgi_addr_un.sun_path) + sizeof(fcgi_addr_un.sun_family); + /* stevens says: */ + servlen = strlen( fcgi_addr_un.sun_path ) + sizeof( fcgi_addr_un.sun_family ); #endif - socket_type = AF_UNIX; - fcgi_addr = (struct sockaddr *) &fcgi_addr_un; + socket_type = AF_UNIX; + fcgi_addr = ( struct sockaddr * ) & fcgi_addr_un; - /* check if some backend is listening on the socket - * as if we delete the socket-file and rebind there will be no "socket already in use" error - */ - if (-1 == (fcgi_fd = socket(socket_type, SOCK_STREAM, 0))) { - fprintf(stderr, "spawn-fcgi: couldn't create socket: %s\n", strerror(errno)); - return -1; - } + /* check if some backend is listening on the socket + * as if we delete the socket-file and rebind there will be no "socket already in use" error + */ + if ( -1 == ( fcgi_fd = socket( socket_type, SOCK_STREAM, 0 ) ) ) + { + fprintf( stderr, "spawn-fcgi: couldn't create socket: %s\n", strerror( errno ) ); + return -1; + } - if (0 == connect(fcgi_fd, fcgi_addr, servlen)) { - fprintf(stderr, "spawn-fcgi: socket is already in use, can't spawn\n"); - close(fcgi_fd); - return -1; - } + if ( 0 == connect( fcgi_fd, fcgi_addr, servlen ) ) + { + fprintf( stderr, "spawn-fcgi: socket is already in use, can't spawn\n" ); + close( fcgi_fd ); + return -1; + } - /* cleanup previous socket if it exists */ - if (-1 == unlink(unixsocket)) { - switch (errno) { - case ENOENT: - break; - default: - fprintf(stderr, "spawn-fcgi: removing old socket failed: %s\n", strerror(errno)); - return -1; - } - } + /* cleanup previous socket if it exists */ + if ( -1 == unlink( unixsocket ) ) + { + switch ( errno ) + { + case ENOENT: + break; + default: + fprintf( stderr, "spawn-fcgi: removing old socket failed: %s\n", strerror( errno ) ); + return -1; + } + } - close(fcgi_fd); - } else { - memset(&fcgi_addr_in, 0, sizeof(fcgi_addr_in)); - fcgi_addr_in.sin_family = AF_INET; - fcgi_addr_in.sin_port = htons(port); + close( fcgi_fd ); + } + else + { + memset( &fcgi_addr_in, 0, sizeof( fcgi_addr_in ) ); + fcgi_addr_in.sin_family = AF_INET; + fcgi_addr_in.sin_port = htons( port ); - servlen = sizeof(fcgi_addr_in); - socket_type = AF_INET; - fcgi_addr = (struct sockaddr *) &fcgi_addr_in; + servlen = sizeof( fcgi_addr_in ); + socket_type = AF_INET; + fcgi_addr = ( struct sockaddr * ) & fcgi_addr_in; #ifdef USE_IPV6 - memset(&fcgi_addr_in6, 0, sizeof(fcgi_addr_in6)); - fcgi_addr_in6.sin6_family = AF_INET6; - fcgi_addr_in6.sin6_port = fcgi_addr_in.sin_port; + memset( &fcgi_addr_in6, 0, sizeof( fcgi_addr_in6 ) ); + fcgi_addr_in6.sin6_family = AF_INET6; + fcgi_addr_in6.sin6_port = fcgi_addr_in.sin_port; #endif - if (addr == NULL) { - fcgi_addr_in.sin_addr.s_addr = htonl(INADDR_ANY); + if ( addr == NULL ) + { + fcgi_addr_in.sin_addr.s_addr = htonl( INADDR_ANY ); #ifdef HAVE_INET_PTON - } else if (1 == inet_pton(AF_INET, addr, &fcgi_addr_in.sin_addr)) { - /* nothing to do */ + } + else if ( 1 == inet_pton( AF_INET, addr, &fcgi_addr_in.sin_addr ) ) + { + /* nothing to do */ #ifdef HAVE_IPV6 - } else if (1 == inet_pton(AF_INET6, addr, &fcgi_addr_in6.sin6_addr)) { - servlen = sizeof(fcgi_addr_in6); - socket_type = AF_INET6; - fcgi_addr = (struct sockaddr *) &fcgi_addr_in6; + } + else if ( 1 == inet_pton( AF_INET6, addr, &fcgi_addr_in6.sin6_addr ) ) + { + servlen = sizeof( fcgi_addr_in6 ); + socket_type = AF_INET6; + fcgi_addr = ( struct sockaddr * ) & fcgi_addr_in6; #endif - } else { - fprintf(stderr, "spawn-fcgi: '%s' is not a valid IP address\n", addr); - return -1; + } + else + { + fprintf( stderr, "spawn-fcgi: '%s' is not a valid IP address\n", addr ); + return -1; #else - } else { - if ((in_addr_t)(-1) == (fcgi_addr_in.sin_addr.s_addr = inet_addr(addr))) { - fprintf(stderr, "spawn-fcgi: '%s' is not a valid IPv4 address\n", addr); - return -1; - } + } + else + { + if (( in_addr_t )( -1 ) == ( fcgi_addr_in.sin_addr.s_addr = inet_addr( addr ) ) ) + { + fprintf( stderr, "spawn-fcgi: '%s' is not a valid IPv4 address\n", addr ); + return -1; + } #endif - } - } + } + } - if (-1 == (fcgi_fd = socket(socket_type, SOCK_STREAM, 0))) { - fprintf(stderr, "spawn-fcgi: couldn't create socket: %s\n", strerror(errno)); - return -1; - } + if ( -1 == ( fcgi_fd = socket( socket_type, SOCK_STREAM, 0 ) ) ) + { + fprintf( stderr, "spawn-fcgi: couldn't create socket: %s\n", strerror( errno ) ); + return -1; + } - val = 1; - if (setsockopt(fcgi_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) { - fprintf(stderr, "spawn-fcgi: couldn't set SO_REUSEADDR: %s\n", strerror(errno)); - return -1; - } + val = 1; + if ( setsockopt( fcgi_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof( val ) ) < 0 ) + { + fprintf( stderr, "spawn-fcgi: couldn't set SO_REUSEADDR: %s\n", strerror( errno ) ); + return -1; + } - if (-1 == bind(fcgi_fd, fcgi_addr, servlen)) { - fprintf(stderr, "spawn-fcgi: bind failed: %s\n", strerror(errno)); - return -1; - } + if ( -1 == bind( fcgi_fd, fcgi_addr, servlen ) ) + { + fprintf( stderr, "spawn-fcgi: bind failed: %s\n", strerror( errno ) ); + return -1; + } - if (unixsocket) { - if (0 != uid || 0 != gid) { - if (0 == uid) uid = -1; - if (0 == gid) gid = -1; - if (-1 == chown(unixsocket, uid, gid)) { - fprintf(stderr, "spawn-fcgi: couldn't chown socket: %s\n", strerror(errno)); - close(fcgi_fd); - unlink(unixsocket); - return -1; - } - } + if ( unixsocket ) + { + if ( 0 != uid || 0 != gid ) + { + if ( 0 == uid ) uid = -1; + if ( 0 == gid ) gid = -1; + if ( -1 == chown( unixsocket, uid, gid ) ) + { + fprintf( stderr, "spawn-fcgi: couldn't chown socket: %s\n", strerror( errno ) ); + close( fcgi_fd ); + unlink( unixsocket ); + return -1; + } + } - if (-1 != mode && -1 == chmod(unixsocket, mode)) { - fprintf(stderr, "spawn-fcgi: couldn't chmod socket: %s\n", strerror(errno)); - close(fcgi_fd); - unlink(unixsocket); - return -1; - } - } + if ( -1 != mode && -1 == chmod( unixsocket, mode ) ) + { + fprintf( stderr, "spawn-fcgi: couldn't chmod socket: %s\n", strerror( errno ) ); + close( fcgi_fd ); + unlink( unixsocket ); + return -1; + } + } - if (-1 == listen(fcgi_fd, backlog)) { - fprintf(stderr, "spawn-fcgi: listen failed: %s\n", strerror(errno)); - return -1; - } + if ( -1 == listen( fcgi_fd, backlog ) ) + { + fprintf( stderr, "spawn-fcgi: listen failed: %s\n", strerror( errno ) ); + return -1; + } - return fcgi_fd; + return fcgi_fd; } -static int fcgi_spawn_connection(char *appPath, char **appArgv, int fcgi_fd, int fork_count, int child_count, int pid_fd, int nofork) { - int status, rc = 0; - struct timeval tv = { 0, 100 * 1000 }; +static int fcgi_spawn_connection( char *appPath, char **appArgv, int fcgi_fd, int fork_count, int child_count, int pid_fd, int nofork ) +{ + int status, rc = 0; + struct timeval tv = { 0, 100 * 1000 }; - pid_t child; + pid_t child; - while (fork_count-- > 0) { + while ( fork_count-- > 0 ) + { - if (!nofork) { - child = fork(); - } else { - child = 0; - } + if ( !nofork ) + { + child = fork(); + } + else + { + child = 0; + } - switch (child) { - case 0: { - char cgi_childs[64]; - int max_fd = 0; + switch ( child ) + { + case 0: + { + char cgi_childs[64]; + int max_fd = 0; - int i = 0; + int i = 0; - if (child_count >= 0) { - snprintf(cgi_childs, sizeof(cgi_childs), "PHP_FCGI_CHILDREN=%d", child_count); - putenv(cgi_childs); - } + if ( child_count >= 0 ) + { + snprintf( cgi_childs, sizeof( cgi_childs ), "PHP_FCGI_CHILDREN=%d", child_count ); + putenv( cgi_childs ); + } - if(fcgi_fd != FCGI_LISTENSOCK_FILENO) { - close(FCGI_LISTENSOCK_FILENO); - dup2(fcgi_fd, FCGI_LISTENSOCK_FILENO); - close(fcgi_fd); - } + if ( fcgi_fd != FCGI_LISTENSOCK_FILENO ) + { + close( FCGI_LISTENSOCK_FILENO ); + dup2( fcgi_fd, FCGI_LISTENSOCK_FILENO ); + close( fcgi_fd ); + } - /* loose control terminal */ - if (!nofork) { - setsid(); + /* loose control terminal */ + if ( !nofork ) + { + setsid(); - max_fd = open("/dev/null", O_RDWR); - if (-1 != max_fd) { - if (max_fd != STDOUT_FILENO) dup2(max_fd, STDOUT_FILENO); - if (max_fd != STDERR_FILENO) dup2(max_fd, STDERR_FILENO); - if (max_fd != STDOUT_FILENO && max_fd != STDERR_FILENO) close(max_fd); - } else { - fprintf(stderr, "spawn-fcgi: couldn't open and redirect stdout/stderr to '/dev/null': %s\n", strerror(errno)); - } - } + max_fd = open( "/dev/null", O_RDWR ); + if ( -1 != max_fd ) + { + if ( max_fd != STDOUT_FILENO ) dup2( max_fd, STDOUT_FILENO ); + if ( max_fd != STDERR_FILENO ) dup2( max_fd, STDERR_FILENO ); + if ( max_fd != STDOUT_FILENO && max_fd != STDERR_FILENO ) close( max_fd ); + } + else + { + fprintf( stderr, "spawn-fcgi: couldn't open and redirect stdout/stderr to '/dev/null': %s\n", strerror( errno ) ); + } + } - /* we don't need the client socket */ - for (i = 3; i < max_fd; i++) { - if (i != FCGI_LISTENSOCK_FILENO) close(i); - } + /* we don't need the client socket */ + for ( i = 3; i < max_fd; i++ ) + { + if ( i != FCGI_LISTENSOCK_FILENO ) close( i ); + } - /* fork and replace shell */ - if (appArgv) { - execv(appArgv[0], appArgv); + /* fork and replace shell */ + if ( appArgv ) + { + execv( appArgv[0], appArgv ); - } else { - char *b = malloc((sizeof("exec ") - 1) + strlen(appPath) + 1); - strcpy(b, "exec "); - strcat(b, appPath); + } + else + { + char *b = malloc(( sizeof( "exec " ) - 1 ) + strlen( appPath ) + 1 ); + strcpy( b, "exec " ); + strcat( b, appPath ); - /* exec the cgi */ - execl("/bin/sh", "sh", "-c", b, (char *)NULL); - } + /* exec the cgi */ + execl( "/bin/sh", "sh", "-c", b, ( char * )NULL ); + } - /* in nofork mode stderr is still open */ - fprintf(stderr, "spawn-fcgi: exec failed: %s\n", strerror(errno)); - exit(errno); + /* in nofork mode stderr is still open */ + fprintf( stderr, "spawn-fcgi: exec failed: %s\n", strerror( errno ) ); + exit( errno ); - break; - } - case -1: - /* error */ - fprintf(stderr, "spawn-fcgi: fork failed: %s\n", strerror(errno)); - break; - default: - /* father */ + break; + } + case -1: + /* error */ + fprintf( stderr, "spawn-fcgi: fork failed: %s\n", strerror( errno ) ); + break; + default: + /* father */ - /* wait */ - select(0, NULL, NULL, NULL, &tv); + /* wait */ + select( 0, NULL, NULL, NULL, &tv ); - switch (waitpid(child, &status, WNOHANG)) { - case 0: - fprintf(stdout, "spawn-fcgi: child spawned successfully: PID: %d\n", child); + switch ( waitpid( child, &status, WNOHANG ) ) + { + case 0: + fprintf( stdout, "spawn-fcgi: child spawned successfully: PID: %d\n", child ); - /* write pid file */ - if (pid_fd != -1) { - /* assume a 32bit pid_t */ - char pidbuf[12]; + /* write pid file */ + if ( pid_fd != -1 ) + { + /* assume a 32bit pid_t */ + char pidbuf[12]; - snprintf(pidbuf, sizeof(pidbuf) - 1, "%d", child); + snprintf( pidbuf, sizeof( pidbuf ) - 1, "%d", child ); - write(pid_fd, pidbuf, strlen(pidbuf)); - /* avoid eol for the last one */ - if (fork_count != 0) { - write(pid_fd, "\n", 1); - } - } + write( pid_fd, pidbuf, strlen( pidbuf ) ); + /* avoid eol for the last one */ + if ( fork_count != 0 ) + { + write( pid_fd, "\n", 1 ); + } + } - break; - case -1: - break; - default: - if (WIFEXITED(status)) { - fprintf(stderr, "spawn-fcgi: child exited with: %d\n", - WEXITSTATUS(status)); - rc = WEXITSTATUS(status); - } else if (WIFSIGNALED(status)) { - fprintf(stderr, "spawn-fcgi: child signaled: %d\n", - WTERMSIG(status)); - rc = 1; - } else { - fprintf(stderr, "spawn-fcgi: child died somehow: exit status = %d\n", - status); - rc = status; - } - } + break; + case -1: + break; + default: + if ( WIFEXITED( status ) ) + { + fprintf( stderr, "spawn-fcgi: child exited with: %d\n", + WEXITSTATUS( status ) ); + rc = WEXITSTATUS( status ); + } + else if ( WIFSIGNALED( status ) ) + { + fprintf( stderr, "spawn-fcgi: child signaled: %d\n", + WTERMSIG( status ) ); + rc = 1; + } + else + { + fprintf( stderr, "spawn-fcgi: child died somehow: exit status = %d\n", + status ); + rc = status; + } + } - break; - } - } - close(pid_fd); + break; + } + } + close( pid_fd ); - close(fcgi_fd); + close( fcgi_fd ); - return rc; + return rc; } -static int find_user_group(const char *user, const char *group, uid_t *uid, gid_t *gid, const char **username) { - uid_t my_uid = 0; - gid_t my_gid = 0; - struct passwd *my_pwd = NULL; - struct group *my_grp = NULL; - char *endptr = NULL; - *uid = 0; *gid = 0; - if (username) *username = NULL; +static int find_user_group( const char *user, const char *group, uid_t *uid, gid_t *gid, const char **username ) +{ + uid_t my_uid = 0; + gid_t my_gid = 0; + struct passwd *my_pwd = NULL; + struct group *my_grp = NULL; + char *endptr = NULL; + *uid = 0; *gid = 0; + if ( username ) *username = NULL; - if (user) { - my_uid = strtol(user, &endptr, 10); + if ( user ) + { + my_uid = strtol( user, &endptr, 10 ); - if (my_uid <= 0 || *endptr) { - if (NULL == (my_pwd = getpwnam(user))) { - fprintf(stderr, "spawn-fcgi: can't find user name %s\n", user); - return -1; - } - my_uid = my_pwd->pw_uid; + if ( my_uid <= 0 || *endptr ) + { + if ( NULL == ( my_pwd = getpwnam( user ) ) ) + { + fprintf( stderr, "spawn-fcgi: can't find user name %s\n", user ); + return -1; + } + my_uid = my_pwd->pw_uid; - if (my_uid == 0) { - fprintf(stderr, "spawn-fcgi: I will not set uid to 0\n"); - return -1; - } + if ( my_uid == 0 ) + { + fprintf( stderr, "spawn-fcgi: I will not set uid to 0\n" ); + return -1; + } - if (username) *username = user; - } else { - my_pwd = getpwuid(my_uid); - if (username && my_pwd) *username = my_pwd->pw_name; - } - } + if ( username ) *username = user; + } + else + { + my_pwd = getpwuid( my_uid ); + if ( username && my_pwd ) *username = my_pwd->pw_name; + } + } - if (group) { - my_gid = strtol(group, &endptr, 10); + if ( group ) + { + my_gid = strtol( group, &endptr, 10 ); - if (my_gid <= 0 || *endptr) { - if (NULL == (my_grp = getgrnam(group))) { - fprintf(stderr, "spawn-fcgi: can't find group name %s\n", group); - return -1; - } - my_gid = my_grp->gr_gid; + if ( my_gid <= 0 || *endptr ) + { + if ( NULL == ( my_grp = getgrnam( group ) ) ) + { + fprintf( stderr, "spawn-fcgi: can't find group name %s\n", group ); + return -1; + } + my_gid = my_grp->gr_gid; - if (my_gid == 0) { - fprintf(stderr, "spawn-fcgi: I will not set gid to 0\n"); - return -1; - } - } - } else if (my_pwd) { - my_gid = my_pwd->pw_gid; + if ( my_gid == 0 ) + { + fprintf( stderr, "spawn-fcgi: I will not set gid to 0\n" ); + return -1; + } + } + } + else if ( my_pwd ) + { + my_gid = my_pwd->pw_gid; - if (my_gid == 0) { - fprintf(stderr, "spawn-fcgi: I will not set gid to 0\n"); - return -1; - } - } + if ( my_gid == 0 ) + { + fprintf( stderr, "spawn-fcgi: I will not set gid to 0\n" ); + return -1; + } + } - *uid = my_uid; - *gid = my_gid; - return 0; + *uid = my_uid; + *gid = my_gid; + return 0; } -static void show_version () { - write(1, CONST_STR_LEN( - PACKAGE_DESC \ - "Build-Date: " __DATE__ " " __TIME__ "\n" - )); +static void show_version() +{ + write( 1, CONST_STR_LEN( + PACKAGE_DESC \ + "Build-Date: " __DATE__ " " __TIME__ "\n" + ) ); } -static void show_help () { - write(1, CONST_STR_LEN( - "Usage: spawn-fcgi [options] [-- [fcgi app arguments]]\n" \ - "\n" \ - PACKAGE_DESC \ - "\n" \ - "Options:\n" \ - " -f filename of the fcgi-application (deprecated; ignored if\n" \ - " is given; needs /bin/sh)\n" \ - " -d chdir to directory before spawning\n" \ - " -a
bind to IPv4/IPv6 address (defaults to 0.0.0.0)\n" \ - " -p bind to TCP-port\n" \ - " -s bind to Unix domain socket\n" \ - " -M change Unix domain socket mode\n" \ - " -C (PHP only) numbers of childs to spawn (default: not setting\n" \ - " the PHP_FCGI_CHILDREN environment variable - PHP defaults to 0)\n" \ - " -F number of children to fork (default 1)\n" \ - " -b backlog to allow on the socket (default 1024)\n" \ - " -P name of PID-file for spawned process (ignored in no-fork mode)\n" \ - " -n no fork (for daemontools)\n" \ - " -v show version\n" \ - " -?, -h show this help\n" \ - "(root only)\n" \ - " -c chroot to directory\n" \ - " -S create socket before chroot() (default is to create the socket\n" \ - " in the chroot)\n" \ - " -u change to user-id\n" \ - " -g change to group-id (default: primary group of user if -u\n" \ - " is given)\n" \ - " -U change Unix domain socket owner to user-id\n" \ - " -G change Unix domain socket group to group-id\n" \ - )); +static void show_help() +{ + write( 1, CONST_STR_LEN( + "Usage: spawn-fcgi [options] [-- [fcgi app arguments]]\n" \ + "\n" \ + PACKAGE_DESC \ + "\n" \ + "Options:\n" \ + " -f filename of the fcgi-application (deprecated; ignored if\n" \ + " is given; needs /bin/sh)\n" \ + " -d chdir to directory before spawning\n" \ + " -a
bind to IPv4/IPv6 address (defaults to 0.0.0.0)\n" \ + " -p bind to TCP-port\n" \ + " -s bind to Unix domain socket\n" \ + " -M change Unix domain socket mode\n" \ + " -C (PHP only) numbers of childs to spawn (default: not setting\n" \ + " the PHP_FCGI_CHILDREN environment variable - PHP defaults to 0)\n" \ + " -F number of children to fork (default 1)\n" \ + " -b backlog to allow on the socket (default 1024)\n" \ + " -P name of PID-file for spawned process (ignored in no-fork mode)\n" \ + " -n no fork (for daemontools)\n" \ + " -v show version\n" \ + " -?, -h show this help\n" \ + "(root only)\n" \ + " -c chroot to directory\n" \ + " -S create socket before chroot() (default is to create the socket\n" \ + " in the chroot)\n" \ + " -u change to user-id\n" \ + " -g change to group-id (default: primary group of user if -u\n" \ + " is given)\n" \ + " -U change Unix domain socket owner to user-id\n" \ + " -G change Unix domain socket group to group-id\n" \ + ) ); } -int main(int argc, char **argv) { - char *fcgi_app = NULL, *changeroot = NULL, *username = NULL, - *groupname = NULL, *unixsocket = NULL, *pid_file = NULL, - *sockusername = NULL, *sockgroupname = NULL, *fcgi_dir = NULL, - *addr = NULL; - char **fcgi_app_argv = { NULL }; - char *endptr = NULL; - unsigned short port = 0; - int sockmode = -1; - int child_count = -1; - int fork_count = 1; - int backlog = 1024; - int i_am_root, o; - int pid_fd = -1; - int nofork = 0; - int sockbeforechroot = 0; - struct sockaddr_un un; - int fcgi_fd = -1; +int main( int argc, char **argv ) +{ + char *fcgi_app = NULL, *changeroot = NULL, *username = NULL, + *groupname = NULL, *unixsocket = NULL, *pid_file = NULL, + *sockusername = NULL, *sockgroupname = NULL, *fcgi_dir = NULL, + *addr = NULL; + char **fcgi_app_argv = { NULL }; + char *endptr = NULL; + unsigned short port = 0; + int sockmode = -1; + int child_count = -1; + int fork_count = 1; + int backlog = 1024; + int i_am_root, o; + int pid_fd = -1; + int nofork = 0; + int sockbeforechroot = 0; + struct sockaddr_un un; + int fcgi_fd = -1; - if (argc < 2) { /* no arguments given */ - show_help(); - return -1; - } + if ( argc < 2 ) /* no arguments given */ + { + show_help(); + return -1; + } - i_am_root = (getuid() == 0); + i_am_root = ( getuid() == 0 ); - while (-1 != (o = getopt(argc, argv, "c:d:f:g:?hna:p:b:u:vC:F:s:P:U:G:M:S"))) { - switch(o) { - case 'f': fcgi_app = optarg; break; - case 'd': fcgi_dir = optarg; break; - case 'a': addr = optarg;/* ip addr */ break; - case 'p': port = strtol(optarg, &endptr, 10);/* port */ - if (*endptr) { - fprintf(stderr, "spawn-fcgi: invalid port: %u\n", (unsigned int) port); - return -1; - } - break; - case 'C': child_count = strtol(optarg, NULL, 10);/* */ break; - case 'F': fork_count = strtol(optarg, NULL, 10);/* */ break; - case 'b': backlog = strtol(optarg, NULL, 10);/* */ break; - case 's': unixsocket = optarg; /* unix-domain socket */ break; - case 'c': if (i_am_root) { changeroot = optarg; }/* chroot() */ break; - case 'u': if (i_am_root) { username = optarg; } /* set user */ break; - case 'g': if (i_am_root) { groupname = optarg; } /* set group */ break; - case 'U': if (i_am_root) { sockusername = optarg; } /* set socket user */ break; - case 'G': if (i_am_root) { sockgroupname = optarg; } /* set socket group */ break; - case 'S': if (i_am_root) { sockbeforechroot = 1; } /* open socket before chroot() */ break; - case 'M': sockmode = strtol(optarg, NULL, 8); /* set socket mode */ break; - case 'n': nofork = 1; break; - case 'P': pid_file = optarg; /* PID file */ break; - case 'v': show_version(); return 0; - case '?': - case 'h': show_help(); return 0; - default: - show_help(); - return -1; - } - } + while ( -1 != ( o = getopt( argc, argv, "c:d:f:g:?hna:p:b:u:vC:F:s:P:U:G:M:S" ) ) ) + { + switch ( o ) + { + case 'f': fcgi_app = optarg; break; + case 'd': fcgi_dir = optarg; break; + case 'a': addr = optarg;/* ip addr */ break; + case 'p': port = strtol( optarg, &endptr, 10 );/* port */ + if ( *endptr ) + { + fprintf( stderr, "spawn-fcgi: invalid port: %u\n", ( unsigned int ) port ); + return -1; + } + break; + case 'C': child_count = strtol( optarg, NULL, 10 );/* */ break; + case 'F': fork_count = strtol( optarg, NULL, 10 );/* */ break; + case 'b': backlog = strtol( optarg, NULL, 10 );/* */ break; + case 's': unixsocket = optarg; /* unix-domain socket */ break; + case 'c': if ( i_am_root ) { changeroot = optarg; }/* chroot() */ + break; + case 'u': if ( i_am_root ) { username = optarg; } /* set user */ + break; + case 'g': if ( i_am_root ) { groupname = optarg; } /* set group */ + break; + case 'U': if ( i_am_root ) { sockusername = optarg; } /* set socket user */ + break; + case 'G': if ( i_am_root ) { sockgroupname = optarg; } /* set socket group */ + break; + case 'S': if ( i_am_root ) { sockbeforechroot = 1; } /* open socket before chroot() */ + break; + case 'M': sockmode = strtol( optarg, NULL, 8 ); /* set socket mode */ break; + case 'n': nofork = 1; break; + case 'P': pid_file = optarg; /* PID file */ break; + case 'v': show_version(); return 0; + case '?': + case 'h': show_help(); return 0; + default: + show_help(); + return -1; + } + } - if (optind < argc) { - fcgi_app_argv = &argv[optind]; - } + if ( optind < argc ) + { + fcgi_app_argv = &argv[optind]; + } - if (NULL == fcgi_app && NULL == fcgi_app_argv) { - fprintf(stderr, "spawn-fcgi: no FastCGI application given\n"); - return -1; - } + if ( NULL == fcgi_app && NULL == fcgi_app_argv ) + { + fprintf( stderr, "spawn-fcgi: no FastCGI application given\n" ); + return -1; + } - if (0 == port && NULL == unixsocket) { - fprintf(stderr, "spawn-fcgi: no socket given (use either -p or -s)\n"); - return -1; - } else if (0 != port && NULL != unixsocket) { - fprintf(stderr, "spawn-fcgi: either a Unix domain socket or a TCP-port, but not both\n"); - return -1; - } + if ( 0 == port && NULL == unixsocket ) + { + fprintf( stderr, "spawn-fcgi: no socket given (use either -p or -s)\n" ); + return -1; + } + else if ( 0 != port && NULL != unixsocket ) + { + fprintf( stderr, "spawn-fcgi: either a Unix domain socket or a TCP-port, but not both\n" ); + return -1; + } - if (unixsocket && strlen(unixsocket) > sizeof(un.sun_path) - 1) { - fprintf(stderr, "spawn-fcgi: path of the Unix domain socket is too long\n"); - return -1; - } + if ( unixsocket && strlen( unixsocket ) > sizeof( un.sun_path ) - 1 ) + { + fprintf( stderr, "spawn-fcgi: path of the Unix domain socket is too long\n" ); + return -1; + } - /* SUID handling */ - if (!i_am_root && issetugid()) { - fprintf(stderr, "spawn-fcgi: Are you nuts? Don't apply a SUID bit to this binary\n"); - return -1; - } + /* SUID handling */ + if ( !i_am_root && issetugid() ) + { + fprintf( stderr, "spawn-fcgi: Are you nuts? Don't apply a SUID bit to this binary\n" ); + return -1; + } - if (nofork) pid_file = NULL; /* ignore pid file in no-fork mode */ + if ( nofork ) pid_file = NULL; /* ignore pid file in no-fork mode */ - if (pid_file && - (-1 == (pid_fd = open(pid_file, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)))) { - struct stat st; - if (errno != EEXIST) { - fprintf(stderr, "spawn-fcgi: opening PID-file '%s' failed: %s\n", - pid_file, strerror(errno)); - return -1; - } + if ( pid_file && + ( -1 == ( pid_fd = open( pid_file, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) ) ) ) + { + struct stat st; + if ( errno != EEXIST ) + { + fprintf( stderr, "spawn-fcgi: opening PID-file '%s' failed: %s\n", + pid_file, strerror( errno ) ); + return -1; + } - /* ok, file exists */ + /* ok, file exists */ - if (0 != stat(pid_file, &st)) { - fprintf(stderr, "spawn-fcgi: stating PID-file '%s' failed: %s\n", - pid_file, strerror(errno)); - return -1; - } + if ( 0 != stat( pid_file, &st ) ) + { + fprintf( stderr, "spawn-fcgi: stating PID-file '%s' failed: %s\n", + pid_file, strerror( errno ) ); + return -1; + } - /* is it a regular file ? */ + /* is it a regular file ? */ - if (!S_ISREG(st.st_mode)) { - fprintf(stderr, "spawn-fcgi: PID-file exists and isn't regular file: '%s'\n", - pid_file); - return -1; - } + if ( !S_ISREG( st.st_mode ) ) + { + fprintf( stderr, "spawn-fcgi: PID-file exists and isn't regular file: '%s'\n", + pid_file ); + return -1; + } - if (-1 == (pid_fd = open(pid_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) { - fprintf(stderr, "spawn-fcgi: opening PID-file '%s' failed: %s\n", - pid_file, strerror(errno)); - return -1; - } - } + if ( -1 == ( pid_fd = open( pid_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) ) ) + { + fprintf( stderr, "spawn-fcgi: opening PID-file '%s' failed: %s\n", + pid_file, strerror( errno ) ); + return -1; + } + } - if (i_am_root) { - uid_t uid, sockuid; - gid_t gid, sockgid; - const char* real_username; + if ( i_am_root ) + { + uid_t uid, sockuid; + gid_t gid, sockgid; + const char* real_username; - if (-1 == find_user_group(username, groupname, &uid, &gid, &real_username)) - return -1; + if ( -1 == find_user_group( username, groupname, &uid, &gid, &real_username ) ) + return -1; - if (-1 == find_user_group(sockusername, sockgroupname, &sockuid, &sockgid, NULL)) - return -1; + if ( -1 == find_user_group( sockusername, sockgroupname, &sockuid, &sockgid, NULL ) ) + return -1; - if (uid != 0 && gid == 0) { - fprintf(stderr, "spawn-fcgi: WARNING: couldn't find the user for uid %i and no group was specified, so only the user privileges will be dropped\n", (int) uid); - } + if ( uid != 0 && gid == 0 ) + { + fprintf( stderr, "spawn-fcgi: WARNING: couldn't find the user for uid %i and no group was specified, so only the user privileges will be dropped\n", ( int ) uid ); + } - if (0 == sockuid) sockuid = uid; - if (0 == sockgid) sockgid = gid; + if ( 0 == sockuid ) sockuid = uid; + if ( 0 == sockgid ) sockgid = gid; - if (sockbeforechroot && -1 == (fcgi_fd = bind_socket(addr, port, unixsocket, sockuid, sockgid, sockmode, backlog))) - return -1; + if ( sockbeforechroot && -1 == ( fcgi_fd = bind_socket( addr, port, unixsocket, sockuid, sockgid, sockmode, backlog ) ) ) + return -1; - /* Change group before chroot, when we have access - * to /etc/group - */ - if (gid != 0) { - setgid(gid); - setgroups(0, NULL); - if (real_username) { - initgroups(real_username, gid); - } - } + /* Change group before chroot, when we have access + * to /etc/group + */ + if ( gid != 0 ) + { + setgid( gid ); + setgroups( 0, NULL ); + if ( real_username ) + { + initgroups( real_username, gid ); + } + } - if (changeroot) { - if (-1 == chroot(changeroot)) { - fprintf(stderr, "spawn-fcgi: chroot('%s') failed: %s\n", changeroot, strerror(errno)); - return -1; - } - if (-1 == chdir("/")) { - fprintf(stderr, "spawn-fcgi: chdir('/') failed: %s\n", strerror(errno)); - return -1; - } - } + if ( changeroot ) + { + if ( -1 == chroot( changeroot ) ) + { + fprintf( stderr, "spawn-fcgi: chroot('%s') failed: %s\n", changeroot, strerror( errno ) ); + return -1; + } + if ( -1 == chdir( "/" ) ) + { + fprintf( stderr, "spawn-fcgi: chdir('/') failed: %s\n", strerror( errno ) ); + return -1; + } + } - if (!sockbeforechroot && -1 == (fcgi_fd = bind_socket(addr, port, unixsocket, sockuid, sockgid, sockmode, backlog))) - return -1; + if ( !sockbeforechroot && -1 == ( fcgi_fd = bind_socket( addr, port, unixsocket, sockuid, sockgid, sockmode, backlog ) ) ) + return -1; - /* drop root privs */ - if (uid != 0) { - setuid(uid); - } - } else { - if (-1 == (fcgi_fd = bind_socket(addr, port, unixsocket, 0, 0, sockmode, backlog))) - return -1; - } + /* drop root privs */ + if ( uid != 0 ) + { + setuid( uid ); + } + } + else + { + if ( -1 == ( fcgi_fd = bind_socket( addr, port, unixsocket, 0, 0, sockmode, backlog ) ) ) + return -1; + } - if (fcgi_dir && -1 == chdir(fcgi_dir)) { - fprintf(stderr, "spawn-fcgi: chdir('%s') failed: %s\n", fcgi_dir, strerror(errno)); - return -1; - } + if ( fcgi_dir && -1 == chdir( fcgi_dir ) ) + { + fprintf( stderr, "spawn-fcgi: chdir('%s') failed: %s\n", fcgi_dir, strerror( errno ) ); + return -1; + } - return fcgi_spawn_connection(fcgi_app, fcgi_app_argv, fcgi_fd, fork_count, child_count, pid_fd, nofork); + return fcgi_spawn_connection( fcgi_app, fcgi_app_argv, fcgi_fd, fork_count, child_count, pid_fd, nofork ); }