Esempio n. 1
0
int main(int argc, char *argv[])
{
    SERVICE_TABLE_ENTRY dispatchTable[] = {
        {SVCNAME, (LPSERVICE_MAIN_FUNCTION)service_main},
        {NULL,    NULL}
    };

    if ((argc > 1) && ((argv[1][0] == '-') && (argv[1][1] == '-'))) {
        if (!_stricmp("install-svc", argv[1]+2))
            return install_service();
        else if (!_stricmp("remove-svc", argv[1]+2))
            return remove_service();
        else if (_stricmp("run-svc", argv[1]+2) && !is_nt_service())
            return run_notservice(argc, argv);
    } else if (!is_nt_service()) {
        return run_notservice(argc, argv);
    }
    printf("mix --install-svc   install the service\n");
    printf("mix --remove-svc    remove the service\n");
    printf("mix --run-svc       run as a service\n");
    printf("mix -h          view a summary of the command line options.\n");

    printf("\nStartServiceCtrlDispatcher being called.\n" );
    printf("This may take several seconds.  Please wait.\n" );
    if (!StartServiceCtrlDispatcher(dispatchTable)) {
        printf("Service not started: StartServiceCtrlDispatcher failed.\n" );
        event_log(1000, "Service not started: StartServiceCtrlDispatcher failed");
    }
    return 0;
} /* main */
Esempio n. 2
0
/*
  kill older tincd for this net
*/
bool kill_other(int signal) {
#ifndef HAVE_MINGW
	pid_t pid;

	pid = read_pid(pidfilename);

	if(!pid) {
		if(netname)
			fprintf(stderr, "No other tincd is running for net `%s'.\n",
					netname);
		else
			fprintf(stderr, "No other tincd is running.\n");
		return false;
	}

	errno = 0;					/* No error, sometimes errno is only changed on error */

	/* ESRCH is returned when no process with that pid is found */
	if(kill(pid, signal) && errno == ESRCH) {
		if(netname)
			fprintf(stderr, "The tincd for net `%s' is no longer running. ",
					netname);
		else
			fprintf(stderr, "The tincd is no longer running. ");

		fprintf(stderr, "Removing stale lock file.\n");
		remove_pid(pidfilename);
	}

	return true;
#else
	return remove_service();
#endif
}
Esempio n. 3
0
/* About to remove the service */
int pre_remove_service(int argc, char **argv) {
  /* Show dialogue box if we didn't pass service name and "confirm" */
  if (argc < 2) return nssm_gui(IDD_REMOVE, argv[0]);
  if (str_equiv(argv[1], "confirm")) return remove_service(argv[0]);
  print_message(stderr, NSSM_MESSAGE_PRE_REMOVE_SERVICE);
  return 100;
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
    if (argc == 2) {

        if (strcmp(argv[1], "install-service") == 0) {

            // Installs and starts the service

            install_service();
            return 0;
        }
        else if (strcmp(argv[1], "remove-service") == 0) {
        
            // Stops and removes the service
            
            remove_service();
            return 0;
        }
        else if (strcmp(argv[1], "service") == 0) {
        
            // Starts the Meterpreter as a service

            start_service();
            return 0;
        }
    }

    // Starts the Meterpreter as a normal application

    start_meterpreter();

    return 0;
}
Esempio n. 5
0
static void service_browser_callback(
        AvahiServiceBrowser *b,
        AvahiIfIndex interface,
        AvahiProtocol protocol,
        AvahiBrowserEvent event,
        const char *name,
        const char *type,
        const char *domain,
        AvahiLookupResultFlags flags,
        void *userdata) {

    Config *c = userdata;

    assert(b);
    assert(c);

    switch (event) {
        case AVAHI_BROWSER_NEW: {
            if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
                break;

            if (find_service(interface, protocol, name, type, domain))
                return;

            add_service(c, interface, protocol, name, type, domain);

            //print_service_line(c, '+', interface, protocol, name, type, domain, 1);
            break;

        }

        case AVAHI_BROWSER_REMOVE: {
            ServiceInfo *info;

            if (!(info = find_service(interface, protocol, name, type, domain)))
                return;

            remove_service(c, info);

            //print_service_line(c, '-', interface, protocol, name, type, domain, 1);
            break;
        }

        case AVAHI_BROWSER_FAILURE:
            fprintf(stderr, ("service_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
            avahi_simple_poll_quit(simple_poll);
            break;

        case AVAHI_BROWSER_CACHE_EXHAUSTED:
            n_cache_exhausted --;
            check_terminate(c);
            break;

        case AVAHI_BROWSER_ALL_FOR_NOW:
            n_all_for_now --;
            check_terminate(c);
            break;
    }
}
Esempio n. 6
0
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
    Config *config = userdata;

    /* This function might be called when avahi_client_new() has not
     * returned yet.*/
    client = c;

    switch (state) {
        case AVAHI_CLIENT_FAILURE:

            if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
                int error;

                /* We have been disconnected, so let reconnect */

                fprintf(stderr, ("Disconnected, reconnecting ...\n"));

                avahi_client_free(client);
                client = NULL;

                avahi_string_list_free(browsed_types);
                browsed_types = NULL;

                while (services)
                    remove_service(config, services);

                browsing = 0;

                if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
                    fprintf(stderr, ("Failed to create client object: %s\n"), avahi_strerror(error));
                    avahi_simple_poll_quit(simple_poll);
                }

            } else {
                fprintf(stderr, ("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
                avahi_simple_poll_quit(simple_poll);
            }

            break;

        case AVAHI_CLIENT_S_REGISTERING:
        case AVAHI_CLIENT_S_RUNNING:
        case AVAHI_CLIENT_S_COLLISION:

            if (!browsing)
            if (start(config) < 0)
                avahi_simple_poll_quit(simple_poll);

            break;

        case AVAHI_CLIENT_CONNECTING:

            if (config->verbose && !config->parsable)
                fprintf(stderr, ("Waiting for daemon ...\n"));

            break;
    }
}
Esempio n. 7
0
int
main(int argc, char **argv)
{
    static SERVICE_TABLE_ENTRY dispatch_table[] = {
        {"tdifw", service_main},
        {NULL, NULL}
    };

	_LEAK_CHECK;

	if (argc >= 2) {
		const char *param = argv[1];
		
		if (strcmp(param, "install") == 0) {
			if (argc < 3) {
				fprintf(stderr, "Use: tdifw install <config>\n");
				return -1;
			}
		
			install_service(argv[2]);
		
		} else if (strcmp(param, "remove") == 0) {
			remove_service();
		} else if (strcmp(param, "debug") == 0) {

			if (argc < 3) {
				fprintf(stderr, "Use: tdifw debug <config>\n");
				return -1;
			}

			if (start(argv[2])) {
				printf("press enter to exit...\n");
				getchar();
				printf("exiting...\n");

				stop();
			}

		} else if (strcmp(param, "listen") == 0) {		// tdifw specific
			enum_listen();
		} else if (strcmp(param, "conn") == 0) {		// tdifw specific
			enum_connect();
		} else {
			fprintf(stderr, "Use: tdifw install|remove|debug|listen|conn\n");
		}
	} else {

		g_console = FALSE;

		// run as service
		if (!StartServiceCtrlDispatcher(dispatch_table))
			winerr("main: StartServiceCtrlDispatcher");

	}

	return 0;
}
Esempio n. 8
0
 int32_t ServiceManager::remove_service(SharedPointer<IProcessor> processor) {
     //NOTE: 本函数不用加锁
     std::string name;
     if (processor.is_valid()) {
         name = processor->get_name();
     }
     else {
         return -1;
     }
     return remove_service(name);
 }
Esempio n. 9
0
static int uninstall(void)
{
    SC_HANDLE   hSCManager;

    hSCManager = OpenSCManager(
                        NULL,                   // machine (NULL == local)
                        NULL,                   // database (NULL == default)
                        SC_MANAGER_ALL_ACCESS   // access required
                        );
    if(hSCManager==NULL) {
		fprintf(stderr,"!ERROR %d opening SC manager\n",GetLastError());
		return(-1);
	}

	remove_service(hSCManager,NULL,NAME,TITLE);

	CloseServiceHandle(hSCManager);

	return(0);
}
UINT wrap(MSIHANDLE hInstall, char *name, int check_only) {
	HRESULT hr = S_OK;
	UINT er = ERROR_SUCCESS;

	hr = WcaInitialize(hInstall, name);
	ExitOnFailure(hr, "Failed to initialize");

	WcaLog(LOGMSG_STANDARD, "Initialized.");

    TCHAR INSTALLDIR[1024];
    DWORD INSTALLDIR_size = sizeof(INSTALLDIR);
	if(MsiGetPropertyW(hInstall, TEXT("CustomActionData"), INSTALLDIR, &INSTALLDIR_size) == ERROR_SUCCESS) {
		int rc = remove_service(INSTALLDIR, check_only);
		if(rc < 0) {
			er = ERROR_CANCELLED;
		}
	} else {
		er = ERROR_CANT_ACCESS_FILE;
	}

LExit:
	return WcaFinalize(er);
}
int remove_service(TCHAR *installdir, int check_only) {
	HKEY hKey;
	int done = 0;

	if(wcslen(installdir) < 3) {
		WcaLog(LOGMSG_STANDARD, "INSTALLDIR is suspiciously short, better not do anything.");
		return 0;
	}

	if(check_only == 0) {
		WcaLog(LOGMSG_STANDARD, "Determining number of matching services...");
		int servicecount = remove_service(installdir, 1);
		if(servicecount <= 0) {
			WcaLog(LOGMSG_STANDARD, "No services found, not removing anything.");
			return 0;
		} else if(servicecount == 1) {
			TCHAR buf[256];
			swprintf_s(buf, sizeof(buf), TEXT("There is a service called '%ls' set up to run from this installation. Do you wish me to stop and remove that service?"), last_service_name);
			int rc = MessageBox(NULL, buf, TEXT("Removing MySQL Server"), MB_ICONQUESTION|MB_YESNOCANCEL|MB_SYSTEMMODAL);
			if(rc == IDCANCEL) return -1;
			if(rc != IDYES) return 0;
		} else if(servicecount > 0) {
			TCHAR buf[256];
			swprintf_s(buf, sizeof(buf), TEXT("There appear to be %d services set up to run from this installation. Do you wish me to stop and remove those services?"), servicecount);
			int rc = MessageBox(NULL, buf, TEXT("Removing MySQL Server"), MB_ICONQUESTION|MB_YESNOCANCEL|MB_SYSTEMMODAL);
			if(rc == IDCANCEL) return -1;
			if(rc != IDYES) return 0;
		}
	}

	if(check_only == -1) check_only = 0;

	WcaLog(LOGMSG_STANDARD, "Looking for service...");
	WcaLog(LOGMSG_STANDARD, "INSTALLDIR = %ls", installdir);
	if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\CurrentControlSet\\services"), 0, KEY_READ, &hKey)==ERROR_SUCCESS) {
		DWORD index = 0;
		TCHAR keyname[1024];
		DWORD keylen = sizeof(keyname);
		FILETIME t;
		/* Go through all services in the registry */
		while(RegEnumKeyExW(hKey, index, keyname, &keylen, NULL, NULL, NULL, &t) == ERROR_SUCCESS) {
			HKEY hServiceKey = 0;
			TCHAR path[1024];
			DWORD pathlen = sizeof(path)-1;
			if (RegOpenKeyExW(hKey, keyname, NULL, KEY_READ, &hServiceKey) == ERROR_SUCCESS) {
				/* Look at the ImagePath value of each service */
				if (RegQueryValueExW(hServiceKey, TEXT("ImagePath"), NULL, NULL, (LPBYTE)path, &pathlen) == ERROR_SUCCESS) {
					path[pathlen] = 0;
					TCHAR *p = path;
					if(p[0] == '"') p += 1;
					/* See if it is similar to our install directory */
					if(wcsncmp(p, installdir, wcslen(installdir)) == 0) {
						WcaLog(LOGMSG_STANDARD, "Found service '%ls' with ImagePath '%ls'.", keyname, path);
						swprintf_s(last_service_name, sizeof(last_service_name), TEXT("%ls"), keyname);
						/* If we are supposed to stop and remove the service... */
						if(!check_only) {
							WcaLog(LOGMSG_STANDARD, "Trying to stop the service.");
							SC_HANDLE hSCM = NULL; 
							hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
							if(hSCM != NULL) {
								SC_HANDLE hService = NULL;
								hService = OpenService(hSCM, keyname, SERVICE_STOP|SERVICE_QUERY_STATUS|DELETE);
								if(hService != NULL) {
									WcaLog(LOGMSG_STANDARD, "Waiting for the service to stop...");
									SERVICE_STATUS status;
									/* Attempt to stop the service */
									if(ControlService(hService, SERVICE_CONTROL_STOP, &status)) {
										/* Now wait until it's stopped */
										while("it's one big, mean and cruel world out there") {
											if(!QueryServiceStatus(hService, &status)) break;
											if(status.dwCurrentState == SERVICE_STOPPED) break;
											Sleep(1000);
										}
										WcaLog(LOGMSG_STANDARD, "Stopped the service.");
									}
									/* Mark the service for deletion */
									DeleteService(hService);
									CloseServiceHandle(hService);
								}
								CloseServiceHandle(hSCM);
							}
						}
						done++;
					}
				}
				RegCloseKey(hServiceKey);
			}
			index++;
			keylen = sizeof(keyname)-1;
		}
		RegCloseKey(hKey);
	} else {
		WcaLog(LOGMSG_STANDARD, "Can't seem to go through the list of installed services in the registry.");
	}
	return done;
}
Esempio n. 12
0
File: sexe.c Progetto: buzz26/toyBox
//
//	Windows メイン関数
//
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpszArgs, int nWinMode)
{
    MSG msg;
	HWND hMain;
	int err;

	// 初期化
	install_flag = FALSE;
	uninstall_flag = FALSE;
	read_ini_flag = FALSE;
	start_flag = FALSE;
	no_error_flag = FALSE;
	question_flag = FALSE;
	ok_flag = FALSE;

	h_instance = hInstance;

	// 実行ディレクトリにある sexe.exe のフルパスを作成
	GetModuleFileName(NULL, module_name, sizeof(module_name));
	lstrcpy(execute_path, module_name);
	extract_directory(execute_path);
	lstrcpy(ini_name, execute_path);
	lstrcat(ini_name, _T("\\sexe.ini"));

	// コマンドライン解析
	analyze_args(lpszArgs);

	// サービスで動作しているか?
	service_flag = check_execute_service();
	// WindowsNT/2000 ?
	if(nt_flag) {
		// サービスとして動作中?
		if(service_flag) {
			// すでに動作中?
			if(!check_already()) {
				// サービスとして起動
				start_service();
			}
		} else {
			// 2011/5/31
			// コマンドラインパラメータでインストール・アンインストール
			if(install_flag) {
				TCHAR *ext;
				if(service_name[0] == _T('\0')) {
					if(!no_error_flag) {
						// サービス名を指定してください
						MessageBoxResourceText(NULL, IDS_ERROR_NO_SERVICE_NAME, NULL, ERROR_HEADER, MB_OK);
					}
					return ERROR_PARAMETER;
				}
				ext = extract_ext(exe_name);
				if(exe_name[0] == _T('\0') || (_tcsicmp(ext, _T("exe")) && _tcsicmp(ext, _T("bat")))) {
					if(!no_error_flag) {
						// プログラム名を指定してください
						MessageBoxResourceText(NULL, IDS_ERROR_NO_PROGRAM_NAME, NULL, ERROR_HEADER, MB_OK);
					}
					return ERROR_PARAMETER;
				}
				if(question_flag) {
					// サービス service_name を登録しますか?
					if(MessageBoxResourceText(NULL, IDS_QUESTION_INSTALL, service_name, ERROR_HEADER, MB_YESNO) != IDYES) {
						return ERROR_NO_INSTALL;
					}
				}
				if(!read_ini_flag) {
					// ini ファイルから読み出したのでなければ設定値を保存
					set_inifile();
				}
				// インストール
				if((err = install_service()) == ERROR_SUCCESS) {
					if(start_flag) {
						// サービス開始
						if(restart_service()) {
							if(ok_flag) {
								// サービス service_name を登録し、開始しました。
								MessageBoxResourceText(NULL, IDS_INSTALL_START_OK, service_name, HEADER, MB_OK);
							}
						} else if(!no_error_flag) {
							// サービス service_name を登録しましたが、開始に失敗しました。
							MessageBoxResourceText(NULL, IDS_ERROR_INSTALL_START, service_name, HEADER, MB_OK);
							return ERROR_START;
						}
					} else if(ok_flag) {
						// サービス service_name を登録しました。
						MessageBoxResourceText(NULL, IDS_INSTALL_OK, service_name, HEADER, MB_OK);
					}
				} else {
					if(!no_error_flag) {
						if(err == ERROR_SERVICE_EXISTS) {
							// すでに同名のサービスが登録済みです
							MessageBoxResourceText(NULL, IDS_ERROR_SAME_SERVICE, NULL, ERROR_HEADER, MB_OK);
						} else {
							// サービスに登録できませんでした。\nサービスの権限があるユーザーでログインして実行してください。
							MessageBoxResourceText(NULL, IDS_ERROR_INSTALL_SERVICE, NULL, ERROR_HEADER, MB_OK);
						}
					}
					return ERROR_INSTALL;
				}
			} else if(uninstall_flag) {
				if(service_name[0] == _T('\0')) {
					if(!no_error_flag) {
						// サービス名を指定してください
						MessageBoxResourceText(NULL, IDS_ERROR_NO_SERVICE_NAME, NULL, ERROR_HEADER, MB_OK);
					}
					return ERROR_PARAMETER;
				}
				if(question_flag) {
					// サービス service_name を削除しますか?
					if(MessageBoxResourceText(NULL, IDS_QUESTION_UNINSTALL, service_name, HEADER, MB_YESNO) != IDYES) {
						return ERROR_NO_INSTALL;
					}
				}
				if(service_install_flag) {
					// サービスから削除
					if(remove_service()) {
						if(ok_flag) {
							// サービス service_name を削除しました
							MessageBoxResourceText(NULL, IDS_UNINSTALL_OK, service_name, HEADER, MB_OK);
						}
					} else {
						if(!no_error_flag) {
							// サービスから削除できませんでした。\nサービスの権限があるユーザーでログインして実行してください。
							MessageBoxResourceText(NULL, IDS_ERROR_UNINSTALL_SERVICE, NULL, ERROR_HEADER, MB_OK);
						}
						return ERROR_INSTALL;
					}
				} else {
					if(!no_error_flag) {
						// サービス service_name は登録されていません
						MessageBoxResourceText(NULL, IDS_ERROR_NOT_INSTALL_SERVICE, service_name, ERROR_HEADER, MB_OK);
					}
					return ERROR_INSTALL;
				}
			} else {
				// 2010/6/10 Vista/7 で WM_DROPFILES を受ける
				funcChangeWindowMessageFilter ChangeWindowMessageFilter;
				if(ChangeWindowMessageFilter = (funcChangeWindowMessageFilter)GetProcAddress(LoadLibrary(_T("user32.dll")) ,"ChangeWindowMessageFilter")) {
					ChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);
					ChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ADD);
					ChangeWindowMessageFilter(0x0049, MSGFLT_ADD);
				}

				// 設定ダイアログを表示
				hMain = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG_SETUP), GetDesktopWindow(), (DLGPROC)MainFunc);
				ShowWindow(hMain, SW_SHOW);
				// Drag&Drop を受け入れる準備
				DragAcceptFiles(hMain, TRUE);
				while(GetMessage(&msg, NULL, 0, 0)) {
					if(!IsDialogMessage(hMain, &msg)) {
						TranslateMessage(&msg);
						DispatchMessage(&msg);
					}
				}
			}
		}
	} else {
		// Windows NT/2000/XP/Vista/7 で起動してください。
		MessageBoxResourceText(NULL, IDS_ERROR_OS, NULL, ERROR_HEADER, MB_OK);
	}
	return 0;
}
Esempio n. 13
0
File: sexe.c Progetto: buzz26/toyBox
//
//	ダイアログメッセージ処理
//
BOOL APIENTRY MainFunc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	HWND h;
	HDROP hDrop;
	OPENFILENAME ofn;
	HICON hicon;
	TCHAR temp[MAX_PATH];
	TCHAR *ext;

	switch(message) {
	case WM_INITDIALOG:
		// アイコンをセット
		if(hicon = LoadIcon(h_instance, MAKEINTRESOURCE(IDI_ICON_SEXE))) {
			SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hicon);
			SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)hicon);
		}
		// 設定値をコントロールにセット
		h = GetDlgItem(hDlg, IDC_EDIT_EXE);
		SendMessage(h, EM_LIMITTEXT, MAX_PATH, 0);
		SetWindowText(h, exe_name);
		EnableWindow(h, !service_install_flag);

		h = GetDlgItem(hDlg, IDC_EDIT_OPTION);
		SendMessage(h, EM_LIMITTEXT, MAX_PATH, 0);
		SetWindowText(h, option_name);
		EnableWindow(h, !service_install_flag);

		h = GetDlgItem(hDlg, IDC_EDIT_NAME);
		SendMessage(h, EM_LIMITTEXT, MAX_PATH, 0);
		SetWindowText(h, service_name);
		EnableWindow(h, !service_install_flag);

		h = GetDlgItem(hDlg, IDC_EDIT_DESCRIPTION);
		SendMessage(h, EM_LIMITTEXT, MAX_PATH, 0);
		SetWindowText(h, description_name);
		EnableWindow(h, !service_install_flag);

		h = GetDlgItem(hDlg, IDC_COMBO_END);
		LoadString(h_instance, IDS_ITEM_CLOSE, temp, MAX_PATH);
		SendMessage(h, CB_ADDSTRING, 0, (DWORD_PTR)temp);
		// 2002/6/18
		LoadString(h_instance, IDS_ITEM_SYSCOMMAND, temp, MAX_PATH);
		SendMessage(h, CB_ADDSTRING, 0, (DWORD_PTR)temp);
		LoadString(h_instance, IDS_ITEM_SYS_CLOSE, temp, MAX_PATH);
		SendMessage(h, CB_ADDSTRING, 0, (DWORD_PTR)temp);
		// 2004/8/9
		LoadString(h_instance, IDS_ITEM_CTRL_BREAK, temp, MAX_PATH);
		SendMessage(h, CB_ADDSTRING, 0, (DWORD_PTR)temp);
		SendMessage(h, CB_SETCURSEL, end_pattern, 0);
		EnableWindow(h, !service_install_flag);

		// 2001/11/9
		h = GetDlgItem(hDlg, IDC_BUTTON_EXE);
		EnableWindow(h, !service_install_flag);

		h = GetDlgItem(hDlg, IDC_BUTTON_TEST);
		if(service_install_flag) {
			LoadString(h_instance, IDS_BUTTON_DELETE, temp, MAX_PATH);
			SetWindowText(h, temp);
		}

		// 2004/8/9
		h = GetDlgItem(hDlg, IDC_CHECK_AUTO);
		SendMessage(h, BM_SETCHECK, auto_flag, 0);
		EnableWindow(h, !service_install_flag);

		h = GetDlgItem(hDlg, IDC_CHECK_DESKTOP);
		SendMessage(h, BM_SETCHECK, desktop_flag, 0);
		EnableWindow(h, !service_install_flag);

		h = GetDlgItem(hDlg, IDC_CHECK_RETRY);
		SendMessage(h, BM_SETCHECK, retry_flag, 0);
		EnableWindow(h, !service_install_flag);
		break;

	case WM_COMMAND:
		switch(LOWORD (wParam)) {
		case IDOK:
			// 設定値得る
			if(!service_install_flag) {
				h = GetDlgItem(hDlg, IDC_EDIT_NAME);
				GetWindowText(h, service_name, MAX_PATH);
				if(service_name[0] == _T('\0')) {
					// サービス名を指定してください
					MessageBoxResourceText(hDlg, IDS_ERROR_NO_SERVICE_NAME, NULL, ERROR_HEADER, MB_OK);
					break;
				}
				h = GetDlgItem(hDlg, IDC_EDIT_EXE);
				GetWindowText(h, exe_name, MAX_PATH);
				// 2007/12/14 test
				ext = extract_ext(exe_name);
				if(exe_name[0] == _T('\0') || (_tcsicmp(ext, _T("exe")) && _tcsicmp(ext, _T("bat")))) {
					// プログラム名を指定してください
					MessageBoxResourceText(hDlg, IDS_ERROR_NO_PROGRAM_NAME, NULL, ERROR_HEADER, MB_OK);
					break;
				}

				h = GetDlgItem(hDlg, IDC_EDIT_OPTION);
				GetWindowText(h, option_name, MAX_PATH);

				h = GetDlgItem(hDlg, IDC_EDIT_DESCRIPTION);
				GetWindowText(h, description_name, MAX_PATH);

				h = GetDlgItem(hDlg, IDC_COMBO_END);
				end_pattern = (int)SendMessage(h, CB_GETCURSEL, 0, 0);

				// 2004/8/9
				h = GetDlgItem(hDlg, IDC_CHECK_AUTO);
				auto_flag = (int)SendMessage(h, BM_GETCHECK, 0, 0);

				h = GetDlgItem(hDlg, IDC_CHECK_DESKTOP);
				desktop_flag = (int)SendMessage(h, BM_GETCHECK, 0, 0);

				h = GetDlgItem(hDlg, IDC_CHECK_RETRY);
				retry_flag = (int)SendMessage(h, BM_GETCHECK, 0, 0);

				// 設定値を保存
				set_inifile();

				// サービス %s を登録しますか?
				if(MessageBoxResourceText(hDlg, IDS_QUESTION_INSTALL, service_name, HEADER, MB_YESNO) == IDYES) {
					int err;
					if((err = install_service()) == ERROR_SUCCESS) {
						// 2001/11/9
						// サービス %s を登録しました。サービスとして起動しますか?
						if(MessageBoxResourceText(hDlg, IDS_QUESTION_INSTALL_START, service_name, HEADER, MB_YESNO) == IDYES) {
							restart_service();
						}
					} else {
						if(err == ERROR_SERVICE_EXISTS) {
							// すでに同名のサービスが登録済みです
							MessageBoxResourceText(NULL, IDS_ERROR_SAME_SERVICE, NULL, ERROR_HEADER, MB_OK);
						} else {
							// サービスに登録できませんでした。\nサービスの権限があるユーザーでログインして実行してください。
							MessageBoxResourceText(hDlg, IDS_ERROR_INSTALL_SERVICE, NULL, HEADER, MB_OK);
						}
					}
				}
			}
			DestroyWindow(hDlg);
			break;

		case IDCANCEL:
			DestroyWindow(hDlg);
			break;

		case IDC_BUTTON_EXE:
			{
				TCHAR filter[MAX_PATH];
				TCHAR title[MAX_PATH];
				int len;
				// 参照ボタンが押された
				temp[0] = '\0';
				FillMemory(&ofn, sizeof(OPENFILENAME), 0);
				ofn.lStructSize = sizeof(OPENFILENAME);
				ofn.hwndOwner = hDlg;
				ofn.nMaxFile = MAX_PATH;
				ofn.lpstrFile = temp;
				// 実行ファイル(*.exe)\0*.exe\0"
				LoadString(h_instance, IDS_FILE_EXE, filter, MAX_PATH);
				len = lstrlen(filter);
				LoadString(h_instance, IDS_FILE_WILD_EXE, filter + len + 1, MAX_PATH - len - 2);
				len += lstrlen(filter + len + 1);
				*(filter + len + 2) = '\0';
				ofn.lpstrFilter = filter;
				// 実行ファイルの選択
				LoadString(h_instance, IDS_FILE_TITLE, title, MAX_PATH);
				ofn.lpstrTitle = title;
				ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
				if(GetOpenFileName(&ofn)) {
					ext = extract_ext(temp);
					if(!_tcsicmp(ext, _T("exe")) || !_tcsicmp(ext, _T("bat"))) {
						TCHAR name[MAX_PATH];
						// .exe ファイルをセット
						h = GetDlgItem(hDlg, IDC_EDIT_EXE);
						SetWindowText(h, (LPCTSTR)temp);
						// サービス名として .exe ファイルの名前をセット
						extract_name_only(name, temp);
						h = GetDlgItem(hDlg, IDC_EDIT_NAME);
						SetWindowText(h, (LPCTSTR)name);
					} else {
						// プログラムファイルのみ登録可能です。
						MessageBoxResourceText(hDlg, IDS_ERROR_NOT_PROGRAM, NULL, ERROR_HEADER, MB_OK);
					}
				}
			}
			break;

		case IDC_BUTTON_TEST:
			// サービスとしてインストール済み?
			if(service_install_flag) {
				// サービス service_name を削除しますか?
				if(MessageBoxResourceText(hDlg, IDS_QUESTION_UNINSTALL, service_name, HEADER, MB_YESNO) == IDYES) {
					// サービスから削除
					if(remove_service()) {
						// サービス service_name を削除しました
						MessageBoxResourceText(hDlg, IDS_UNINSTALL_OK, service_name, HEADER, MB_OK);

						service_install_flag = FALSE;
						h = GetDlgItem(hDlg, IDC_EDIT_EXE);
						EnableWindow(h, TRUE);

						h = GetDlgItem(hDlg, IDC_EDIT_OPTION);
						EnableWindow(h, TRUE);

						h = GetDlgItem(hDlg, IDC_EDIT_NAME);
						EnableWindow(h, TRUE);

						h = GetDlgItem(hDlg, IDC_EDIT_DESCRIPTION);
						EnableWindow(h, TRUE);

						h = GetDlgItem(hDlg, IDC_COMBO_END);
						EnableWindow(h, TRUE);

						// 2001/11/9
						h = GetDlgItem(hDlg, IDC_BUTTON_EXE);
						EnableWindow(h, TRUE);

						h = GetDlgItem(hDlg, IDC_BUTTON_TEST);
						// テスト起動(&T)
						LoadString(h_instance, IDS_BUTTON_TEST, temp, MAX_PATH);
						SetWindowText(h, temp);

						// 2004/8/9
						h = GetDlgItem(hDlg, IDC_CHECK_AUTO);
						EnableWindow(h, TRUE);

						h = GetDlgItem(hDlg, IDC_CHECK_DESKTOP);
						EnableWindow(h, TRUE);

						h = GetDlgItem(hDlg, IDC_CHECK_RETRY);
						EnableWindow(h, TRUE);
					} else {
						// サービスから削除できませんでした。\nサービスの権限があるユーザーでログインして実行してください。
						MessageBoxResourceText(NULL, IDS_ERROR_UNINSTALL_SERVICE, NULL, ERROR_HEADER, MB_OK);
					}
				}
			} else {
				TCHAR param[MAX_PATH];

				// テスト起動処理
				h = GetDlgItem(hDlg, IDC_EDIT_EXE);
				GetWindowText(h, temp, MAX_PATH);

				h = GetDlgItem(hDlg, IDC_EDIT_OPTION);
				GetWindowText(h, param, MAX_PATH);

				h = GetDlgItem(hDlg, IDC_COMBO_END);
				execute_program(hDlg, temp, param, (int)SendMessage(h, CB_GETCURSEL, 0, 0));
			}
			break;
		}
		break;

	case WM_DROPFILES:
		// Drag&Drop で受けたファイル名を取り出す
		hDrop = (HDROP)wParam;
		DragQueryFile(hDrop, 0, temp, MAX_PATH);
		DragFinish(hDrop);
		if(!service_install_flag) {
			if(!_tcsicmp(extract_ext(temp), _T("exe"))) {
				TCHAR name[MAX_PATH];
				// .exe ファイルをセット
				h = GetDlgItem(hDlg, IDC_EDIT_EXE);
				SetWindowText(h, temp);
				// サービス名として .exe ファイルの名前をセット
				extract_name_only(name, temp);
				h = GetDlgItem(hDlg, IDC_EDIT_NAME);
				SetWindowText(h, (LPCTSTR)name);
			} else {
				// プログラムファイルのみ登録可能です。
				MessageBoxResourceText(hDlg, IDS_ERROR_NOT_PROGRAM, NULL, ERROR_HEADER, MB_OK);
			}
		}
		SetForegroundWindow(hDlg);
		break;

	case WM_HELP:
		ShellExecute(hDlg, _T("open"), _T("sexe.chm"), NULL, NULL, SW_SHOW);
		break;

	case WM_DESTROY:
		// 2001/11/9
		if(service_stop_flag) {
			restart_service();
		}
		PostQuitMessage(0);
		break;

	default:
		return FALSE;
	}
	return FALSE;
}
Esempio n. 14
0
/* Called whenever a new service is found or removed */
static void browse_reply(
        AvahiServiceBrowser *UNUSED(b),
        AvahiIfIndex interface,
        AvahiProtocol protocol,
        AvahiBrowserEvent event,
        const char *name,
        const char *type,
        const char *domain,
        AvahiLookupResultFlags UNUSED(flags),
        void *userdata) {

    struct daemon_data *d = userdata;
    assert(d);

    switch (event) {
        case AVAHI_BROWSER_NEW: {
            struct host *h;

            h = malloc(sizeof(struct host));
            assert(h);

            rs_log_info("new service: %s\n", name);

            if (!(h->resolver = avahi_service_resolver_new(d->client,
                                                           interface,
                                                           protocol,
                                                           name,
                                                           type,
                                                           domain,
                                                           AVAHI_PROTO_UNSPEC,
                                                           0,
                                                           resolve_reply,
                                                           h))) {
                rs_log_warning("Failed to create service resolver for '%s': %s\n", name,
                               avahi_strerror(avahi_client_errno(d->client)));

                free(h);

            } else {

                /* Fill in missing data */
                h->service = strdup(name);
                assert(h->service);
                h->domain = strdup(domain);
                assert(h->domain);
                h->daemon_data = d;
                h->interface = interface;
                h->protocol = protocol;
                h->next = d->hosts;
                h->n_cpus = 1;
                d->hosts = h;
            }

            break;
        }

        case AVAHI_BROWSER_REMOVE:

            rs_log_info("Removed service: %s\n", name);

            remove_service(d, interface, protocol, name, domain);
            write_hosts(d);
            break;

        case AVAHI_BROWSER_FAILURE:
            rs_log_crit("Service Browser failure '%s': %s\n", name,
                        avahi_strerror(avahi_client_errno(d->client)));

            avahi_simple_poll_quit(d->simple_poll);
            break;

        case AVAHI_BROWSER_CACHE_EXHAUSTED:
        case AVAHI_BROWSER_ALL_FOR_NOW:
            ;

    }
}
Esempio n. 15
0
int main(int argc, char *argv[]) {
    int ret = 1, error;
    Config config;
    const char *argv0;
    char *ec;

    //avahi_init_i18n();
    setlocale(LC_ALL, "");

    if ((argv0 = strrchr(argv[0], '/')))
        argv0++;
    else
        argv0 = argv[0];

    if ((ec = getenv("COLUMNS")))
        n_columns = atoi(ec);

    if (n_columns < 40)
        n_columns = 40;

    /*if (parse_command_line(&config, argv0, argc, argv) < 0)
        goto fail;*/

    config.command = COMMAND_BROWSE_SERVICES;
    config.terminate_on_all_for_now = 1;
    config.verbose = 0;
    config.terminate_on_cache_exhausted = 0;
    config.ignore_local = 0;
    config.resolve = 1;
    config.no_fail = 1;
    config.parsable = 0;
    config.domain = NULL;
    config.stype = avahi_strdup("_rtp._tcp");


    switch (config.command) {
        case COMMAND_HELP:
            help(stdout, argv0);
            ret = 0;
            break;

        case COMMAND_VERSION:
            printf("%s "PACKAGE_VERSION"\n", argv0);
            ret = 0;
            break;

        case COMMAND_BROWSE_SERVICES:
        case COMMAND_BROWSE_ALL_SERVICES:
        case COMMAND_BROWSE_DOMAINS:

            if (!(simple_poll = avahi_simple_poll_new())) {
                fprintf(stderr, ("Failed to create simple poll object.\n"));
                goto fail;
            }

/*            if (sigint_install(simple_poll) < 0)
                goto fail;*/

            if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) {
                fprintf(stderr, ("Failed to create client object: %s\n"), avahi_strerror(error));
                goto fail;
            }

            avahi_simple_poll_loop(simple_poll);
            ret = 0;
            break;

#if defined(HAVE_GDBM) || defined(HAVE_DBM)
        case COMMAND_DUMP_STDB: {
            char *t;
            stdb_setent();

            while ((t = stdb_getent())) {
                if (config.no_db_lookup)
                    printf("%s\n", t);
                else
                    printf("%s\n", stdb_lookup(t));
            }

            ret = 0;
            break;
        }
#endif
    }


    fail:

    while (services)
        remove_service(&config, services);

    if (client)
        avahi_client_free(client);

    //sigint_uninstall();

    if (simple_poll)
        avahi_simple_poll_free(simple_poll);

    avahi_free(config.domain);
    avahi_free(config.stype);

    avahi_string_list_free(browsed_types);

#if defined(HAVE_GDBM) || defined(HAVE_DBM)
    stdb_shutdown();
#endif

    return ret;
}
Esempio n. 16
0
/*  ---------------------------------------------------------------------[<]-
    Function: service_begin

    Synopsis: depending on arguments (from the command line):
      -i: install service               (windows)
      -u: remove  service               (windows)
      -d: runs service in console mode  (windows)
      -h: basic help information        (windows)
      -d: runs service in background    (UNIX / Linux)

      if no arguments, the service is actually started.

    NOTE: with Windows, the working directory is set to the one where the
    binary program stands.

    Returns: 0 is everything is OK, negative error code otherwise
    ---------------------------------------------------------------------[>]-*/
int
service_begin (
    int                   argc, 
    char                **argv,
    SMT_AGENTS_INIT_FCT  *init_fct, 
    SMT_AGENTS_TERM_FCT  *term_fct,
    const char           *appl_version)
{  
    int
        action;
    int
        rc = 0;

#if (defined(WIN32)) 
    static char
        buffer [LINE_MAX];
    char
        *p_char;

    SERVICE_TABLE_ENTRY 
        dispatch_table [] = {
        { NULL, (LPSERVICE_MAIN_FUNCTION) service_main },
        { NULL, NULL }
    };

    /*  Change to the correct working directory, where config file stands    */
    GetModuleFileName (NULL, buffer, LINE_MAX);
    if ((p_char = strrchr (buffer, '\\')) != NULL)
        *p_char = '\0';
    SetCurrentDirectory (buffer);
#endif

    rc = init_resources (argv[0], appl_version, init_fct, term_fct);
    if (rc != 0)
        return (1);

    ASSERT (application_config);        /* init_resources post condition     */
    if (load_service_config (application_config) != 0)
      {
        free_resources ();
        return (1);
      }

    ASSERT (service_trace_file);        /* load_service_config postcondition */
    console_set_mode (CONSOLE_DATETIME);
    console_capture (service_trace_file, 'a');

#if (defined(WIN32)) 
    dispatch_table [0].lpServiceName = service_name;
    win_version = get_windows_version ();
#endif

    action = parse_command_line (argc, argv);

    if (action == ACTION_HELP)
      {
        puts (USAGE);
      }
#if (defined(WIN32)) 
    else
    if (action == ACTION_INSTALL)
      {
        if (win_version == WINDOWS_95)
            set_win95_service (TRUE);
        else
            install_service ();
      }
    else 
    if (action == ACTION_UNINSTALL)
      {
        if (win_version == WINDOWS_95)
            set_win95_service (FALSE);
        else
            remove_service ();
      }
    else
    if (action == ACTION_CONSOLE)
      {
        console_mode  = TRUE;
        console_service (argc, argv);
      }
    else
    if (action == ACTION_NOARG)
      {
        console_send (NULL, FALSE);

        if (win_version == WINDOWS_95)
          {
            hide_window ();
            console_mode = TRUE;
            console_service (argc, argv);
          }
        else
        if (win_version == WINDOWS_NT_3X
        ||  win_version == WINDOWS_NT_4
        ||  win_version == WINDOWS_2000)
          {
            log_printf ("%s: initialising service ...", application_name);
            if (!StartServiceCtrlDispatcher (dispatch_table))
                add_to_message_log ("StartServiceCtrlDispatcher failed");
          }
      }
#elif (defined(__UNIX__))
    else
    if (action == ACTION_BACKGROUND)
      {
        const char
           *background_args [] = { "-s", NULL };

        log_printf ("Moving into the background");
        if (process_server (NULL, NULL, argc, argv, background_args) != 0)
          {
            log_printf ("Backgrounding failed.  Giving up.");
            rc = -1;
          }
        else
          action = ACTION_NOARG;
      }
    if (action == ACTION_NOARG)
      {
        rc = smt_init ();
        if (!rc && (init_fct != NULL))
            rc = (*init_fct)(application_config);
        if (!rc)
            smt_exec_full ();
        if (term_fct != NULL)
            (*term_fct)();
        smt_term ();
      }
#endif
    else
    if (action == ACTION_ERROR)
        puts (USAGE);
    
    free_resources ();
    return rc;
}