mswindows.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* Windows support
10  * Inspired by previous work by Romeo Anghelache & Eric Stern. */
11 
12 #include "squid.h"
13 
14 // The following code section is part of an EXPERIMENTAL native Windows NT/2000 Squid port.
15 // Compiles only on MS Visual C++
16 // CygWin appears not to need any of these
17 #if _SQUID_WINDOWS_ && !_SQUID_CYGWIN_
18 
19 #define sys_nerr _sys_nerr
20 
21 #undef assert
22 #include <cassert>
23 #include <cstring>
24 #include <fcntl.h>
25 #include <sys/timeb.h>
26 #if HAVE_PSAPI_H
27 #include <psapi.h>
28 #endif
29 #ifndef _MSWSOCK_
30 #include <mswsock.h>
31 #endif
32 
33 THREADLOCAL int ws32_result;
34 LPCRITICAL_SECTION dbg_mutex = nullptr;
35 
36 void GetProcessName(pid_t, char *);
37 
38 #if HAVE_GETPAGESIZE > 1
39 size_t
40 getpagesize()
41 {
42  static DWORD system_pagesize = 0;
43  if (!system_pagesize) {
44  SYSTEM_INFO system_info;
45  GetSystemInfo(&system_info);
46  system_pagesize = system_info.dwPageSize;
47  }
48  return system_pagesize;
49 }
50 #endif /* HAVE_GETPAGESIZE > 1 */
51 
52 int
53 chroot(const char *dirname)
54 {
55  if (SetCurrentDirectory(dirname))
56  return 0;
57  else
58  return GetLastError();
59 }
60 
61 void
62 GetProcessName(pid_t pid, char *ProcessName)
63 {
64  strcpy(ProcessName, "unknown");
65 #if defined(PSAPI_VERSION)
66  /* Get a handle to the process. */
67  HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
68  /* Get the process name. */
69  if (hProcess) {
70  HMODULE hMod;
71  DWORD cbNeeded;
72 
73  if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
74  GetModuleBaseName(hProcess, hMod, ProcessName, sizeof(ProcessName));
75  else {
76  CloseHandle(hProcess);
77  return;
78  }
79  } else
80  return;
81  CloseHandle(hProcess);
82 #endif
83 }
84 
85 int
86 kill(pid_t pid, int sig)
87 {
88  HANDLE hProcess;
89  char MyProcessName[MAX_PATH];
90  char ProcessNameToCheck[MAX_PATH];
91 
92  if (sig == 0) {
93  if (!(hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid)))
94  return -1;
95  else {
96  CloseHandle(hProcess);
97  GetProcessName(getpid(), MyProcessName);
98  GetProcessName(pid, ProcessNameToCheck);
99  if (strcmp(MyProcessName, ProcessNameToCheck) == 0)
100  return 0;
101  return -1;
102  }
103  } else
104  return 0;
105 }
106 
107 #if !HAVE_GETTIMEOFDAY
108 int
109 gettimeofday(struct timeval *pcur_time, void *tzp)
110 {
111  struct _timeb current;
112  struct timezone *tz = (struct timezone *) tzp;
113 
114  _ftime(&current);
115 
116  pcur_time->tv_sec = current.time;
117  pcur_time->tv_usec = current.millitm * 1000L;
118  if (tz) {
119  tz->tz_minuteswest = current.timezone; /* minutes west of Greenwich */
120  tz->tz_dsttime = current.dstflag; /* type of dst correction */
121  }
122  return 0;
123 }
124 #endif /* !HAVE_GETTIMEOFDAY */
125 
126 int
127 WIN32_ftruncate(int fd, off_t size)
128 {
129  HANDLE hfile;
130  unsigned int curpos;
131 
132  if (fd < 0)
133  return -1;
134 
135  hfile = (HANDLE) _get_osfhandle(fd);
136  curpos = SetFilePointer(hfile, 0, nullptr, FILE_CURRENT);
137  if (curpos == 0xFFFFFFFF
138  || SetFilePointer(hfile, size, nullptr, FILE_BEGIN) == 0xFFFFFFFF
139  || !SetEndOfFile(hfile)) {
140  int error = GetLastError();
141 
142  switch (error) {
143  case ERROR_INVALID_HANDLE:
144  errno = EBADF;
145  break;
146  default:
147  errno = EIO;
148  break;
149  }
150 
151  return -1;
152  }
153  return 0;
154 }
155 
156 int
157 WIN32_truncate(const char *pathname, off_t length)
158 {
159  int fd;
160  int res = -1;
161 
162  fd = open(pathname, O_RDWR);
163 
164  if (fd == -1)
165  errno = EBADF;
166  else {
167  res = WIN32_ftruncate(fd, length);
168  _close(fd);
169  }
170 
171  return res;
172 }
173 
174 struct passwd *
175 getpwnam(char *unused) {
176  static struct passwd pwd = {nullptr, nullptr, 100, 100, nullptr, nullptr, nullptr};
177  return &pwd;
178 }
179 
180 struct group *
181 getgrnam(char *unused) {
182  static struct group grp = {nullptr, nullptr, 100, nullptr};
183  return &grp;
184 }
185 
186 /* syslog emulation layer derived from git */
187 static HANDLE ms_eventlog;
188 
189 void
190 openlog(const char *ident, int logopt, int facility)
191 {
192  if (ms_eventlog)
193  return;
194 
195  ms_eventlog = RegisterEventSourceA(nullptr, ident);
196 
197  // note: RegisterEventAtSourceA may fail and return nullptr.
198  // in that case we'll just retry at the next message or not log
199 }
200 #define SYSLOG_MAX_MSG_SIZE 1024
201 
202 void
203 syslog(int priority, const char *fmt, ...)
204 {
205  WORD logtype;
206  char *str=static_cast<char *>(xmalloc(SYSLOG_MAX_MSG_SIZE));
207  int str_len;
208  va_list ap;
209 
210  if (!ms_eventlog)
211  return;
212 
213  va_start(ap, fmt);
214  str_len = vsnprintf(str, SYSLOG_MAX_MSG_SIZE-1, fmt, ap);
215  va_end(ap);
216 
217  if (str_len < 0) {
218  /* vsnprintf failed */
219  return;
220  }
221 
222  switch (priority) {
223  case LOG_EMERG:
224  case LOG_ALERT:
225  case LOG_CRIT:
226  case LOG_ERR:
227  logtype = EVENTLOG_ERROR_TYPE;
228  break;
229 
230  case LOG_WARNING:
231  logtype = EVENTLOG_WARNING_TYPE;
232  break;
233 
234  case LOG_NOTICE:
235  case LOG_INFO:
236  case LOG_DEBUG:
237  default:
238  logtype = EVENTLOG_INFORMATION_TYPE;
239  break;
240  }
241 
242  //Windows API suck. They are overengineered
243  ReportEventA(ms_eventlog, logtype, 0, 0, nullptr, 1, 0,
244  const_cast<const char **>(&str), nullptr);
245 }
246 
247 /* note: this is all MSWindows-specific code; all of it should be conditional */
248 #endif /* _SQUID_WINDOWS_ && !_SQUID_CYGWIN_*/
#define xmalloc
#define FALSE
Definition: std-includes.h:56
SQUIDCEXTERN LPCRITICAL_SECTION dbg_mutex
void error(char *format,...)
unsigned short WORD
Definition: smblib-priv.h:145
static pid_t pid
Definition: IcmpSquid.cc:34
int size
Definition: ModDevPoll.cc:69

 

Introduction

Documentation

Support

Miscellaneous