-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
many: cleanup, revert the -1 support in services list for client
- Loading branch information
1 parent
ccc770c
commit abe095c
Showing
10 changed files
with
332 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2024 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package clientutil | ||
|
||
import ( | ||
"path/filepath" | ||
"strconv" | ||
|
||
"github.com/snapcore/snapd/dirs" | ||
) | ||
|
||
// AvailableUserSessions returns a list of available user-session targets for | ||
// snapd, by probing the available snapd-session-agent sockets in the | ||
// XDG runtime directory. | ||
func AvailableUserSessions() ([]int, error) { | ||
sockets, err := filepath.Glob(filepath.Join(dirs.XdgRuntimeDirGlob, "snapd-session-agent.socket")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var uids []int | ||
for _, sock := range sockets { | ||
uidStr := filepath.Base(filepath.Dir(sock)) | ||
uid, err := strconv.Atoi(uidStr) | ||
if err != nil { | ||
// Ignore directories that do not | ||
// appear to be valid XDG runtime dirs | ||
// (i.e. /run/user/NNNN). | ||
continue | ||
} | ||
uids = append(uids, uid) | ||
} | ||
return uids, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2024 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package clientutil_test | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"sort" | ||
|
||
"github.com/snapcore/snapd/client/clientutil" | ||
"github.com/snapcore/snapd/dirs" | ||
. "gopkg.in/check.v1" | ||
) | ||
|
||
type sessionSuite struct{} | ||
|
||
var _ = Suite(&sessionSuite{}) | ||
|
||
func (s *sessionSuite) SetUpTest(c *C) { | ||
dirs.SetRootDir(c.MkDir()) | ||
} | ||
|
||
func (s *sessionSuite) TestAvailableUserSessionsHappy(c *C) { | ||
// fake two sockets, one for 0 and one for 1000 | ||
err := os.MkdirAll(path.Join(dirs.XdgRuntimeDirBase, "0", "snapd-session-agent.socket"), 0700) | ||
c.Assert(err, IsNil) | ||
err = os.MkdirAll(path.Join(dirs.XdgRuntimeDirBase, "1000", "snapd-session-agent.socket"), 0700) | ||
c.Assert(err, IsNil) | ||
err = os.MkdirAll(path.Join(dirs.XdgRuntimeDirBase, "1337", "snapd-session-agent.socket"), 0700) | ||
c.Assert(err, IsNil) | ||
|
||
res, err := clientutil.AvailableUserSessions() | ||
c.Assert(err, IsNil) | ||
c.Check(res, HasLen, 3) | ||
sort.Ints(res) | ||
c.Check(res, DeepEquals, []int{ | ||
0, | ||
1000, | ||
1337, | ||
}) | ||
} | ||
|
||
func (s *sessionSuite) TestAvailableUserSessionsIgnoresBadUids(c *C) { | ||
// fake two sockets, one for 0 and one for 1000 | ||
err := os.MkdirAll(path.Join(dirs.XdgRuntimeDirBase, "hello", "snapd-session-agent.socket"), 0700) | ||
c.Assert(err, IsNil) | ||
err = os.MkdirAll(path.Join(dirs.XdgRuntimeDirBase, "*34i8932", "snapd-session-agent.socket"), 0700) | ||
c.Assert(err, IsNil) | ||
err = os.MkdirAll(path.Join(dirs.XdgRuntimeDirBase, "3", "snapd-session-agent.socket"), 0700) | ||
c.Assert(err, IsNil) | ||
|
||
res, err := clientutil.AvailableUserSessions() | ||
c.Assert(err, IsNil) | ||
c.Check(res, HasLen, 1) | ||
sort.Ints(res) | ||
c.Check(res, DeepEquals, []int{ | ||
3, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.