| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-03-16 04:27:16 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2020-09-12 14:02:32 +08:00
										 |  |  | 	"errors" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2018-11-13 07:07:16 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/logger" | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | func handleSignals() { | 
					
						
							|  |  |  | 	// Custom exit function
 | 
					
						
							| 
									
										
										
										
											2019-08-13 12:25:34 +08:00
										 |  |  | 	exit := func(success bool) { | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | 		// If global profiler is set stop before we exit.
 | 
					
						
							| 
									
										
										
										
											2020-01-11 09:19:58 +08:00
										 |  |  | 		globalProfilerMu.Lock() | 
					
						
							|  |  |  | 		defer globalProfilerMu.Unlock() | 
					
						
							| 
									
										
										
										
											2020-01-22 07:49:25 +08:00
										 |  |  | 		for _, p := range globalProfiler { | 
					
						
							|  |  |  | 			p.Stop() | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 12:25:34 +08:00
										 |  |  | 		if success { | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | 			os.Exit(0) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | 		os.Exit(1) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | 	stopProcess := func() bool { | 
					
						
							|  |  |  | 		var err, oerr error | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-09 00:10:55 +08:00
										 |  |  | 		// send signal to various go-routines that they need to quit.
 | 
					
						
							|  |  |  | 		cancelGlobalContext() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-24 21:42:36 +08:00
										 |  |  | 		if globalEventNotifier != nil { | 
					
						
							|  |  |  | 			globalEventNotifier.RemoveAllRemoteTargets() | 
					
						
							| 
									
										
										
										
											2018-03-16 04:03:41 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 07:54:26 +08:00
										 |  |  | 		if httpServer := newHTTPServerFn(); httpServer != nil { | 
					
						
							|  |  |  | 			err = httpServer.Shutdown() | 
					
						
							| 
									
										
										
										
											2020-09-12 14:02:32 +08:00
										 |  |  | 			if !errors.Is(err, http.ErrServerClosed) { | 
					
						
							|  |  |  | 				logger.LogIf(context.Background(), err) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-12-03 07:54:26 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 00:59:52 +08:00
										 |  |  | 		if objAPI := newObjectLayerFn(); objAPI != nil { | 
					
						
							| 
									
										
										
										
											2018-03-16 04:27:16 +08:00
										 |  |  | 			oerr = objAPI.Shutdown(context.Background()) | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 			logger.LogIf(context.Background(), oerr) | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 09:19:58 +08:00
										 |  |  | 		if srv := newConsoleServerFn(); srv != nil { | 
					
						
							|  |  |  | 			logger.LogIf(context.Background(), srv.Shutdown()) | 
					
						
							| 
									
										
										
										
											2021-09-01 09:52:48 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | 		return (err == nil && oerr == nil) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		select { | 
					
						
							| 
									
										
										
										
											2022-10-22 05:42:28 +08:00
										 |  |  | 		case err := <-globalHTTPServerErrorCh: | 
					
						
							|  |  |  | 			logger.LogIf(context.Background(), err) | 
					
						
							| 
									
										
										
										
											2020-09-09 00:10:55 +08:00
										 |  |  | 			exit(stopProcess()) | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | 		case osSignal := <-globalOSSignalCh: | 
					
						
							| 
									
										
										
										
											2022-10-25 08:44:15 +08:00
										 |  |  | 			globalReplicationPool.SaveState(context.Background()) | 
					
						
							| 
									
										
										
										
											2018-11-13 07:07:16 +08:00
										 |  |  | 			logger.Info("Exiting on signal: %s", strings.ToUpper(osSignal.String())) | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | 			exit(stopProcess()) | 
					
						
							|  |  |  | 		case signal := <-globalServiceSignalCh: | 
					
						
							| 
									
										
										
										
											2022-10-25 08:44:15 +08:00
										 |  |  | 			globalReplicationPool.SaveState(context.Background()) | 
					
						
							| 
									
										
										
										
											2019-08-29 06:04:43 +08:00
										 |  |  | 			switch signal { | 
					
						
							|  |  |  | 			case serviceRestart: | 
					
						
							| 
									
										
										
										
											2018-04-11 00:37:14 +08:00
										 |  |  | 				logger.Info("Restarting on service signal") | 
					
						
							| 
									
										
										
										
											2018-06-01 03:30:15 +08:00
										 |  |  | 				stop := stopProcess() | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | 				rerr := restartProcess() | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 				logger.LogIf(context.Background(), rerr) | 
					
						
							| 
									
										
										
										
											2018-06-01 03:30:15 +08:00
										 |  |  | 				exit(stop && rerr == nil) | 
					
						
							| 
									
										
										
										
											2019-08-29 06:04:43 +08:00
										 |  |  | 			case serviceStop: | 
					
						
							| 
									
										
										
										
											2018-04-11 00:37:14 +08:00
										 |  |  | 				logger.Info("Stopping on service signal") | 
					
						
							| 
									
										
										
										
											2017-07-13 07:33:21 +08:00
										 |  |  | 				exit(stopProcess()) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | } |