Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT sources DOT redhat DOT com Delivered-To: mailing list cygwin AT sources DOT redhat DOT com Date: Thu, 9 Aug 2001 16:05:24 +0200 From: Corinna Vinschen To: cygwin AT cygwin DOT com Subject: Re: Rebooting a windows 2000 box Message-ID: <20010809160524.A2117@cygbert.vinschen.de> Mail-Followup-To: cygwin AT cygwin DOT com References: <20010801093425 DOT C9574 AT cygbert DOT vinschen DOT de> <86YulJ4g7tB AT polaris DOT franken DOT de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <86YulJ4g7tB@polaris.franken.de>; from Polaris@polaris.franken.de on Thu, Aug 09, 2001 at 03:01:00PM +0200 On Thu, Aug 09, 2001 at 03:01:00PM +0200, Norbert Fischer wrote: > Corinna Vinschen wrote: > > > I wrote the following tiny code in 1998. If you call it `shutdown.exe' > > it will shutdown, call it `reboot.exe' and it will reboot. Otherwise > > use the flags. > > Did you write it especially for WinNT/2000 ? Yes. > Is it complicated to rewrite it for Win98 ? No. Try this: ================================================================================ /* * shutdown.c: implementation of shutdown(1) as part of a Cygwin environment * * Copyright 1998,2001 Corinna Vinschen, * bug reports to cygwin AT cygwin DOT com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include static char *SCCSid = "@(#)shutdown V1.1, Corinna Vinschen, " __DATE__ "\n"; char *myname; int usage(void) { fprintf(stderr, "usage: %s [-fr] secs|\"now\"\n", myname); return 1; } BOOL (*openprocesstoken)(HANDLE,DWORD,PHANDLE); BOOL (*adjusttokenprivileges)(HANDLE,BOOL,PTOKEN_PRIVILEGES,DWORD,PTOKEN_PRIVILEGES,PDWORD); BOOL (*lookupprivilegevalue)(LPCTSTR,LPCTSTR,PLUID); int setprivs(void) { HANDLE token; TOKEN_PRIVILEGES privs; HMODULE adv; /* Privileges are not supported on 9x/ME. */ if (GetVersion () >= 0x80000000L) return 0; if (! (adv = LoadLibrary ("advapi32.dll"))) { int ret = GetLastError(); fprintf(stderr, "%s: can't load advapi32.dll\n", myname); return 1; } if (! (lookupprivilegevalue = (BOOL(*)(LPCTSTR,LPCTSTR,PLUID)) GetProcAddress (adv, "LookupPrivilegeValueA"))) { int ret = GetLastError(); fprintf(stderr, "%s: can't load symbol from advapi32.dll\n", myname); return 1; } if (! (openprocesstoken = (BOOL(*)(HANDLE,DWORD,PHANDLE)) GetProcAddress (adv, "OpenProcessToken"))) { int ret = GetLastError(); fprintf(stderr, "%s: can't load symbol from advapi32.dll\n", myname); return 1; } if (! (adjusttokenprivileges = (BOOL(*)(HANDLE,BOOL,PTOKEN_PRIVILEGES, DWORD,PTOKEN_PRIVILEGES,PDWORD)) GetProcAddress (adv, "AdjustTokenPrivileges"))) { int ret = GetLastError(); fprintf(stderr, "%s: can't load symbol from advapi32.dll\n", myname); return 1; } privs.PrivilegeCount = 1; lookupprivilegevalue(NULL, SE_SHUTDOWN_NAME, &privs.Privileges[0].Luid); privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (! openprocesstoken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) { int ret = GetLastError(); fprintf(stderr, "%s: insufficient privileges\n", myname); return 1; } if (! adjusttokenprivileges(token, FALSE, &privs, 0, NULL, NULL)) { fprintf(stderr, "%s: insufficient privileges\n", myname); return 1; } return 0; } int main(int argc, char **argv) { int c; unsigned int secs; int action = EWX_POWEROFF; int force = 0; if ((myname = strrchr(argv[0], '/')) || (myname = strrchr(argv[0], '\\'))) ++myname; else myname = argv[0]; if (strrchr(myname, '.')) *strrchr(myname, '.') = '\0'; if (! strcmp(myname, "reboot")) action = EWX_REBOOT; while ((c = getopt(argc, argv, "fr")) != EOF) switch (c) { case 'f': /* EWX_FORCE doesn't work correctly on 9x/ME. */ if (GetVersion () < 0x80000000L) force = EWX_FORCE; break; case 'r': action = EWX_REBOOT; break; default: return usage(); } if (optind >= argc) return usage(); if (! strcmp(argv[optind], "now")) secs = 0; else if (! isdigit(argv[optind][0])) { usage(); fprintf(stderr, "%s: secs must be numerical or the word \"now\"\n", myname); return 2; } else secs = atoi(argv[optind]); if (setprivs()) return 3; printf("WARNING!!! System is going down "); if (secs) printf("in %d seconds\n", secs); else printf("NOW\n"); while (secs) secs = sleep(secs); if (! ExitWindowsEx(action | force, 0)) { fprintf(stderr, "%s: insufficient privileges\n", myname); return 3; } return 0; } ================================================================================ Corinna -- Corinna Vinschen Please, send mails regarding Cygwin to Cygwin Developer mailto:cygwin AT cygwin DOT com Red Hat, Inc. -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Bug reporting: http://cygwin.com/bugs.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/