├── .gitignore ├── COPYING ├── Makefile.am ├── README.md ├── RHSrvAny.sln ├── RHSrvAny ├── Makefile.am ├── RHSrvAny.c ├── RHSrvAny.h ├── RHSrvAny.mc ├── RHSrvAny.vcproj ├── RHSrvAnyInfo.rc ├── mc.bat └── resource.h ├── configure.ac └── pnp_wait ├── Makefile.am └── pnp_wait.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | *.o 3 | *~ 4 | 5 | .deps 6 | Makefile 7 | Makefile.in 8 | 9 | /aclocal.m4 10 | /autom4te.cache 11 | /compile 12 | /config.guess 13 | /config.h 14 | /config.h.in 15 | /config.log 16 | /config.status 17 | /config.sub 18 | /configure 19 | /depcomp 20 | /install-sh 21 | /mingw32-config.cache 22 | /missing 23 | /stamp-h1 24 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | # RHSrvAny - Turn any Windows program into a Windows service. 2 | # Written by Yuval Kashtan. 3 | # Copyright (C) 2010 Red Hat Inc. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 2 of the License, or 8 | # (at your option) any later version. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program; if not, write to the Free Software 17 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 | 19 | # Note that this program only makes sense when cross-compiled as a 20 | # Windows program. Therefore you probably need to use something like 21 | # the Fedora Windows cross-compiler and invoke 'mingw32-configure' 22 | # instead of the normal './configure' command. 23 | 24 | SUBDIRS = RHSrvAny pnp_wait 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rhsrvany 2 | 3 | **Turn any Windows program or script into a service.** 4 | 5 | When you need to run some program or batch script as a Windows 6 | service, you either have to modify the program so it calls the Windows 7 | service APIs (impossible for a batch script), or you use some helper 8 | tool like Microsoft "SrvAny" that turns the program/script into a 9 | service. 10 | 11 | Unfortunately SrvAny is a proprietary program. 12 | 13 | This project, **rhsrvany**, is a lightweight free software equivalent. 14 | 15 | ## More information about rhsrvany 16 | 17 | For more information, see: 18 | 19 | http://rwmj.wordpress.com/2010/04/29/tip-install-a-service-in-a-windows-vm/#content 20 | 21 | Please send patches etc to the virt-tools mailing list: 22 | 23 | http://www.redhat.com/mailman/listinfo/virt-tools-list 24 | 25 | This program was originally written by Yuval Kashtan as part of the 26 | Qumranet / RHEV-M / oVirt project, and released as free software by 27 | Red Hat. 28 | 29 | This program is currently maintained by Richard W.M. Jones. 30 | 31 | ## Note for virt-v2v users 32 | 33 | You may have arrived here searching for a particular error message 34 | raised by the [virt-v2v][^virt-v2v] tool: 35 | 36 | ``` 37 | virt-v2v: error: One of rhsrvany.exe or pvvxsvc.exe is missing in /usr/share/virt-tools. 38 | 39 | One of them is required in order to install Windows firstboot scripts. 40 | 41 | You can get one by building rhsrvany (https://github.com/rwmjones/rhsrvany) 42 | ``` 43 | 44 | [^virt-v2v]: https://libguestfs.org/virt-v2v.1.html 45 | 46 | ## Binary releases 47 | 48 | This project does not release binary packages directly. Binaries are 49 | available on multiple Linux distributions which, normally, also ship 50 | `virt-v2v`. 51 | 52 | Please check if your distribution provides such binaries in the form 53 | of a package. 54 | 55 | It is possible to extract `rhsrvany.exe` from the distribution packages. 56 | 57 | Here is ane example, using Fedora's RPM package. It should work on any 58 | system that has the `rpm2cpio` tool. 59 | 60 | - Get the latest RPM from Fedora of the mingw-srvany package[^mingw-srvany] 61 | (you want the "noarch" one) 62 | - Extract the files using `rpm2cpio` (this tool is available on pretty much 63 | any distro) 64 | 65 | [^mingw-srvany]: https://koji.fedoraproject.org/koji/packageinfo?packageID=13522 66 | 67 | ### Full example 68 | 69 | The following code snippet would make `virt-v2v` happy on a Debian (or 70 | derivative) system: 71 | 72 | ```sh 73 | apt install -y rpm2cpio 74 | 75 | wget -nd -O srvany.rpm https://kojipkgs.fedoraproject.org//packages/mingw-srvany/1.1/4.fc38/noarch/mingw32-srvany-1.1-4.fc38.noarch.rpm 76 | 77 | rpm2cpio srvany.rpm | cpio -idmv \ 78 | && mkdir /usr/share/virt-tools \ 79 | && mv ./usr/i686-w64-mingw32/sys-root/mingw/bin/*exe /usr/share/virt-tools/ 80 | ``` 81 | 82 | which installs `pnp_wait.exe` and `rhsrvany.exe` into `/usr/share/virt-tools/` 83 | -------------------------------------------------------------------------------- /RHSrvAny.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 10.00 3 | # Visual Studio 2008 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RHSrvAny", "RHSrvAny\RHSrvAny.vcproj", "{D495A542-2328-44AC-AFD8-CA29D415FA16}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Win32 = Debug|Win32 9 | Release|Win32 = Release|Win32 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {D495A542-2328-44AC-AFD8-CA29D415FA16}.Debug|Win32.ActiveCfg = Debug|Win32 13 | {D495A542-2328-44AC-AFD8-CA29D415FA16}.Debug|Win32.Build.0 = Debug|Win32 14 | {D495A542-2328-44AC-AFD8-CA29D415FA16}.Release|Win32.ActiveCfg = Release|Win32 15 | {D495A542-2328-44AC-AFD8-CA29D415FA16}.Release|Win32.Build.0 = Release|Win32 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /RHSrvAny/Makefile.am: -------------------------------------------------------------------------------- 1 | # RHSrvAny - Turn any Windows program into a Windows service. 2 | # Written by Yuval Kashtan. 3 | # Copyright (C) 2010 Red Hat Inc. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 2 of the License, or 8 | # (at your option) any later version. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program; if not, write to the Free Software 17 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 | 19 | bin_PROGRAMS = rhsrvany$(EXEEXT) 20 | 21 | rhsrvany_SOURCES = resource.h RHSrvAny.h RHSrvAny.c 22 | -------------------------------------------------------------------------------- /RHSrvAny/RHSrvAny.c: -------------------------------------------------------------------------------- 1 | /* RHSrvAny - Turn any Windows program into a Windows service. 2 | * Written by Yuval Kashtan. 3 | * Copyright (C) 2010,2013 Red Hat Inc. 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 | */ 19 | 20 | #ifdef HAVE_CONFIG_H 21 | #include 22 | #endif /* HAVE_CONFIG_H */ 23 | 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #define SNPRINTF StringCchPrintf 32 | 33 | #include "RHSrvAny.h" 34 | 35 | static TCHAR *svcname = TEXT("RHSrvAny"); 36 | 37 | static SERVICE_STATUS gSvcStatus; 38 | static HANDLE ghSvcStopEvent = NULL; 39 | static SERVICE_STATUS_HANDLE gSvcStatusHandle; 40 | 41 | static int SvcInstall (void); 42 | static int SvcUninstall (void); 43 | 44 | VOID WINAPI SvcCtrlHandler (DWORD); 45 | VOID WINAPI SvcMain (DWORD, LPTSTR *); 46 | 47 | static VOID SvcReportEvent (LPTSTR); 48 | static VOID SvcInit (DWORD, LPTSTR *); 49 | static VOID ReportSvcStatus (DWORD, DWORD, DWORD); 50 | 51 | static int 52 | argument_error (const char *msg) 53 | { 54 | printf("%s\n", msg); 55 | 56 | return EXIT_FAILURE; 57 | } 58 | 59 | static int compat_tmain (int argc, TCHAR *argv[]); 60 | 61 | #ifdef __MINGW32__ 62 | int 63 | main (int argc, char **argv) 64 | { 65 | #ifdef UNICODE 66 | TCHAR **w_argv = CommandLineToArgvW(GetCommandLineW(), &argc); 67 | return compat_tmain(argc, w_argv); 68 | #else 69 | return compat_tmain(argc, argv); 70 | #endif 71 | } 72 | #else 73 | int 74 | _tmain(int argc, TCHAR *argv[]) 75 | { 76 | return compat_tmain(argc, argv); 77 | } 78 | #endif 79 | 80 | static int 81 | compat_tmain (int argc, TCHAR *argv[]) 82 | { 83 | SERVICE_TABLE_ENTRY DispatchTable[2]; 84 | int i; 85 | 86 | for (i = 1; i < argc; i++) { 87 | TCHAR *arg = argv[i]; 88 | 89 | if (arg[0] == TEXT('-')) { 90 | if (lstrcmpi(arg + 1, TEXT("s")) == 0) { 91 | if (i == argc) { 92 | return argument_error("Option -s requires an argument"); 93 | } 94 | 95 | svcname = argv[++i]; 96 | } 97 | 98 | else { 99 | return argument_error("Unknown option"); 100 | } 101 | } 102 | 103 | /* Stop parsing arguments when we hit something which isn't an option */ 104 | else { 105 | if (lstrcmpi(arg, TEXT("install")) == 0) { 106 | return SvcInstall(); 107 | } else if (lstrcmpi(arg, TEXT("uninstall")) == 0) { 108 | return SvcUninstall(); 109 | } 110 | break; 111 | } 112 | } 113 | 114 | DispatchTable[0].lpServiceName = svcname; 115 | DispatchTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION) SvcMain; 116 | DispatchTable[1].lpServiceName = NULL; 117 | DispatchTable[1].lpServiceProc = NULL; 118 | 119 | if (!StartServiceCtrlDispatcher( DispatchTable )) 120 | { 121 | SvcReportEvent(TEXT("StartServiceCtrlDispatcher")); 122 | return EXIT_FAILURE; 123 | } 124 | 125 | return EXIT_SUCCESS; 126 | } 127 | 128 | static int 129 | SvcUninstall() { 130 | SC_HANDLE schSCManager = NULL; 131 | SC_HANDLE schService = NULL; 132 | 133 | schSCManager = OpenSCManager( 134 | NULL, 135 | NULL, 136 | SC_MANAGER_ALL_ACCESS 137 | ); 138 | 139 | if (NULL == schSCManager) { 140 | printf("OpenSCManager failed (%d)\n", (int) GetLastError()); 141 | return EXIT_FAILURE; 142 | } 143 | 144 | schService = OpenService( 145 | schSCManager, 146 | svcname, 147 | SERVICE_ALL_ACCESS 148 | ); 149 | 150 | if (schService == NULL) { 151 | DWORD err = GetLastError(); 152 | switch (err) { 153 | case ERROR_ACCESS_DENIED: 154 | printf("You do not have permission to uninstall this service\n"); 155 | break; 156 | 157 | case ERROR_SERVICE_DOES_NOT_EXIST: 158 | printf("The service does not exist\n"); 159 | break; 160 | 161 | default: 162 | printf("OpenService failed (%d)\n", (int) err); 163 | } 164 | 165 | goto error; 166 | } 167 | 168 | if (DeleteService(schService) == 0) { 169 | printf("DeleteService failed (%d)\n", (int) GetLastError()); 170 | goto error; 171 | } else { 172 | printf("Service uninstalled successfully\n"); 173 | } 174 | 175 | CloseServiceHandle (schService); 176 | CloseServiceHandle (schSCManager); 177 | 178 | return EXIT_SUCCESS; 179 | 180 | error: 181 | if (schService) CloseServiceHandle (schService); 182 | if (schSCManager) CloseServiceHandle (schSCManager); 183 | 184 | return EXIT_FAILURE; 185 | } 186 | 187 | static int 188 | SvcInstall() { 189 | SC_HANDLE schSCManager; 190 | SC_HANDLE schService; 191 | TCHAR szPath[MAX_PATH]; 192 | TCHAR imagePath[MAX_PATH]; 193 | 194 | schSCManager = OpenSCManager( 195 | NULL, 196 | NULL, 197 | SC_MANAGER_ALL_ACCESS 198 | ); 199 | 200 | if (NULL == schSCManager) { 201 | printf("OpenSCManager failed (%d)\n", (int) GetLastError()); 202 | return EXIT_FAILURE; 203 | } 204 | 205 | /* Get the full path of the current executable in szPath */ 206 | if (GetModuleFileName(NULL, szPath, MAX_PATH) == 0) { 207 | printf("GetModuleFileName failed (%d)\n", (int) GetLastError()); 208 | return EXIT_FAILURE; 209 | } 210 | 211 | /* Construct ImagePath, which is actually a command line in this instance */ 212 | if (SNPRINTF(imagePath, MAX_PATH, TEXT("%s -s %s"), szPath, svcname) >= MAX_PATH) 213 | { 214 | printf("ImagePath exceeded %d characters\n", MAX_PATH); 215 | return EXIT_FAILURE; 216 | } 217 | 218 | schService = CreateService ( 219 | schSCManager, 220 | svcname, 221 | svcname, 222 | SERVICE_ALL_ACCESS, 223 | SERVICE_WIN32_OWN_PROCESS, 224 | SERVICE_AUTO_START, 225 | SERVICE_ERROR_NORMAL, 226 | imagePath, 227 | NULL, 228 | NULL, 229 | NULL, 230 | NULL, 231 | NULL 232 | ); 233 | 234 | if (schService == NULL) { 235 | DWORD err = GetLastError(); 236 | switch (err) { 237 | case ERROR_SERVICE_EXISTS: 238 | printf("A service with this name already exists\n"); 239 | break; 240 | 241 | default: 242 | printf("CreateService failed (%d)\n", (int) err); 243 | } 244 | 245 | CloseServiceHandle (schSCManager); 246 | return EXIT_FAILURE; 247 | } 248 | else { 249 | printf("Service installed successfully\n"); 250 | } 251 | 252 | CloseServiceHandle (schService); 253 | CloseServiceHandle (schSCManager); 254 | 255 | return EXIT_SUCCESS; 256 | } 257 | 258 | VOID 259 | WINAPI 260 | SvcMain ( 261 | DWORD dwArgc, 262 | LPTSTR *lpszArgv 263 | ) { 264 | gSvcStatusHandle = RegisterServiceCtrlHandler ( 265 | svcname, 266 | SvcCtrlHandler 267 | ); 268 | 269 | if (!gSvcStatusHandle) { 270 | SvcReportEvent(TEXT("RegisterServiceCtrlHandler")); 271 | return; 272 | } 273 | 274 | gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; 275 | gSvcStatus.dwServiceSpecificExitCode = 0; 276 | 277 | ReportSvcStatus ( 278 | SERVICE_START_PENDING, 279 | NO_ERROR, 280 | 3000 281 | ); 282 | 283 | SvcInit ( 284 | dwArgc, 285 | lpszArgv 286 | ); 287 | } 288 | 289 | static BOOL 290 | RegistryRead ( 291 | HKEY hHive, 292 | TCHAR *szKeyPath, 293 | TCHAR *szValue, 294 | TCHAR *szData, 295 | DWORD *nSize 296 | ) { 297 | HKEY hKey; 298 | long lSuccess; 299 | 300 | lSuccess = RegOpenKey ( 301 | hHive, 302 | szKeyPath, 303 | &hKey 304 | ); 305 | 306 | if (lSuccess == ERROR_SUCCESS) { 307 | lSuccess = RegQueryValueEx ( 308 | hKey, 309 | szValue, 310 | NULL, 311 | NULL, 312 | (LPBYTE) szData, 313 | nSize 314 | ); 315 | 316 | if (lSuccess == ERROR_SUCCESS) { 317 | return (TRUE); 318 | } 319 | } 320 | 321 | return (FALSE); 322 | } 323 | 324 | static VOID 325 | SvcInit ( 326 | DWORD dwArgc, 327 | LPTSTR *lpszArgv 328 | ) { 329 | DWORD nSize; 330 | BOOL fSuccess; 331 | STARTUPINFO si; 332 | TCHAR szPWD[1024]; 333 | PROCESS_INFORMATION pi; 334 | TCHAR szCmdLine[1024]; 335 | TCHAR szRegistryPath[1024]; 336 | 337 | // TO_DO: Declare and set any required variables. 338 | // Be sure to periodically call ReportSvcStatus() with 339 | // SERVICE_START_PENDING. If initialization fails, call 340 | // ReportSvcStatus with SERVICE_STOPPED. 341 | 342 | // Create an event. The control handler function, SvcCtrlHandler, 343 | // signals this event when it receives the stop control code. 344 | 345 | ghSvcStopEvent = CreateEvent ( 346 | NULL, 347 | TRUE, 348 | FALSE, 349 | NULL 350 | ); 351 | 352 | if (ghSvcStopEvent == NULL) { 353 | ReportSvcStatus ( 354 | SERVICE_STOPPED, 355 | NO_ERROR, 356 | 0 357 | ); 358 | return; 359 | } 360 | 361 | // Report running status when initialization is complete. 362 | ReportSvcStatus ( 363 | SERVICE_RUNNING, 364 | NO_ERROR, 365 | 0 366 | ); 367 | 368 | // TO_DO: Perform work until service stops. 369 | ZeroMemory( &si, sizeof(si) ); 370 | si.cb = sizeof(si); 371 | ZeroMemory( &pi, sizeof(pi) ); 372 | nSize=1024; 373 | 374 | SNPRINTF( 375 | szRegistryPath, 376 | sizeof szRegistryPath, 377 | TEXT("SYSTEM\\CurrentControlSet\\services\\%s\\Parameters"), 378 | svcname 379 | ); 380 | 381 | fSuccess = RegistryRead ( 382 | HKEY_LOCAL_MACHINE, 383 | szRegistryPath, 384 | TEXT("CommandLine"), 385 | szCmdLine, 386 | &nSize 387 | ); 388 | 389 | if (fSuccess) { 390 | fSuccess = RegistryRead ( 391 | HKEY_LOCAL_MACHINE, 392 | szRegistryPath, 393 | TEXT("PWD"), 394 | szPWD, 395 | &nSize 396 | ); 397 | } 398 | 399 | if (fSuccess) { 400 | fSuccess = CreateProcess ( 401 | NULL, 402 | szCmdLine, 403 | NULL, 404 | NULL, 405 | FALSE, 406 | ( 407 | CREATE_NO_WINDOW 408 | ), 409 | NULL, 410 | szPWD, 411 | &si, 412 | &pi 413 | ); 414 | } 415 | 416 | // treat errors 417 | 418 | while(1) 419 | { 420 | // Check whether to stop the service. 421 | WaitForSingleObject ( 422 | ghSvcStopEvent, 423 | INFINITE 424 | ); 425 | 426 | ReportSvcStatus ( 427 | SERVICE_STOPPED, 428 | NO_ERROR, 429 | 0 430 | ); 431 | 432 | return; 433 | } 434 | } 435 | 436 | static VOID 437 | ReportSvcStatus ( 438 | DWORD dwCurrentState, 439 | DWORD dwWin32ExitCode, 440 | DWORD dwWaitHint 441 | ) { 442 | static DWORD dwCheckPoint = 1; 443 | 444 | // Fill in the SERVICE_STATUS structure. 445 | gSvcStatus.dwCurrentState = dwCurrentState; 446 | gSvcStatus.dwWin32ExitCode = dwWin32ExitCode; 447 | gSvcStatus.dwWaitHint = dwWaitHint; 448 | 449 | if (dwCurrentState == SERVICE_START_PENDING) { 450 | gSvcStatus.dwControlsAccepted = 0; 451 | } 452 | else { 453 | gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; 454 | } 455 | 456 | if ( 457 | (dwCurrentState == SERVICE_RUNNING) || 458 | (dwCurrentState == SERVICE_STOPPED) 459 | ) { 460 | gSvcStatus.dwCheckPoint = 0; 461 | } else { 462 | gSvcStatus.dwCheckPoint = dwCheckPoint++; 463 | } 464 | 465 | // Report the status of the service to the SCM. 466 | SetServiceStatus ( 467 | gSvcStatusHandle, 468 | &gSvcStatus 469 | ); 470 | } 471 | 472 | VOID 473 | WINAPI 474 | SvcCtrlHandler ( 475 | DWORD dwCtrl 476 | ) { 477 | switch(dwCtrl) { 478 | case SERVICE_CONTROL_STOP: 479 | ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0); 480 | 481 | /* Signal the service to stop. */ 482 | SetEvent(ghSvcStopEvent); 483 | 484 | return; 485 | 486 | case SERVICE_CONTROL_INTERROGATE: 487 | /* Fall through to send current status. */ 488 | break; 489 | 490 | default: 491 | break; 492 | } 493 | 494 | ReportSvcStatus(gSvcStatus.dwCurrentState, NO_ERROR, 0); 495 | } 496 | 497 | /* Logs messages to the event log */ 498 | static VOID 499 | SvcReportEvent ( 500 | LPTSTR szFunction 501 | ) { 502 | TCHAR Buffer[80]; 503 | HANDLE hEventSource; 504 | LPCTSTR lpszStrings[2]; 505 | 506 | hEventSource = RegisterEventSource ( 507 | NULL, 508 | svcname 509 | ); 510 | 511 | if ( 512 | NULL != hEventSource 513 | ) { 514 | SNPRINTF( 515 | Buffer, 516 | 80, 517 | TEXT("%s failed with %d"), 518 | szFunction, 519 | GetLastError() 520 | ); 521 | 522 | lpszStrings[0] = svcname; 523 | lpszStrings[1] = Buffer; 524 | 525 | ReportEvent ( 526 | hEventSource, 527 | EVENTLOG_ERROR_TYPE, 528 | 0, 529 | SVC_ERROR, 530 | NULL, 531 | 2, 532 | 0, 533 | lpszStrings, 534 | NULL 535 | ); 536 | 537 | DeregisterEventSource (hEventSource); 538 | } 539 | } 540 | -------------------------------------------------------------------------------- /RHSrvAny/RHSrvAny.h: -------------------------------------------------------------------------------- 1 | /* RHSrvAny - Turn any Windows program into a Windows service. 2 | * Written by Yuval Kashtan. 3 | * Copyright (C) 2010 Red Hat Inc. 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 | */ 19 | 20 | // The following are message definitions. 21 | // 22 | // Values are 32 bit values laid out as follows: 23 | // 24 | // 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 25 | // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 26 | // +---+-+-+-----------------------+-------------------------------+ 27 | // |Sev|C|R| Facility | Code | 28 | // +---+-+-+-----------------------+-------------------------------+ 29 | // 30 | // where 31 | // 32 | // Sev - is the severity code 33 | // 34 | // 00 - Success 35 | // 01 - Informational 36 | // 10 - Warning 37 | // 11 - Error 38 | // 39 | // C - is the Customer code flag 40 | // 41 | // R - is a reserved bit 42 | // 43 | // Facility - is the facility code 44 | // 45 | // Code - is the facility's status code 46 | // 47 | // 48 | // Define the facility codes 49 | // 50 | #define FACILITY_SYSTEM 0x0 51 | #define FACILITY_STUBS 0x3 52 | #define FACILITY_RUNTIME 0x2 53 | #define FACILITY_IO_ERROR_CODE 0x4 54 | 55 | 56 | // 57 | // Define the severity codes 58 | // 59 | #define STATUS_SEVERITY_WARNING 0x2 60 | #define STATUS_SEVERITY_SUCCESS 0x0 61 | #define STATUS_SEVERITY_INFORMATIONAL 0x1 62 | #define STATUS_SEVERITY_ERROR 0x3 63 | 64 | 65 | // 66 | // MessageId: SVC_ERROR 67 | // 68 | // MessageText: 69 | // 70 | // An error has occurred (%2). 71 | // 72 | #define SVC_ERROR ((DWORD)0xC0020001L) 73 | 74 | // A message file must end with a period on its own line 75 | // followed by a blank line. 76 | -------------------------------------------------------------------------------- /RHSrvAny/RHSrvAny.mc: -------------------------------------------------------------------------------- 1 | MessageIdTypedef=DWORD 2 | 3 | SeverityNames= ( 4 | Success=0x0:STATUS_SEVERITY_SUCCESS 5 | Informational=0x1:STATUS_SEVERITY_INFORMATIONAL 6 | Warning=0x2:STATUS_SEVERITY_WARNING 7 | Error=0x3:STATUS_SEVERITY_ERROR 8 | ) 9 | 10 | 11 | FacilityNames= ( 12 | System=0x0:FACILITY_SYSTEM 13 | Runtime=0x2:FACILITY_RUNTIME 14 | Stubs=0x3:FACILITY_STUBS 15 | Io=0x4:FACILITY_IO_ERROR_CODE 16 | ) 17 | 18 | LanguageNames=(English=0x409:MSG00409) 19 | 20 | ; // The following are message definitions. 21 | 22 | MessageId=0x1 23 | Severity=Error 24 | Facility=Runtime 25 | SymbolicName=SVC_ERROR 26 | Language=English 27 | An error has occurred (%2). 28 | . 29 | 30 | ; // A message file must end with a period on its own line 31 | ; // followed by a blank line. 32 | 33 | 34 | -------------------------------------------------------------------------------- /RHSrvAny/RHSrvAny.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 26 | 30 | 33 | 36 | 39 | 42 | 53 | 56 | 59 | 62 | 69 | 72 | 75 | 78 | 81 | 84 | 87 | 90 | 91 | 99 | 103 | 106 | 109 | 112 | 115 | 126 | 129 | 132 | 135 | 144 | 147 | 150 | 153 | 156 | 159 | 162 | 165 | 166 | 167 | 168 | 169 | 170 | 175 | 178 | 179 | 182 | 183 | 184 | 189 | 192 | 193 | 196 | 197 | 198 | 203 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | -------------------------------------------------------------------------------- /RHSrvAny/RHSrvAnyInfo.rc: -------------------------------------------------------------------------------- 1 | // Microsoft Visual C++ generated resource script. 2 | // 3 | #include "resource.h" 4 | 5 | #define APSTUDIO_READONLY_SYMBOLS 6 | ///////////////////////////////////////////////////////////////////////////// 7 | // 8 | // Generated from the TEXTINCLUDE 2 resource. 9 | // 10 | #include "afxres.h" 11 | 12 | ///////////////////////////////////////////////////////////////////////////// 13 | #undef APSTUDIO_READONLY_SYMBOLS 14 | 15 | ///////////////////////////////////////////////////////////////////////////// 16 | // English (U.S.) resources 17 | 18 | #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) 19 | #ifdef _WIN32 20 | LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US 21 | #pragma code_page(1252) 22 | #endif //_WIN32 23 | 24 | #ifdef APSTUDIO_INVOKED 25 | ///////////////////////////////////////////////////////////////////////////// 26 | // 27 | // TEXTINCLUDE 28 | // 29 | 30 | 1 TEXTINCLUDE 31 | BEGIN 32 | "resource.h\0" 33 | END 34 | 35 | 2 TEXTINCLUDE 36 | BEGIN 37 | "#include ""afxres.h""\r\n" 38 | "\0" 39 | END 40 | 41 | 3 TEXTINCLUDE 42 | BEGIN 43 | "\r\n" 44 | "\0" 45 | END 46 | 47 | #endif // APSTUDIO_INVOKED 48 | 49 | 50 | ///////////////////////////////////////////////////////////////////////////// 51 | // 52 | // Version 53 | // 54 | 55 | VS_VERSION_INFO VERSIONINFO 56 | FILEVERSION 1,1,0,0 57 | PRODUCTVERSION 1,1,0,0 58 | FILEFLAGSMASK 0x17L 59 | #ifdef _DEBUG 60 | FILEFLAGS 0x1L 61 | #else 62 | FILEFLAGS 0x0L 63 | #endif 64 | FILEOS 0x4L 65 | FILETYPE 0x2L 66 | FILESUBTYPE 0x0L 67 | BEGIN 68 | BLOCK "StringFileInfo" 69 | BEGIN 70 | BLOCK "040904b0" 71 | BEGIN 72 | VALUE "CompanyName", "Red Hat Inc." 73 | VALUE "FileDescription", "Red Hat Service Application" 74 | VALUE "FileVersion", "1.1" 75 | VALUE "InternalName", "RHSrvAny" 76 | VALUE "LegalCopyright", "Copyright (C) 2010 Red Hat Inc." 77 | VALUE "OriginalFilename", "RHSrvAny.exe" 78 | VALUE "ProductName", "Red Hat SrvAny" 79 | VALUE "ProductVersion", "1.1" 80 | END 81 | END 82 | BLOCK "VarFileInfo" 83 | BEGIN 84 | VALUE "Translation", 0x409, 1200 85 | END 86 | END 87 | 88 | #endif // English (U.S.) resources 89 | ///////////////////////////////////////////////////////////////////////////// 90 | 91 | 92 | 93 | #ifndef APSTUDIO_INVOKED 94 | ///////////////////////////////////////////////////////////////////////////// 95 | // 96 | // Generated from the TEXTINCLUDE 3 resource. 97 | // 98 | 99 | 100 | ///////////////////////////////////////////////////////////////////////////// 101 | #endif // not APSTUDIO_INVOKED 102 | 103 | -------------------------------------------------------------------------------- /RHSrvAny/mc.bat: -------------------------------------------------------------------------------- 1 | rem RHSrvAny - Turn any Windows program into a Windows service. 2 | rem Written by Yuval Kashtan. 3 | rem Copyright (C) 2010 Red Hat Inc. 4 | rem 5 | rem This program is free software; you can redistribute it and/or modify 6 | rem it under the terms of the GNU General Public License as published by 7 | rem the Free Software Foundation; either version 2 of the License, or 8 | rem (at your option) any later version. 9 | rem 10 | rem This program is distributed in the hope that it will be useful, 11 | rem but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | rem MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | rem GNU General Public License for more details. 14 | rem 15 | rem You should have received a copy of the GNU General Public License 16 | rem along with this program; if not, write to the Free Software 17 | rem Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 | 19 | mc.exe -U RHSrvAny.mc 20 | rc.exe -r RHSrvAny.rc 21 | link /dll /noentry /MACHINE:x86 /out:RHSrvAny.dll RHSrvAny.res -------------------------------------------------------------------------------- /RHSrvAny/resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by RHSrvAnyInfo.rc 4 | // 5 | 6 | // Next default values for new objects 7 | // 8 | #ifdef APSTUDIO_INVOKED 9 | #ifndef APSTUDIO_READONLY_SYMBOLS 10 | #define _APS_NEXT_RESOURCE_VALUE 101 11 | #define _APS_NEXT_COMMAND_VALUE 40001 12 | #define _APS_NEXT_CONTROL_VALUE 1001 13 | #define _APS_NEXT_SYMED_VALUE 101 14 | #endif 15 | #endif 16 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # RHSrvAny - Turn any Windows program into a Windows service. 2 | # Written by Yuval Kashtan. 3 | # Copyright (C) 2010 Red Hat Inc. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 2 of the License, or 8 | # (at your option) any later version. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program; if not, write to the Free Software 17 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 | 19 | # Note that this program only makes sense when cross-compiled as a 20 | # Windows program. Therefore you probably need to use something like 21 | # the Fedora Windows cross-compiler and invoke 'mingw32-configure' 22 | # instead of the normal './configure' command. 23 | 24 | # See also RHSrvAny/RHSrvAnyInfo.rc 25 | AC_INIT([RHSrvAny],[1.1]) 26 | AM_INIT_AUTOMAKE([foreign]) 27 | 28 | AC_PROG_CC_STDC 29 | AC_PROG_INSTALL 30 | AC_PROG_CPP 31 | 32 | dnl If the host is Windows, define _WIN32_WINNT and others. 33 | AC_CANONICAL_HOST 34 | case $host in 35 | *mingw*|*cygwin*|*win32*) 36 | CFLAGS="$CFLAGS -Wall -Werror -D_WIN32_WINNT=0x0500 -DUNICODE -D_UNICODE" ;; 37 | esac 38 | 39 | dnl Produce output files. 40 | AC_CONFIG_HEADERS([config.h]) 41 | AC_CONFIG_FILES([Makefile RHSrvAny/Makefile pnp_wait/Makefile]) 42 | AC_OUTPUT 43 | -------------------------------------------------------------------------------- /pnp_wait/Makefile.am: -------------------------------------------------------------------------------- 1 | # pnp_wait - wait for PnP installation activities to complete. 2 | # Author: Roman Kagan 3 | # Copyright (C) 2016 Parallels IP Holdings GmbH. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 2 of the License, or 8 | # (at your option) any later version. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program; if not, write to the Free Software 17 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 | 19 | bin_PROGRAMS = pnp_wait$(EXEEXT) 20 | 21 | pnp_wait_SOURCES = pnp_wait.c 22 | pnp_wait_LDADD = -lcfgmgr32 23 | -------------------------------------------------------------------------------- /pnp_wait/pnp_wait.c: -------------------------------------------------------------------------------- 1 | /* pnp_wait - wait for PnP installation activities to complete. 2 | * Author: Roman Kagan 3 | * Copyright (C) 2016 Parallels IP Holdings GmbH. 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #define RET_SUCCESS 0 29 | #define RET_TIMEOUT 1 30 | #define RET_ERROR 2 31 | 32 | static const char *waitres2str(int res) 33 | { 34 | switch (res) { 35 | case WAIT_OBJECT_0: 36 | return "done"; 37 | case WAIT_TIMEOUT: 38 | return "timed out"; 39 | default: 40 | return "error"; 41 | } 42 | } 43 | 44 | static int waitres2retcode(int res) 45 | { 46 | switch (res) { 47 | case WAIT_OBJECT_0: 48 | return RET_SUCCESS; 49 | case WAIT_TIMEOUT: 50 | return RET_TIMEOUT; 51 | default: 52 | return RET_ERROR; 53 | } 54 | } 55 | 56 | void usage(FILE *fp, const char *cmd) 57 | { 58 | fprintf(fp, 59 | "Usage: %s [-h|--help] [TIMEOUT]\n" 60 | "Wait for PnP activities to complete\n" 61 | "\n" 62 | " -h,--help show this message\n" 63 | " TIMEOUT timeout in ms (default: wait forever)\n" 64 | "\n" 65 | "exit code:\n" 66 | " %d %s\n" 67 | " %d %s\n" 68 | " %d %s\n", 69 | cmd, 70 | waitres2retcode(WAIT_OBJECT_0), waitres2str(WAIT_OBJECT_0), 71 | waitres2retcode(WAIT_TIMEOUT), waitres2str(WAIT_TIMEOUT), 72 | waitres2retcode(WAIT_FAILED), waitres2str(WAIT_FAILED)); 73 | } 74 | 75 | int main(int argc, char **argv) 76 | { 77 | int ret, i; 78 | time_t t; 79 | unsigned tmo = INFINITE; 80 | const char *prog = argv[0]; 81 | 82 | for (i = 1; i < argc; i++) { 83 | const char *s = argv[i]; 84 | char *e; 85 | 86 | if (!strcmp(s, "-h") || !strcmp(s, "--help")) { 87 | usage(stdout, prog); 88 | return RET_SUCCESS; 89 | } 90 | 91 | tmo = strtoul(s, &e, 0); 92 | if (*e == '\0' && e != s) 93 | continue; 94 | 95 | fprintf(stderr, "failed to parse parameter: \"%s\"\n", s); 96 | usage(stderr, prog); 97 | return RET_ERROR; 98 | } 99 | 100 | t = time(NULL); 101 | printf("start waiting for PnP to complete @ %s", ctime(&t)); 102 | 103 | ret = CMP_WaitNoPendingInstallEvents(tmo); 104 | 105 | t = time(NULL); 106 | printf("%s waiting for PnP to complete @ %s", waitres2str(ret), 107 | ctime(&t)); 108 | return waitres2retcode(ret); 109 | } 110 | --------------------------------------------------------------------------------