int TerminateUsersAndUnmountMountPoint()

in host/common/unix/portablehelpersmajor.cpp [2480:2612]


int TerminateUsersAndUnmountMountPoint(const char * mntpt,std::string& error)
{
	int retval = 0; //Unmount successful.
	do
	{
		// Bug #4044
		// Using InmCommand to execute the command
		// and capture the error output and exitcode
		DebugPrintf(SV_LOG_INFO, "Trying to find processes accessing %s ...\n", mntpt);

		std::string command = SV_FUSER_FOR_MNTPNT;

		InmCommand fusermnt_cmd(command + mntpt);

		std::string pathvar = "PATH=";
		pathvar += GetCommandPath(command);
		fusermnt_cmd.PutEnv(pathvar);

		InmCommand::statusType status = fusermnt_cmd.Run();

		if(status != InmCommand::completed) {
			DebugPrintf(SV_LOG_ERROR, 
				"Function %s @LINE %d in FILE %s :failed to find processes accessing %s\n",
				FUNCTION_NAME, LINE_NO, FILE_NAME,mntpt);
			error = "Failed to execute the command ";
			error += command;
			error += mntpt;
			if(!fusermnt_cmd.StdErr().empty())
			{
				error += fusermnt_cmd.StdErr();
			}
			retval = -1;
			break;
		}

		if(!fusermnt_cmd.ExitCode())
		{
			DebugPrintf(SV_LOG_INFO, "Trying to shutdown processes accessing %s ...\n", mntpt);
			command = SV_FUSER_MNTPNT_KILL;
			InmCommand fuserkill(command + mntpt);

			pathvar = "PATH=";
			pathvar += GetCommandPath(command);
			fuserkill.PutEnv(pathvar);

			status = fuserkill.Run();


			if (status != InmCommand::completed) { 
				DebugPrintf(SV_LOG_ERROR, 
					"Function %s @LINE %d in FILE %s :failed to shutdown processes accessing %s\n",
					FUNCTION_NAME, LINE_NO, FILE_NAME,mntpt);
				error = "Failed to shutdown processes accessing ";
				error += mntpt;
				if(!fuserkill.StdErr().empty())
				{
					error += fuserkill.StdErr();
				}
				retval = -1;
				break;
			}

			if(fuserkill.ExitCode())
			{
				DebugPrintf(SV_LOG_ERROR, 
					"Function %s @LINE %d in FILE %s :failed to shutdown processes accessing %s\n",
					FUNCTION_NAME, LINE_NO, FILE_NAME,mntpt);
				error = "Failed to shutdown processes accessing ";
				error += mntpt;
				std::ostringstream msg;
				msg << "Exit Code = " << fuserkill.ExitCode() << std::endl;
				if(!fuserkill.StdOut().empty())
				{
					msg << "Output = " << fuserkill.StdOut() << std::endl;
				}
				if(!fuserkill.StdErr().empty())
				{
					msg << "Error = " << fuserkill.StdErr() << std::endl;
				}
				error += msg.str();
				retval = 1;
				break;
			}
		}

		DebugPrintf(SV_LOG_INFO, "Performing unmount operation on %s ...\n", mntpt);
		command = SV_UMOUNT;
		InmCommand unmount(command + mntpt);
		pathvar = "PATH=";
		pathvar += GetCommandPath(command);
		unmount.PutEnv(pathvar);
		status = unmount.Run();

		if (status != InmCommand::completed) {
			DebugPrintf(SV_LOG_ERROR, 
				"Function %s @LINE %d in FILE %s :failed to unmount %s\n",
				FUNCTION_NAME, LINE_NO, FILE_NAME,mntpt);
			error = "Failed to unmount ";
			error += mntpt;
			if(!unmount.StdErr().empty())
			{
				error += unmount.StdErr();
			}
			retval = -1;
			break;
		}

		if(unmount.ExitCode())
		{
			error = "Failed to unmount ";
			error += mntpt;
			std::ostringstream msg;
			msg << " Exit Code = " << unmount.ExitCode()<< std::endl;
			if(!unmount.StdOut().empty())
			{
				msg << "Output = " << unmount.StdOut() << std::endl;
			}
			if(!unmount.StdErr().empty())
			{
				msg << "Error = " << unmount.StdErr() << std::endl;
			}
			error += msg.str();

			DebugPrintf(SV_LOG_INFO, 
				"Function %s @LINE %d in FILE %s :failed to unmount %s\n : %s\n",
				FUNCTION_NAME, LINE_NO, FILE_NAME,mntpt, error.c_str());
			retval = 1;
			break;
		}
	}while(0);

	return retval;
}