4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2012 Milan Jurik. All rights reserved.
28 * This code has a lot in common with the original sys-suspend
29 * code. Windowing facilities have been removed, and it has been
30 * updated to use more recent API's.
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/utsname.h>
49 #include <sys/uadmin.h>
50 #include <auth_attr.h>
51 #include <auth_list.h>
53 #include <security/pam_appl.h>
58 #include <bsm/adt_event.h>
68 #define NMAX (sizeof (utmp.ut_name))
71 * Authorizations used by Power Management
73 #define AUTHNAME_SHUTDOWN "solaris.system.shutdown"
74 #define AUTHNAME_SUSPEND_RAM "solaris.system.power.suspend.ram"
75 #define AUTHNAME_SUSPEND_DISK "solaris.system.power.suspend.disk"
77 /* Platform specific definitions */
79 #define AD_CHECK_SUSPEND AD_CHECK_SUSPEND_TO_RAM
80 #define AD_SUSPEND AD_SUSPEND_TO_RAM
81 #define ADT_FCN ADT_UADMIN_FCN_AD_SUSPEND_TO_RAM
82 #define AUTHNAME_SUSPEND AUTHNAME_SUSPEND_RAM
84 #define AD_CHECK_SUSPEND AD_CHECK_SUSPEND_TO_DISK
85 #define AD_SUSPEND AD_SUSPEND_TO_DISK
86 #define ADT_FCN ADT_UADMIN_FCN_AD_SUSPEND_TO_DISK
87 #define AUTHNAME_SUSPEND AUTHNAME_SUSPEND_DISK
91 static int no_tty
= 0;
93 * Flag definitions - could go in a header file, but there are just a few
97 #define NO_XLOCK 0x004
98 #define SHUTDOWN 0x008
99 #define LOWPOWER 0x010
102 static sigjmp_buf jmp_stack
;
103 static char user
[NMAX
+ 1];
109 * Forward Declarations.
111 static void pm_poweroff(void);
112 static int bringto_lowpower(void);
113 static int is_mou3(void);
114 static void suspend_error(int);
115 static int pm_check_suspend(void);
116 static void pm_suspend(void);
117 static void pm_do_auth(adt_session_data_t
*);
120 * External Declarations.
122 extern int pam_tty_conv(int, struct pam_message
**,
123 struct pam_response
**, void *);
127 * Audit related code. I would also think that some of this could be
128 * in external code, as they could be useful of other apps.
131 * Write audit event. Could be useful in the PM library, so it is
132 * included here. For the most part it is only used by the PAM code.
135 pm_audit_event(adt_session_data_t
*ah
, au_event_t event_id
, int status
)
137 adt_event_data_t
*event
;
140 if ((event
= adt_alloc_event(ah
, event_id
)) == NULL
) {
144 (void) adt_put_event(event
,
145 status
== PAM_SUCCESS
? ADT_SUCCESS
: ADT_FAILURE
,
146 status
== PAM_SUCCESS
? ADT_SUCCESS
: ADT_FAIL_PAM
+ status
);
148 adt_free_event(event
);
151 #define RETRY_COUNT 15
153 change_audit_file(void)
157 if (!adt_audit_state(AUC_AUDITING
)) {
158 /* auditd not running, just return */
162 if ((pid
= fork()) == 0) {
163 (void) execl("/usr/sbin/audit", "audit", "-n", NULL
);
164 (void) fprintf(stderr
, gettext("error changing audit files: "
165 "%s\n"), strerror(errno
));
167 } else if (pid
== -1) {
168 (void) fprintf(stderr
, gettext("error changing audit files: "
169 "%s\n"), strerror(errno
));
173 int retries
= RETRY_COUNT
;
176 * Wait for audit(1M) -n process to complete
180 if ((rc
= waitpid(pid
, NULL
, WNOHANG
)) == pid
) {
182 } else if (rc
== -1) {
189 } while (retries
!= 0);
200 while (retries
-- && auditon(A_GETSTAT
, (caddr_t
)&au_stat
, NULL
) == 0) {
201 if (au_stat
.as_enqueue
== au_stat
.as_written
) {
208 /* End of Audit-related code */
212 alarm_handler(int sig
)
214 siglongjmp(jmp_stack
, 1);
218 * These are functions that would be candidates for moving to a library.
222 * pm_poweroff - similar to poweroff(1M)
223 * This should do the same auditing as poweroff(1m) would do when it
224 * becomes a libpower function. Till then we use poweroff(1m).
229 if (chkauthattr(AUTHNAME_SHUTDOWN
, user
) != 1) {
230 (void) printf(gettext("User %s does not have correct "
231 "authorizations to shutdown this machine.\n"), user
);
234 openlog("suspend", 0, LOG_DAEMON
);
235 syslog(LOG_NOTICE
, "System is being shut down.");
239 * Call poweroff(1m) to shut down the system.
241 (void) execl("/usr/sbin/poweroff", "poweroff", NULL
);
246 * pm_check_suspend() - Check to see if suspend is supported/enabled
248 * Ultimately, we would prefer to get the "default" suspend type from
249 * a PM property or some other API, but for now, we know that STR is
250 * only available on x86 and STD is only available on Sparc. It does
251 * make this function quite easy, though.
254 pm_check_suspend(void) {
256 * Use the uadmin(2) "CHECK" command to see if suspend is supported
258 return (uadmin(A_FREEZE
, AD_CHECK_SUSPEND
, 0));
262 * This entry point _should_ be the common entry to suspend. It is in
263 * it's entirety here, but would be best moved to libpower when that
269 int cprarg
= AD_SUSPEND
;
270 enum adt_uadmin_fcn fcn_id
= ADT_FCN
;
271 au_event_t event_id
= ADT_uadmin_freeze
;
272 adt_event_data_t
*event
= NULL
; /* event to be generated */
273 adt_session_data_t
*ah
= NULL
; /* audit session handle */
276 * Does the user have permission to use this command?
278 if (chkauthattr(AUTHNAME_SUSPEND
, user
) != 1) {
279 (void) printf(gettext("User %s does not have correct "
280 "authorizations to suspend this machine.\n"), user
);
284 if (flags
& LOWPOWER
) {
285 if (bringto_lowpower() == -1) {
286 (void) printf(gettext("LowPower Failed\n"));
289 } else if (flags
& TEST
) {
291 * Test mode, do checks as if a real suspend, but
292 * don't actually do the suspend.
294 /* Check if suspend is supported */
295 if (pm_check_suspend() == -1) {
296 suspend_error(errno
);
299 (void) printf(gettext("TEST: Suspend would have been"
303 /* Check if suspend is supported */
304 if (pm_check_suspend() == -1) {
305 suspend_error(errno
);
309 * We are about to suspend this machine, try and
310 * lock the screen. We don't really care if this
311 * succeeds or not, but that we actually tried. We
312 * also know that we have sufficient privileges to
313 * be here, so we lock the screen now, even if
314 * suspend actually fails.
315 * Note that garbage is sometimes displayed, and
316 * we don't really care about it, so we toss all
318 * it would also be good if there were another option
319 * instead of launcing a file, as the disk might be
320 * spun down if we are suspending due to idle.
322 if (!(flags
& NO_XLOCK
)) {
323 (void) system("/usr/bin/xdg-screensaver lock "
327 /* Time to do the actual deed! */
329 * Before we actually suspend, we need to audit and
330 * "suspend" the audit files.
332 /* set up audit session and event */
333 if (adt_start_session(&ah
, NULL
, ADT_USE_PROC_DATA
) == 0) {
334 if ((event
= adt_alloc_event(ah
, event_id
)) != NULL
) {
335 event
->adt_uadmin_freeze
.fcn
= fcn_id
;
336 event
->adt_uadmin_freeze
.mdep
= NULL
;
337 if (adt_put_event(event
, ADT_SUCCESS
, 0) != 0) {
338 (void) fprintf(stderr
, gettext(
339 "%s: can't put audit event\n"),
345 (void) change_audit_file();
347 (void) fprintf(stderr
, gettext(
348 "%s: can't start audit session\n"), argvl
[0]);
351 if (uadmin(A_FREEZE
, cprarg
, 0) != 0) {
352 (void) printf(gettext("Suspend Failed\n"));
355 * Note, that if we actually poweroff,
356 * that the poweroff function will handle
357 * that audit trail, and the resume
358 * trail is effectively done.
362 /* suspend_error() will exit. */
363 suspend_error(errno
);
365 * Audit the suspend failure and
366 * reuse the event, but don't create one
367 * if we don't already have one.
370 (void) adt_put_event(event
,
377 * Write the thaw event.
380 if ((event
== NULL
) &&
381 ((event
= adt_alloc_event(ah
, ADT_uadmin_thaw
))
383 (void) fprintf(stderr
, gettext(
384 "%s: can't allocate thaw audit event\n"),
387 event
->adt_uadmin_thaw
.fcn
= fcn_id
;
388 if (adt_put_event(event
, ADT_SUCCESS
, 0) != 0) {
389 (void) fprintf(stderr
, gettext(
390 "%s: can't put thaw audit event\n"),
393 (void) adt_free_event(event
);
397 if ((no_tty
? 0 : 1) && !(flags
& NO_XLOCK
)) {
401 (void) adt_end_session(ah
);
403 /* End of "library" functions */
406 * Print an appropriate error message and exit.
410 suspend_error(int error
) {
414 (void) printf(gettext("suspend: "
415 "Suspend already in progress.\n\n"));
421 (void) printf(gettext("suspend: "
422 "Not enough resources to suspend.\n\n"));
426 (void) printf(gettext("suspend: "
427 "Suspend is not supported.\n\n"));
431 (void) printf(gettext("suspend: "
432 "Not sufficient privileges.\n\n"));
436 (void) printf(gettext("suspend: "
437 "unknown error.\n\n"));
444 * refresh_dt() - Refresh screen when 'dtgreet' is running.
445 * This is here for compatibility reasons, and could be removed once
446 * dtgreet is no longer part of the system.
452 struct stat stat_buf
;
455 * If dtgreet exists, HUP it, otherwise just let screenlock
458 if ((stat("/usr/dt/bin/dtgreet", &stat_buf
) == 0) &&
459 (stat_buf
.st_mode
& S_IXUSR
)) {
465 (void) execl("/usr/bin/pkill", "pkill",
466 "-HUP", "-u", "0", "-x", "dtgreet", NULL
);
469 (void) wait(&status
);
476 #define DT_TMP "/var/dt/tmp"
479 * On enter, the "xauthority" string has the value "XAUTHORITY=". On
480 * return, if a Xauthority file is found, concatenate it to this string,
481 * otherwise, return "xauthority" as it is.
484 get_xauthority(char *xauthority
)
489 char filepath
[MAXPATHLEN
];
490 struct stat stat_buf
;
493 char xauth
[MAXPATHLEN
] = "";
499 * Determine home directory of the user.
501 if ((home_dir
= getenv("HOME")) == NULL
) {
502 if ((pwd
= getpwuid(uid
)) == NULL
) {
503 (void) printf(gettext("Error: unable to get passwd "
504 "entry for user.\n"));
507 home_dir
= pwd
->pw_dir
;
509 if ((strlen(home_dir
) + sizeof ("/.Xauthority")) >= MAXPATHLEN
) {
510 (void) printf(gettext("Error: path to home directory is too "
516 * If there is a .Xauthority file in home directory, reference it.
519 (void) sprintf(filepath
, "%s/.Xauthority", home_dir
);
520 if (stat(filepath
, &stat_buf
) == 0)
521 return (strcat(xauthority
, filepath
));
524 * If Xsession can not access user's home directory, it creates the
525 * Xauthority file in "/var/dt/tmp" directory. Since the exact
526 * name of the Xauthority is not known, search the directory and
527 * find the last changed file that starts with ".Xauth" and owned
528 * by the user. Hopefully, that is the valid Xauthority file for
529 * the current X session.
531 if ((dirp
= opendir(DT_TMP
)) == NULL
)
534 while ((dp
= readdir(dirp
)) != NULL
) {
535 if (strstr(dp
->d_name
, ".Xauth") != NULL
) {
537 (void) sprintf(filepath
, "%s/%s", DT_TMP
, dp
->d_name
);
538 if (stat(filepath
, &stat_buf
) == -1)
540 if (stat_buf
.st_uid
!= uid
)
542 if (stat_buf
.st_ctime
> latest
) {
543 (void) strcpy(xauth
, filepath
);
544 latest
= stat_buf
.st_ctime
;
548 (void) closedir(dirp
);
550 return (strcat(xauthority
, xauth
));
554 * suspend can be called in following ways:
555 * 1. from daemon (powerd) for auto-shutdown.
556 * a. there might be a OW/CDE environment
557 * b. there might not be any windowing environment
558 * 2. by a user entered command.
559 * a. the command can be entered from a cmdtool type OW/CDE tool
560 * b. the command can be entered by a user logged in on a dumb
562 * i) there might be a OW/CDE running on console
563 * and we have permission to talk to it.
564 * ii) there is no OW/CDE running on console or we
565 * don't have permission to talk to it or console
566 * itself is the dumb terminal we have logged into.
568 * In main(), we decide on the correct case and call appropriate functions.
572 main(int argc
, char **argv
)
575 char display_name
[MAXNAMELEN
+ 9] = "DISPLAY=";
576 char xauthority
[MAXPATHLEN
+ 12] = "XAUTHORITY=";
579 (void) signal(SIGHUP
, SIG_IGN
);
580 (void) signal(SIGINT
, SIG_IGN
);
581 (void) signal(SIGQUIT
, SIG_IGN
);
582 (void) signal(SIGTSTP
, SIG_IGN
);
583 (void) signal(SIGTTIN
, SIG_IGN
);
584 (void) signal(SIGTTOU
, SIG_IGN
);
587 * If suspend is invoked from a daemon (case 1 above), it
588 * will not have a working stdin, stdout and stderr. We need
589 * these to print proper error messages and possibly get user
590 * input. We attach them to console and hope that attachment
593 if (ttyname(0) == NULL
) {
595 (void) dup2(open("/dev/console", O_RDONLY
), 0);
596 (void) dup2(open("/dev/console", O_WRONLY
), 1);
597 (void) dup2(open("/dev/console", O_WRONLY
), 2);
600 while ((c
= getopt(argc
, argv
, "fnxhtd:")) != EOF
) {
604 * Force machine to poweroff if
610 /* No warning popups - Obsolete */
614 /* Don't try to screenlock */
618 /* Do a shutdown instead of suspend */
623 /* Set the DISPLAY value in the environment */
624 if (strlen(optarg
) >= MAXNAMELEN
) {
625 (void) printf(gettext("Error: "
626 "display name is too long.\n"));
629 (void) strcat(display_name
, optarg
);
630 if (putenv(display_name
) != 0) {
631 (void) printf(gettext("Error: "
632 "unable to set DISPLAY "
633 "environment variable.\n"));
638 /* Test, don't actually do any operation */
642 (void) printf(gettext("USAGE: suspend "
643 "[-fnxh] [-d <display>]\n"));
649 * The action of pressing power key and power button on a MOU-3 machine
650 * causes suspend being invoked with SYSSUSPENDDODEFAULT
651 * enviromental variable set - indicating the default action is machine
652 * dependent: for MOU-3 type machine, "LowPower" mode is the default,
653 * for all the rest, "Suspend" is the default. Existing suspend
654 * flags works the same.
656 if (getenv("SYSSUSPENDDODEFAULT"))
660 if ((flags
& FORCE
) && (flags
& LOWPOWER
))
664 * Flag "-h" overrides flag "-f".
666 if ((flags
& SHUTDOWN
) && (flags
& FORCE
))
667 flags
&= ~(FORCE
| LOWPOWER
);
673 * Check initally if the user has the authorizations to
674 * do either a suspend or shutdown. pm_suspend() will also
675 * make this test, so we could defer till then, but if we
676 * do it now, we at least prevent a lot of unneeded setup.
678 pw
= getpwuid(getuid());
679 (void) strncpy(user
, pw
->pw_name
, NMAX
);
681 if ((flags
& (FORCE
|SHUTDOWN
)) &&
682 (chkauthattr(AUTHNAME_SHUTDOWN
, pw
->pw_name
) != 1)) {
683 (void) printf(gettext("User does not have correct "
684 "authorizations to shutdown the machine.\n"));
687 if (!(flags
& SHUTDOWN
) &&
688 (chkauthattr(AUTHNAME_SUSPEND
, pw
->pw_name
) != 1)) {
689 (void) printf(gettext("User does not have correct "
690 "authorizations to suspend.\n"));
695 * If we are only shutting down, there isn't much to do, just
696 * call pm_poweroff(), and let it do all the work.
698 if (flags
& SHUTDOWN
) {
700 * pm_poweroff either powers off or exits,
701 * so there is no return.
704 (void) printf("TEST: This machine would have "
714 * If XAUTHORITY environment variable is not set, try to set
717 if (getenv("XAUTHORITY") == NULL
)
718 (void) putenv(get_xauthority(xauthority
));
721 * In case of "suspend" being called from daemon "powerd",
722 * signal SIGALRM is blocked so use "sigset()" instead of "signal()".
724 (void) sigset(SIGALRM
, alarm_handler
);
726 /* Call the "suspend" function to do the last of the work */
729 if (refresh_dt() == -1) {
730 (void) printf("%s: Failed to refresh screen.\n", argv
[0]);
739 * Note that some of these functions are more relevant to Sparc platforms,
740 * but they do function properly on other platforms, they just don't do
745 * This tells the PM framework to put the devices it controls in an idle
746 * state. The framework only complains if a device that *must* be idle
747 * doesn't succeed in getting there.
754 if ((fd
= open("/dev/pm", O_RDWR
)) < 0) {
755 (void) printf(gettext("Can't open /dev/pm\n"));
759 if (ioctl(fd
, PM_IDLE_DOWN
, NULL
) < 0) {
760 (void) printf(gettext("Failed to bring system "
761 "to low power mode.\n"));
772 * Though this test is predominantly used on Sparc, it will run on other
773 * platforms, and might be usefull one day on those.
782 if ((fd
= open(CPR_CONFIG
, O_RDONLY
)) < 0) {
783 (void) printf(gettext("Can't open /etc/.cpr_config file."));
787 if (read(fd
, (void *) &cf
, sizeof (cf
)) != sizeof (cf
)) {
788 (void) printf(gettext("Can't read /etc/.cpr_config file."));
790 found
= cf
.is_autopm_default
;
798 * Reauthenticate the user on return from suspend.
799 * This is here and not in the PAM-specific file, as there are
800 * items specific to sys-suspend, and not generic to PAM. This may
801 * become part of a future PM library. The audit handle is passed,
802 * as the pm_suspend code actually starts an audit session, so it
803 * makes sense to just continue to use it. If it were separated
804 * from the pm_suspend code, it will need to open a new session.
806 #define DEF_ATTEMPTS 3
808 pm_do_auth(adt_session_data_t
*ah
)
810 pam_handle_t
*pm_pamh
;
814 struct pam_conv pam_conv
= {pam_tty_conv
, NULL
};
819 if ((err
= pam_start("sys-suspend", user
, &pam_conv
,
820 &pm_pamh
)) != PAM_SUCCESS
)
823 pam_flag
= PAM_DISALLOW_NULL_AUTHTOK
;
826 err
= pam_authenticate(pm_pamh
, pam_flag
);
828 if (err
== PAM_SUCCESS
) {
829 err
= pam_acct_mgmt(pm_pamh
, pam_flag
);
831 if (err
== PAM_NEW_AUTHTOK_REQD
) {
835 err
= pam_chauthtok(pm_pamh
,
836 PAM_CHANGE_EXPIRED_AUTHTOK
);
839 } while ((err
== PAM_AUTHTOK_ERR
||
840 err
== PAM_TRY_AGAIN
) &&
841 chpasswd_tries
< DEF_ATTEMPTS
);
842 pm_audit_event(ah
, ADT_passwd
, err
);
844 err
= pam_setcred(pm_pamh
, PAM_REFRESH_CRED
);
846 if (err
!= PAM_SUCCESS
) {
847 (void) fprintf(stdout
, "%s\n",
848 pam_strerror(pm_pamh
, err
));
849 pm_audit_event(ah
, ADT_screenunlock
, err
);
851 } while (err
!= PAM_SUCCESS
);
852 pm_audit_event(ah
, ADT_passwd
, 0);
854 (void) pam_end(pm_pamh
, err
);