*/
#include "DSUtilSocket.h"
+#include <malloc.h>
+#include <sys/xattr.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <grp.h>
namespace display_server
{
}
}
+void DSUtilSocket::initSocket(std::string socketName)
+{
+ char *dir = nullptr;
+ int res;
+#undef STRERR_BUFSIZE
+#define STRERR_BUFSIZE 1024
+ char buf[STRERR_BUFSIZE];
+
+ if (socketName.empty()) return;
+
+ dir = getenv("XDG_RUNTIME_DIR");
+ if (!dir) return;
+
+ std::string xdgDir(dir);
+
+ /* check whether buffer size is less than concatenated string which
+ * is made of XDG_RUNTIME_DIR, '/', socket name and NULL.
+ */
+
+ int dirLength = xdgDir.length();
+ int nameLength = socketName.length();
+ if ((dirLength + nameLength + 2) > STRERR_BUFSIZE)
+ {
+ DSLOG_WRN("DSUtilSocket", "Size of buffer is not enough. dir:%s name:%s", xdgDir, socketName);
+ return;
+ }
+
+ //snprintf(socket_path, sizeof(socket_path), "%s/%s", dir, name);
+ std::string socketPath = xdgDir + "/" + socketName;
+
+ uid_t uid = __getpwnam("root");
+ gid_t gid = __getgrnam("display");
+
+ res = chmod(socketPath.c_str(), 509);
+ if (res < 0)
+ {
+ DSLOG_WRN("DSUtilSocket", "Could not change modes of socket file:%s (%s)", socketPath.c_str(), strerror_r(errno, buf, STRERR_BUFSIZE));
+ //PRCTL("[Winsys] Could not chane modes of socket file: %s", socketPath.c_str());
+ return;
+ }
+
+ res = chown(socketPath.c_str(), uid, gid);
+ if (res < 0)
+ {
+ DSLOG_WRN("DSUtilSocket", "Could not change owner of socket file:%s (%s)", socketPath.c_str(), strerror_r(errno, buf, STRERR_BUFSIZE));
+ //PRCTL("[Winsys] Could not change owner of socket file: %s", socketPath.c_str());
+ return;
+ }
+}
+
+int DSUtilSocket::__getpwnam(std::string name)
+{
+ struct ::passwd *u;
+ struct ::passwd *uRes;
+ char* buf;
+ size_t bufLen;
+ int ret;
+#undef BUFLEN
+#define BUFLEN 2048
+ bufLen = sysconf(_SC_GETPW_R_SIZE_MAX);
+ if ((int)bufLen == -1) /* Value was indeterminate */
+ bufLen = BUFLEN; /* Should be more than enough */
+#undef BUFLEN
+
+ buf = (char *)malloc(bufLen);
+ if (buf == NULL)
+ {
+ DSLOG_WRN("DSUtilSocket", "failed to create buffer");
+ return 0;
+ }
+
+ u = (struct ::passwd *)malloc(sizeof(struct passwd));
+ if (!u)
+ {
+ DSLOG_WRN("DSUtilSocket", "failed to create password struct");
+ free(buf);
+ return 0;
+ }
+
+ ret = getpwnam_r(name.c_str(), u, buf, bufLen, &uRes);
+ if (uRes == nullptr)
+ {
+ if (ret == 0) DSLOG_WRN("DSUtilSocket", "password not found");
+ else DSLOG_WRN("DSUtilSocket", "errno returned by getpwnam_r is %d", ret);
+ free(buf);
+ free(u);
+ return 0;
+ }
+ ret = u->pw_uid;
+ free(buf);
+ free(u);
+
+ return ret;
+}
+
+int DSUtilSocket::__getgrnam(std::string name)
+{
+ struct ::group *g;
+ struct ::group *grpRes;
+ char* buf;
+ size_t bufLen;
+ int ret;
+#undef BUFLEN
+#define BUFLEN 2048
+ bufLen = sysconf(_SC_GETGR_R_SIZE_MAX);
+ if ((int)bufLen == -1) /* Value was indeterminate */
+ bufLen = BUFLEN; /* Should be more than enough */
+#undef BUFLEN
+
+ buf = (char *)malloc(bufLen);
+ if (buf == nullptr)
+ {
+ DSLOG_WRN("DSUtilSocket", "failed to create buffer");
+ return 0;
+ }
+
+ g = (struct ::group *)malloc(sizeof(struct group));
+ if (!g)
+ {
+ DSLOG_WRN("DSUtilSocket", "failed to create group struct");
+ free(buf);
+ return 0;
+ }
+
+ ret = getgrnam_r(name.c_str(), g, buf, bufLen, &grpRes);
+ if (grpRes == NULL)
+ {
+ if (ret == 0) DSLOG_WRN("DSUtilSocket", "Group not found");
+ else DSLOG_WRN("DSUtilSocket", "errno returned by getpwnam_r is %d", ret);
+ free(buf);
+ free(g);
+ return 0;
+ }
+
+ ret = g->gr_gid;
+ free(buf);
+ free(g);
+ return ret;
+}
+
+
}