Beispiel #1
0
        public static void StartChildren(FileInfo[] configFiles, ConfigurationManager configurationManager)
        {
            if (configFiles == null)
            {
                throw new ArgumentNullException("configFiles");
            }
            if (configurationManager == null)
            {
                throw new ArgumentNullException("configurationManager");
            }
            foreach (FileInfo fileInfo in configFiles)
            {
                if (fileInfo == null)
                {
                    continue;
                }
                Logger.Write(LogLevel.Debug, "Loading {0}", fileInfo.Name);
                var    childConfigurationManager = new ChildConfigurationManager();
                string configFile = fileInfo.FullName;
                if (!childConfigurationManager.TryLoadXmlConfig(configFile))
                {
                    continue;
                }
                string user           = childConfigurationManager.User;
                string fastCgiCommand = configurationManager.FastCgiCommand;

                Func <bool, Process> spawner;
                if (Platform.IsUnix)
                {
                    if (String.IsNullOrEmpty(user))
                    {
                        Logger.Write(LogLevel.Warning, "Configuration file {0} didn't specify username, defaulting to file owner", fileInfo.Name);
                        user = UnixFileSystemInfo.GetFileSystemEntry(configFile).OwnerUser.UserName;
                    }

                    spawner = onDemand => Spawner.RunAs(user, Spawner.SpawnChild, configFile, fastCgiCommand, onDemand);
                }
                else
                {
                    Logger.Write(LogLevel.Warning, "Configuration file {0} didn't specify username, defaulting to the current one", fileInfo.Name);
                    spawner = onDemand => Spawner.SpawnChild(configFile, fastCgiCommand, onDemand);
                }
                var child = new ChildInfo {
                    Spawner = spawner, ConfigurationManager = childConfigurationManager, Name = configFile, OnDemand = childConfigurationManager.InstanceType == InstanceType.Dynamic
                };
                children.Add(child);
                if (child.OnDemand)
                {
                    Socket socket;
                    if (FastCgi.Server.TryCreateSocket(childConfigurationManager, out socket))
                    {
                        var server = new GenericServer <Connection> (socket, child);
                        server.Start(configurationManager.Stoppable, (int)childConfigurationManager.Backlog);
                    }
                }
                else
                {
                    if (child.TrySpawn())
                    {
                        Logger.Write(LogLevel.Notice, "Started fastcgi daemon [static] with pid {0} and config file {1}", child.Process.Id, Path.GetFileName(configFile));
                    }
                    else
                    {
                        Logger.Write(LogLevel.Error, "Couldn't start child with config file {0}", configFile);
                    }
                }
            }
        }