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

 

Introduction

Documentation

Support

Miscellaneous