Make new_error_socket() into a printf-style function.

Almost all the call sites were doing a cumbersome dupprintf-use-free
cycle to get a formatted message into an ErrorSocket anyway, so it
seems more sensible to give them an easier way of doing so.

The few call sites that were passing a constant string literal
_shouldn't_ have been - they'll be all the better for adding a
strerror suffix to the message they were previously giving!
This commit is contained in:
Simon Tatham 2018-10-07 14:47:16 +01:00
Родитель 55860cace7
Коммит cea1329b9e
5 изменённых файлов: 38 добавлений и 37 удалений

2
Recipe
Просмотреть файл

@ -270,7 +270,7 @@ MISC = misc marshal
MISCNET = timing callback MISC version settings tree234 proxy CONF be_misc
WINMISC = MISCNET winstore winnet winhandl cmdline windefs winmisc winproxy
+ wintime winhsock errsock winsecur winucs miscucs
UXMISC = MISCNET uxstore uxsel uxnet uxpeer uxmisc uxproxy time
UXMISC = MISCNET uxstore uxsel uxnet uxpeer uxmisc uxproxy errsock time
# import.c and dependencies, for PuTTYgen-like utilities that have to
# load foreign key files.

Просмотреть файл

@ -56,11 +56,23 @@ static const SocketVtable ErrorSocket_sockvt = {
sk_error_peer_info,
};
Socket *new_error_socket(const char *errmsg, Plug *plug)
static Socket *new_error_socket_internal(char *errmsg, Plug *plug)
{
ErrorSocket *es = snew(ErrorSocket);
es->sock.vt = &ErrorSocket_sockvt;
es->plug = plug;
es->error = dupstr(errmsg);
es->error = errmsg;
return &es->sock;
}
Socket *new_error_socket_fmt(Plug *plug, const char *fmt, ...)
{
va_list ap;
char *msg;
va_start(ap, fmt);
msg = dupvprintf(fmt, ap);
va_end(ap);
return new_error_socket_internal(msg, plug);
}

Просмотреть файл

@ -220,7 +220,7 @@ char *get_hostname(void);
* Trivial socket implementation which just stores an error. Found in
* errsock.c.
*/
Socket *new_error_socket(const char *errmsg, Plug *plug);
Socket *new_error_socket_fmt(Plug *plug, const char *fmt, ...);
/*
* Trivial plug that does absolutely nothing. Found in nullplug.c.

Просмотреть файл

@ -23,7 +23,6 @@ Socket *new_named_pipe_client(const char *pipename, Plug *plug)
HANDLE pipehandle;
PSID usersid, pipeowner;
PSECURITY_DESCRIPTOR psd;
char *err;
Socket *ret;
assert(strncmp(pipename, "\\\\.\\pipe\\", 9) == 0);
@ -38,11 +37,9 @@ Socket *new_named_pipe_client(const char *pipename, Plug *plug)
break;
if (GetLastError() != ERROR_PIPE_BUSY) {
err = dupprintf("Unable to open named pipe '%s': %s",
pipename, win_strerror(GetLastError()));
ret = new_error_socket(err, plug);
sfree(err);
return ret;
return new_error_socket_fmt(
plug, "Unable to open named pipe '%s': %s",
pipename, win_strerror(GetLastError()));
}
/*
@ -53,41 +50,33 @@ Socket *new_named_pipe_client(const char *pipename, Plug *plug)
* take excessively long.)
*/
if (!WaitNamedPipe(pipename, NMPWAIT_USE_DEFAULT_WAIT)) {
err = dupprintf("Error waiting for named pipe '%s': %s",
pipename, win_strerror(GetLastError()));
ret = new_error_socket(err, plug);
sfree(err);
return ret;
return new_error_socket_fmt(
plug, "Error waiting for named pipe '%s': %s",
pipename, win_strerror(GetLastError()));
}
}
if ((usersid = get_user_sid()) == NULL) {
CloseHandle(pipehandle);
err = dupprintf("Unable to get user SID");
ret = new_error_socket(err, plug);
sfree(err);
return ret;
return new_error_socket_fmt(
plug, "Unable to get user SID: %s", win_strerror(GetLastError()));
}
if (p_GetSecurityInfo(pipehandle, SE_KERNEL_OBJECT,
OWNER_SECURITY_INFORMATION,
&pipeowner, NULL, NULL, NULL,
&psd) != ERROR_SUCCESS) {
err = dupprintf("Unable to get named pipe security information: %s",
win_strerror(GetLastError()));
ret = new_error_socket(err, plug);
sfree(err);
CloseHandle(pipehandle);
return ret;
return new_error_socket_fmt(
plug, "Unable to get named pipe security information: %s",
win_strerror(GetLastError()));
}
if (!EqualSid(pipeowner, usersid)) {
err = dupprintf("Owner of named pipe '%s' is not us", pipename);
ret = new_error_socket(err, plug);
sfree(err);
CloseHandle(pipehandle);
LocalFree(psd);
return ret;
return new_error_socket_fmt(
plug, "Owner of named pipe '%s' is not us", pipename);
}
LocalFree(psd);

Просмотреть файл

@ -50,30 +50,30 @@ Socket *platform_new_connection(SockAddr *addr, const char *hostname,
sa.lpSecurityDescriptor = NULL; /* default */
sa.bInheritHandle = TRUE;
if (!CreatePipe(&us_from_cmd, &cmd_to_us, &sa, 0)) {
Socket *ret =
new_error_socket("Unable to create pipes for proxy command", plug);
sfree(cmd);
return ret;
return new_error_socket_fmt(
plug, "Unable to create pipes for proxy command: %s",
win_strerror(GetLastError()));
}
if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) {
Socket *ret =
new_error_socket("Unable to create pipes for proxy command", plug);
sfree(cmd);
CloseHandle(us_from_cmd);
CloseHandle(cmd_to_us);
return ret;
return new_error_socket_fmt(
plug, "Unable to create pipes for proxy command: %s",
win_strerror(GetLastError()));
}
if (!CreatePipe(&us_from_cmd_err, &cmd_err_to_us, &sa, 0)) {
Socket *ret = new_error_socket
("Unable to create pipes for proxy command", plug);
sfree(cmd);
CloseHandle(us_from_cmd);
CloseHandle(cmd_to_us);
CloseHandle(us_to_cmd);
CloseHandle(cmd_from_us);
return ret;
return new_error_socket_fmt(
plug, "Unable to create pipes for proxy command: %s",
win_strerror(GetLastError()));
}
SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);